lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.5k stars 164 forks source link

Improve printing of floating point numbers #949

Open certik opened 2 years ago

certik commented 2 years ago

Currently code like this:

print([1.23 , 324.3 , 56.431, 90.5, 34.1])

Can print like this:

[    1.22999999999999998,   324.30000000000001137,    56.43099999999999739,    90.50000000000000000,    34.10000000000000142]

But it should print like this:

[1.23, 324.3, 56.431, 90.5, 34.1]

We can look how CPython does it, but one algorithm is to take the above strings, try to remove last 4 digits, and then check if there is a sequence of 4 or more 0s or 9s. If so, then remove all 0s and print the rest, so 324.30000000000001137 becomes 324.3. If it is 9s, then remove all 9s, then add one in the last place, so 56.43099999999999739 -> 56.4309999999999 -> 56.430 -> 56.431.

nikhil061102 commented 1 year ago

Hello @certik I suppose I have a solution for this issue. Can I work on it?

Thirumalai-Shaktivel commented 1 year ago

@nikhil061102 go ahead and submit a PR with the required changes. Any doubts, please ask.