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
This commit is contained in:
skilchen
2018-06-05 06:09:07 +02:00
committed by Varriount
parent fd102f39bb
commit 230692a22f
2 changed files with 14 additions and 1 deletions

View File

@@ -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

View File

@@ -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"