Skip to content

Commit 83fbb94

Browse files
committedAug 29, 2022
fix bad fixed point conversion precision
1 parent 0fb1da5 commit 83fbb94

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed
 

‎software/nimapp/src/fp128.nim

+11-5
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,38 @@ proc strToFp128*(s: string): Int128 =
6767
for i in 0 ..< len(frac):
6868
exponent *= i128(10)
6969
# we shift in two steps to avoid shaving off the MSBs
70-
((frac.parseInt128() shl (SCALE div 2)) div exponent) shl (SCALE div 2)
70+
((frac.parseInt128() shl (SCALE - 2)) div exponent) shl 2
7171
else:
7272
i128(0)
7373

7474
let absresult = whole.parseInt128 shl SCALE + fraction
7575
if negative: -absresult: else: absresult
7676

7777
proc test() =
78-
let negpi = cast[UInt128](strToFp128("-3.14159265359"))
78+
let
79+
negpi_str = "-3.1415926535897932384"
80+
negpi = cast[UInt128](strToFp128(negpi_str))
7981
echo negpi.toHex()
8082
echo cast[UInt128](strToFp128("-1")).toHex()
8183
echo cast[UInt128](strToFp128("1")).toHex()
8284
echo cast[UInt128](strToFp128("1.25")).toHex()
8385
echo cast[UInt128](strToFp128("1.32")).toHex()
8486
echo " 998877665544332211"
8587

88+
echo negpi_str
8689
echo fp128ToStr(cast[Int128](negpi))
90+
8791
echo fp128ToStr(strToFp128("-1"))
8892
echo fp128ToStr(strToFp128("1"))
8993
echo fp128ToStr(strToFp128("1.25"))
9094
echo fp128ToStr(strToFp128("1.32"))
9195
echo fp128ToStr(strToFp128("1.3333333333333333333"))
92-
echo fp128ToStr(strToFp128("0.0000000000000000001"))
93-
echo fp128ToStr(i128(1))
96+
echo fp128ToStr(strToFp128("0.000000000000000001"))
97+
echo "smallest possible:\n", fp128ToStr(i128(1))
9498
echo fp128ToStr(strToFp128(""))
95-
echo fp128ToStr(strToFp128("-0.95908709673676639795"))
99+
let hi_bit_number = "-0.95908709673676639795"
100+
echo hi_bit_number
101+
echo fp128ToStr(strToFp128(hi_bit_number))
96102

97103
#test()
98104
#quit()

0 commit comments

Comments
 (0)
Please sign in to comment.