Git Product home page Git Product logo

Comments (3)

TomFrost avatar TomFrost commented on September 17, 2024

Unfortunately, there's nothing Jexl can do about this -- the behavior you're seeing is native to Node, and also native to most floating point math operations on an x86 processor. It's kind of crazy how obviously wrong it is to us humans, but with how the x86 works, that's just how it comes out. Regardless of the programming language you use, it's best to convert your significant figures to integers and handle the decimal superficially.

$ node
> 1.2+1.222
2.4219999999999997
$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1.2 + 1.222
2.4219999999999997
$ irb
irb(main):001:0> 1.2 + 1.222
=> 2.4219999999999997

(oddly enough, PHP autocorrects for decimal math. Point for PHP.)
(I got curious and tried it in Go as well -- Go does the correction automatically too!)

from jexl.

erotavlas avatar erotavlas commented on September 17, 2024

Is it possible to overload the math operators to use operations from another library (such as BigNumber.js , Big.js, or, Decimal.js) I was reading somewhere that this library evaluates to the correct number of decimal places. https://stackoverflow.com/a/27251971/1462656

EDIT - I tested those libraries and they produce the expected values.

from jexl.

TomFrost avatar TomFrost commented on September 17, 2024

@erotavlas: You certainly can! jexl.addBinaryOp(str, (left, right) => { ... })) is exactly what you need. While the intention for this call is to add new operators to the Jexl grammar, you can use it to overwrite existing ones like + and -. For reference, here is a list of all built in unary and binary operators: https://github.com/TechnologyAdvice/Jexl/blob/2.0/lib/grammar.js

Though, as a performance suggestion, I'd shy away from overwriting + and - et. al. because doing complex calculation for every arithmetic operation would make evaluations unnecessarily heavy. It might be worth it to add operators like +. and -. to denote decimal-sensitive arithmetic instead, and only use them when you know you need them.

from jexl.

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.