Permalink
Browse files

Fix strformat neg zero (#7954)

* fix strformat handling of neg zero with sign

* better tests for neg zero with sign

* use inplace insertion of the sign as suggested by Varriount
  • Loading branch information...
skilchen authored and Varriount committed Jun 5, 2018
1 parent fd102f3 commit 230692a22f92ed010e04ba8c1b2b95f86350f1a5
Showing with 14 additions and 1 deletion.
  1. +6 −1 lib/pure/strformat.nim
  2. +8 −0 tests/stdlib/tstrformat.nim
@@ -527,8 +527,13 @@ proc format*(value: SomeFloat; specifier: string; res: var string) =
var sign = false
if value >= 0.0:
if spec.sign != '-':
f = spec.sign & f
sign = true
if value == 0.0:
if 1.0 / value == Inf:
# only insert the sign if value != negZero
f.insert($spec.sign, 0)
else:
f.insert($spec.sign, 0)
else:
sign = true

@@ -46,3 +46,11 @@ doAssert fmt"{-1.5:0>8}" == "0000-1.5" # even that does not work for negative fl
doAssert fmt"{-1.5:08}" == "-00001.5" # works
doAssert fmt"{1.5:+08}" == "+00001.5" # works
doAssert fmt"{1.5: 08}" == " 00001.5" # works

# only add explicitly requested sign if value != -0.0 (neg zero)
doAssert fmt"{-0.0:g}" == "-0"
doassert fmt"{-0.0:+g}" == "-0"
doassert fmt"{-0.0: g}" == "-0"
doAssert fmt"{0.0:g}" == "0"
doAssert fmt"{0.0:+g}" == "+0"
doAssert fmt"{0.0: g}" == " 0"

0 comments on commit 230692a

Please sign in to comment.