diff options
author | Timothee Cour <timothee.cour2@gmail.com> | 2020-12-27 05:35:01 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-27 14:35:01 +0100 |
commit | 357729639ff970ba934a0dea2ae06ff063e37910 (patch) | |
tree | 14492f138b9c1d58fd78cbe82249cf4133cad695 | |
parent | 0c8ce2dccf6f779dae01a6a66cf7a29b50796481 (diff) | |
download | Nim-357729639ff970ba934a0dea2ae06ff063e37910.tar.gz |
fix #16469 vm float constants: do not conflate -0.0 and 0.0 (#16470)
* fix #16469 vm float constants: do not conflate -0.0 and 0.0 * fix test for 32bit
-rw-r--r-- | compiler/vmgen.nim | 7 | ||||
-rw-r--r-- | tests/float/tfloatnan.nim | 15 |
2 files changed, 20 insertions, 2 deletions
diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index 2fbb78c8f..9f36fc736 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -453,7 +453,12 @@ proc sameConstant*(a, b: PNode): bool = of nkSym: result = a.sym == b.sym of nkIdent: result = a.ident.id == b.ident.id of nkCharLit..nkUInt64Lit: result = a.intVal == b.intVal - of nkFloatLit..nkFloat64Lit: result = a.floatVal == b.floatVal + of nkFloatLit..nkFloat64Lit: + result = cast[uint64](a.floatVal) == cast[uint64](b.floatVal) + # refs bug #16469 + # if we wanted to only distinguish 0.0 vs -0.0: + # if a.floatVal == 0.0: result = cast[uint64](a.floatVal) == cast[uint64](b.floatVal) + # else: result = a.floatVal == b.floatVal of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal of nkType, nkNilLit: result = a.typ == b.typ of nkEmpty: result = true diff --git a/tests/float/tfloatnan.nim b/tests/float/tfloatnan.nim index 8f384c3d9..9e3dd94f6 100644 --- a/tests/float/tfloatnan.nim +++ b/tests/float/tfloatnan.nim @@ -15,7 +15,7 @@ echo "Nim: ", f32, " (float)" let f64: float64 = NaN echo "Nim: ", f64, " (double)" -block: # issue #10305 +block: # bug #10305 # with `-O3 -ffast-math`, generated C/C++ code is not nan compliant # user can pass `--passC:-ffast-math` if he doesn't care. proc fun() = @@ -42,3 +42,16 @@ block: # issue #10305 fun() fun2(0) +template main() = + # xxx move all tests under here + block: # bug #16469 + let a1 = 0.0 + let a2 = -0.0 + let a3 = 1.0 / a1 + let a4 = 1.0 / a2 + doAssert a3 == Inf + doAssert a4 == -Inf + doAssert $(a1, a2, a3, a4) == "(0.0, -0.0, inf, -inf)" + +static: main() +main() |