Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

round(Float64, 1.2) is documented to return 1.0, but throws. #50778

Closed
LilithHafner opened this issue Aug 3, 2023 · 4 comments · Fixed by #50812
Closed

round(Float64, 1.2) is documented to return 1.0, but throws. #50778

LilithHafner opened this issue Aug 3, 2023 · 4 comments · Fixed by #50812
Labels
bug Indicates an unexpected problem or unintended behavior maths Mathematical functions

Comments

@LilithHafner
Copy link
Member

help?> round(Float64, 1.2)
  round([T,] x, [r::RoundingMode])
  round(x, [r::RoundingMode]; digits::Integer=0, base = 10)
  round(x, [r::RoundingMode]; sigdigits::Integer, base = 10)

  Rounds the number x.

  Without keyword arguments, x is rounded to an integer value, returning a value 
  of type T, or of the same type of x if no T is provided. An InexactError will be 
  thrown if the value is not representable by T, similar to convert.

...

julia> round(Float64, 1.2)
ERROR: MethodError: no method matching round(::Type{Float64}, ::Float64)

Closest candidates are:
  round(::Type{Bool}, ::AbstractFloat)
   @ Base float.jl:391
  round(::Type{T}, ::AbstractFloat, ::RoundingMode) where T<:Integer
   @ Base floatfuncs.jl:122
  round(::Type{T}, ::AbstractFloat) where T<:Integer
   @ Base float.jl:385
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[13]:1
@LilithHafner LilithHafner added bug Indicates an unexpected problem or unintended behavior docs This change adds or pertains to documentation maths Mathematical functions labels Aug 3, 2023
@oscardssmith
Copy link
Member

yeah that's a bug

@oscardssmith
Copy link
Member

oscardssmith commented Aug 3, 2023

Also, when you fix this make sure that round(Float32, 1e60) and round(Float32, floatmax(Float32)-1.0) throw inexact errors.

@LilithHafner LilithHafner removed the docs This change adds or pertains to documentation label Aug 6, 2023
@LilithHafner
Copy link
Member Author

The docstring says

An InexactError will be thrown if the value is not representable by T, similar to convert.

but looking at the behavior of convert, convert(Float32, 1e60) == Inf32—it does not throw. The documented criterion is "representable" not "exactly representable".

Also, the implementation is simpler if this does not throw.

@Seelengrab
Copy link
Contributor

Seelengrab commented Aug 7, 2023

The "similar to" indicates to me that it's not about the exact behavior of convert, but whether the value is losslessy convertible. That is not possible with Inf32, though the docstring should probably clarify that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior maths Mathematical functions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants