[jsbuilt-ins] fixing numerical stability of to_string
This commit is contained in:
parent
56cecddc75
commit
9ead39caee
@ -90,21 +90,20 @@ def to_string(o):
|
|||||||
elif isinf(ov):
|
elif isinf(ov):
|
||||||
return 'Infinity'
|
return 'Infinity'
|
||||||
else:
|
else:
|
||||||
n = log10(ov)
|
# numerically unstable example: 3333330000000000000.3 or 3.3333300000000000003e+20
|
||||||
c = 1 if 0 < n else 0
|
n = log10(ov) + 1
|
||||||
n = int(n)
|
n = int(n)
|
||||||
k = 1
|
k = 1
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
exp = 10 ** (n - k)
|
exp = 10 ** (k - n)
|
||||||
s = int(ov / exp)
|
s = int(ov * exp)
|
||||||
if abs(ov - s * exp) < float_info.epsilon:
|
if abs(ov * exp - s) < float_info.epsilon:
|
||||||
break
|
break
|
||||||
k += 1
|
k += 1
|
||||||
|
|
||||||
if s % 10 == 0:
|
if s % 10 == 0:
|
||||||
s //= 10
|
s //= 10
|
||||||
n += c
|
|
||||||
m = '%d' % s
|
m = '%d' % s
|
||||||
|
|
||||||
if k <= n <= 21:
|
if k <= n <= 21:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user