Skip to content

Commit

Permalink
printing float values will have one more digit. (nim-lang#13276) [bac…
Browse files Browse the repository at this point in the history
…kport]

* printing float values will have one more digit. Fixes nim-lang#13196
  • Loading branch information
Arne Döring committed Feb 7, 2020
1 parent cdedb86 commit b2c6db9
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 41 deletions.
4 changes: 2 additions & 2 deletions lib/pure/json.nim
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ proc pretty*(node: JsonNode, indent = 2): string =
## Similar to prettyprint in Python.
runnableExamples:
let j = %* {"name": "Isaac", "books": ["Robot Dreams"],
"details": {"age": 35, "pi": 3.1415}}
"details": {"age": 35, "number": 3.125}}
doAssert pretty(j) == """
{
"name": "Isaac",
Expand All @@ -686,7 +686,7 @@ proc pretty*(node: JsonNode, indent = 2): string =
],
"details": {
"age": 35,
"pi": 3.1415
"number": 3.125
}
}"""
result = ""
Expand Down
2 changes: 1 addition & 1 deletion lib/system/formatfloat.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ proc writeFloatToBuffer*(buf: var array[65, char]; value: BiggestFloat): int =
## * `buf` - A buffer to write into. The buffer does not need to be
## initialized and it will be overridden.
##
var n: int = c_sprintf(addr buf, "%.16g", value)
var n: int = c_sprintf(addr buf, "%.17g", value)
var hasDot = false
for i in 0..n-1:
if buf[i] == ',':
Expand Down
5 changes: 2 additions & 3 deletions tests/errmsgs/tsigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ proc f(a1: string; a2: varargs[string]; a3: float; a4: var string)
required type for a4: var string
but expression '"bad"' is immutable, not 'var'
expression: f("asdf", "1", "2", "3", "4", 2.3, "bad")
expression: f("asdf", "1", "2", "3", "4", 2.25, "bad")
tsigmatch.nim(164, 4) Error: type mismatch: got <string, a0: int literal(12)>
but expected one of:
proc f(x: string; a0: var int)
Expand Down Expand Up @@ -153,7 +153,7 @@ block:
# sigmatch gets confused with param/arg position after varargs
proc f(a1: int) = discard
proc f(a1: string, a2: varargs[string], a3: float, a4: var string) = discard
f("asdf", "1", "2", "3", "4", 2.3, "bad")
f("asdf", "1", "2", "3", "4", 2.25, "bad")

block:
# bug: https://github.com/nim-lang/Nim/issues/11061#issuecomment-508970046
Expand All @@ -169,4 +169,3 @@ block:
proc fun1(a1: MyInt, a2: Mystring) = discard
proc fun1(a1: float, a2: Mystring) = discard
fun1(Mystring.default, "asdf")

25 changes: 8 additions & 17 deletions tests/float/tfloat6.nim
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
discard """
output: '''
1e-06 : 1e-06
1e-06 : 1e-06
0.001 : 0.001
1e-06 : 1e-06
1e-06 : 1e-06
10.000001 : 10.000001
100.000001 : 100.000001
'''
disabled: "windows"
disabled: "windows"
"""

import strutils

echo "0.00_0001".parseFloat(), " : ", 1E-6
echo "0.00__00_01".parseFloat(), " : ", 1E-6
echo "0.0_01".parseFloat(), " : ", 0.001
echo "0.00_000_1".parseFloat(), " : ", 1E-6
echo "0.00000_1".parseFloat(), " : ", 1E-6
doAssert "0.00_0001".parseFloat() == 1E-6
doAssert "0.00__00_01".parseFloat() == 1E-6
doAssert "0.0_01".parseFloat() == 0.001
doAssert "0.00_000_1".parseFloat() == 1E-6
doAssert "0.00000_1".parseFloat() == 1E-6

echo "1_0.00_0001".parseFloat(), " : ", 10.000001
echo "1__00.00_0001".parseFloat(), " : ", 1_00.000001
doAssert "1_0.00_0001".parseFloat() == 10.000001
doAssert "1__00.00_0001".parseFloat() == 1_00.000001
19 changes: 19 additions & 0 deletions tests/float/tfloat8.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
discard """
disabled: windows
"""

{.passL: "-lm".} # not sure how to do this on windows

import strutils

proc nextafter(a,b: float64): float64 {.importc: "nextafter", header: "<math.h>".}

var myFloat = 2.5

for i in 0 .. 100:
let newFloat = nextafter(myFloat, Inf)
let oldStr = $myFloat
let newStr = $newFloat
doAssert parseFloat(newStr) == newFloat
doAssert oldStr != newStr
myFloat = newFloat
5 changes: 2 additions & 3 deletions tests/generics/tforwardgeneric.nim
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
discard """
output: "1.1 11\n42\n0"
output: "1.125 11\n42\n0"
ccodecheck: "!@'ClEnv'"
"""

proc p[T](a, b: T): T

echo p(0.9, 0.1), " ", p(9, 1)
echo p(0.875, 0.125), " ", p(9, 1)

proc p[T](a, b: T): T =
let c = b
Expand All @@ -25,4 +25,3 @@ proc print[T](t: T) =
echo t

echo bar()

12 changes: 6 additions & 6 deletions tests/generics/tgenerics_issues.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ discard """
perm: 22 det: 22
TMatrix[3, 3, system.int]
3
@[0.9, 0.1]
@[0.875, 0.125]
U[3]
U[(f: 3)]
U[[3]]
Expand All @@ -20,9 +20,9 @@ concrete 88
2
3
!!Hi!!
G:0,1:0.1
G:0,1:0.1
H:1:0.1
G:0,1:0.125
G:0,1:0.125
H:1:0.125
'''
joinable: false
"""
Expand Down Expand Up @@ -370,7 +370,7 @@ block t2304:
type TV2[T:SomeNumber] = array[0..1, T]
proc newV2T[T](x, y: T=0): TV2[T] = [x, y]

let x = newV2T[float](0.9, 0.1)
let x = newV2T[float](0.875, 0.125)
echo(@x)


Expand Down Expand Up @@ -711,7 +711,7 @@ block t4863:
proc q[j: static[int]](x:H[j]) = echo "H:", j, ":", x.v

var
g0 = G[0,1](v: 0.1)
g0 = G[0,1](v: 0.125)
h0:H[1] = g0
p(g0)
p(h0)
Expand Down
6 changes: 3 additions & 3 deletions tests/objects/t12753.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
discard """
output: '''
(v: [(v: [0.0, 1.1]), (v: [2.2, 3.3])])
(v: [(v: [0.0, 1.1]), (v: [2.2, 3.3])])
(v: [(v: [0.0, 1.125]), (v: [2.25, 3.375])])
(v: [(v: [0.0, 1.125]), (v: [2.25, 3.375])])
'''
"""

Expand All @@ -13,7 +13,7 @@ type

var
a = M(v:[ V(v:[0.0,1.0]), V(v:[2.0,3.0]) ])
b = M(v:[ V(v:[0.0,0.1]), V(v:[0.2,0.3]) ])
b = M(v:[ V(v:[0.0,0.125]), V(v:[0.25,0.375]) ])

echo M(v: [V(v: [b.v[0].v[0] + a.v[0].v[0], b.v[0].v[1] + a.v[0].v[1]]),
V(v: [b.v[1].v[0] + a.v[1].v[0], b.v[1].v[1] + a.v[1].v[1]])])
Expand Down
4 changes: 2 additions & 2 deletions tests/showoff/tdrdobbs_examples.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
discard """
output: '''108
11 -1 1936
0.4
0.5
true
truefalse'''
"""
Expand Down Expand Up @@ -75,7 +75,7 @@ proc evaluate(n: Formula, varToVal: proc (name: string): float): float =
of fkMul: evaluate(n.left, varToVal) * evaluate(n.right, varToVal)
of fkExp: pow(evaluate(n.left, varToVal), evaluate(n.right, varToVal))

echo evaluate(Formula(kind: fkLit, value: 0.4), nil)
echo evaluate(Formula(kind: fkLit, value: 0.5), nil)

proc isPolyTerm(n: Formula): bool =
n.kind == fkMul and n.left.kind == fkLit and (let e = n.right;
Expand Down
4 changes: 0 additions & 4 deletions tests/system/tostring.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ doAssert """["", "foo", "bar"]""" == $(@["", "foo", "bar"].toOpenArray(0, 2))
# bug #2395
let alphaSet: set[char] = {'a'..'c'}
doAssert "{'a', 'b', 'c'}" == $alphaSet
doAssert "2.3242" == $(2.3242)
doAssert "2.982" == $(2.982)
doAssert "123912.1" == $(123912.1)
doAssert "123912.1823" == $(123912.1823)
doAssert "5.0" == $(5.0)
doAssert "1e+100" == $(1e100)
doAssert "inf" == $(1e1000000)
Expand Down

0 comments on commit b2c6db9

Please sign in to comment.