Comments (3)
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.
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.
@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)
- Match operator not recognized HOT 2
- Opinions on functions vs transforms? HOT 3
- '&&' and '||' priority HOT 1
- -a(2,3) HOT 1
- Scientific number notation: 1e2, 2.1e-3 etc HOT 3
- Cannot index into static array or object HOT 4
- Is this project maintained? Do you want a hand? HOT 8
- Errors for bad identifiers HOT 1
- Object's prototype methods return undefined for token type
- Security? does it use actual JS eval function? HOT 1
- Problem with inline if HOT 1
- How to convert AST to an expression string? HOT 5
- Object keys with a space HOT 2
- Run in browser?
- Cannot read properties of undefined (reading '0')
- Backslashes are wrongly unescaped
- Keys with hyphens evaluate to null HOT 1
- Inconsistency between null and undefined operations HOT 1
- Transform Confusion HOT 1
- Minus token is confused for binaryOp in function arguments HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jexl.