Code Monkey home page Code Monkey logo

Comments (4)

etrepum avatar etrepum commented on July 22, 2024

0.3 does not have an exact floating point representation, and the version of Python you're using doesn't use a clever algorithm to compactly repr floats.

If you use Decimal('0.3') which is exactly 0.3 then you will get the expected output.

from simplejson.

etrepum avatar etrepum commented on July 22, 2024

Here's some more evidence:

In [1]: repr(0.3)
Out[1]: '0.29999999999999999'

In [2]: 0.29999999999999999 == 0.3
Out[2]: True

If this doesn't make sense to you, I suggest reading up on how floating point works (IEEE 754).

from simplejson.

ukch avatar ukch commented on July 22, 2024

I do understand the issue, but I don't agree that nothing should be done to work around it. Converting to string rather than just doing a repr yields the correct human-readable (and JavaScript-readable) string.

In [1]: str(0.3)
Out[1]: '0.3'

In [2]: str(0.29999999999999999)
Out[2]: '0.3'

But at the end of the day, it's your code, and if you don't think this issue should be fixed, then no worries - I will continue to work around it as I am at present.

from simplejson.

etrepum avatar etrepum commented on July 22, 2024

Sorry, but that is no good. str(f) is lossy for floats, so it can not be used.

>>> import math
>>> float(str(math.pi)) == math.pi
False
>>> float(repr(math.pi)) == math.pi
True

This is more of an issue with Python than simplejson.

from simplejson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.