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(Int, -Inf16) doesn't throw #51113

Closed
LilithHafner opened this issue Aug 30, 2023 · 2 comments · Fixed by #50812
Closed

round(Int, -Inf16) doesn't throw #51113

LilithHafner opened this issue Aug 30, 2023 · 2 comments · Fixed by #50812
Labels
domain:maths Mathematical functions kind:bug Indicates an unexpected problem or unintended behavior

Comments

@LilithHafner
Copy link
Member

julia> round(Int32, -Inf16)
-2147483648

julia> round(Int64, -Inf16)
-9223372036854775808

julia> round(Int128, -Inf16)
0

julia> versioninfo()
Julia Version 1.10.0-beta2
Commit a468aa198d0 (2023-08-17 06:27 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 8 × Apple M2
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
  Threads: 1 on 4 virtual cores
Environment:
  JULIA_EDITOR = code
@LilithHafner LilithHafner added kind:bug Indicates an unexpected problem or unintended behavior domain:maths Mathematical functions labels Aug 30, 2023
@ararslan
Copy link
Member

Looks like this is because Float16(typemin(Int)) is -Inf16 and the check in trunc is inclusive on the lower bound. Compare that to Float32 for which Float32(typemin(Int)) > -Inf32. In fact, this is touched on in a comment in the code:

This assumes that Tf(typemin(Ti)) > -Inf, which is true for these types, but not for Float16 or larger integer types.

Perhaps that assumption should be explicit as part of the condition? Like

--- a/base/float.jl
+++ b/base/float.jl
@@ -953,7 +953,7 @@ for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UIn
             # these types, but not for `Float16` or larger integer types.
             @eval begin
                 function trunc(::Type{$Ti},x::$Tf)
-                    if $(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti)))
+                    if isfinite(x) && $(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti)))
                         return unsafe_trunc($Ti,x)
                     else
                         throw(InexactError(:trunc, $Ti, x))

@LilithHafner
Copy link
Member Author

I noticed this issue when writing tests for #51095, but #50812 should already fix it.

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

Successfully merging a pull request may close this issue.

2 participants