behavior of trunc, floor, and ceil on rationals seems inconsistent with the rest of julia "Reals"

222 views
Skip to first unread message

Mike Bryniarski

unread,
Nov 21, 2015, 9:27:38 AM11/21/15
to julia-dev
I noticed for general reals these functions call signatures take the form:

trunc{T<:Integer}(::Type{T<:Integer}, x::Number)

However, for rationals, the signature is actually:

trunc{T}(::Type{T}, x::Rational)

This means that you can do things like trunc(Float64,5//2), while trunc(Float64,x) is not defined for numeric types. Additionally trunc(1.0) returns a Float64 but trunc(Float64,1.0) is undefined. It seems to me, either these operations should allow any Real output type or be consistently restricted to integers (i.e. trunc{T<:Integer}(::Type{T}, x::Rational)). 

Also if trunc to floats actually makes sense things like trunc(Float64,2^53+1//1) should throw an inexact error.

One last note that is sort of related: If one wants to use the unsafe_trunc function to speed up some code (with the understanding of its limitations) you have to make sure it is actually being given floats. I currently get around this by writing something like unsafe_trunc{T<:Integer}(::Type{T}, x::Number) = trunc(T,x). is there any reason this fallback behavior for this function should not be included in Julia by default?

Jeffrey Sarnoff

unread,
Nov 30, 2015, 1:27:32 PM11/30/15
to julia-dev
Also ceil, floor, round.
Given that these functions return a floating point value when no type is specified as an initial argument, allowing a floating point type as the initial argument makes sense to me.
Reply all
Reply to author
Forward
0 new messages