This appears to be a bug.  The docs for rationalize 
<http://docs.julialang.org/en/latest/stdlib/math> say 
rationalize([*Type=Int*, ]*x; tol=eps(x)*) Approximate floating point number
 x as a Rational number with components of the given integer type. The 
result will differ from x by no more than tol.

meanwhile, here is a "long way around" that may do what you want (signature 
and special cases are cribbed from rational.jl)

function rationalized{T<:Integer}(::Type{T}, x::AbstractFloat; 
tol::Real=eps(x))
    tol < 0 && throw(ArgumentError("negative tolerance'))
    isnan(x) && return zero(T)//zero(T)
    isinf(x) && return (x < 0 ? -one(T) : one(T)) // zero(T)
    rationalize(round(x, floor(Int,abs(log2(tol)))))
end


On Thursday, May 5, 2016 at 11:13:45 AM UTC-4, James Fairbanks wrote:
>
> Hello julia-users,
>
> Does rationalize ever return 0//1 for nonzero inputs?
>
> 0.0+eps(Float64) is much closer to 0//1 than to 1//1.
> Why can't I get 0//1 out of rationalize(x) if x != 0.0?
> Is this to avoid divide by 0 errors when using a/rationalize(x) where 0 < 
> x < tol?
>
> For example on julia v0.4.5 
> julia> rationalize(0.0)
> 0//1
>
> julia> rationalize(0.0+eps(Float64))
> 1//4503599627370496
>
> julia> rationalize(0.0+eps(Float64);tol=1e-4)
> 1//1
>
> julia> rationalize(0.0+eps(Float64);tol=1e-4)
> 1//1
>
> Thanks,
> James
>

Reply via email to