In JavaScript 0.1 + 0.2 = 0.30000000000000004
and Is floating point math broken? explains why because of IEEE 754 math. I am working with applications in the finance/accounting domain and want a way to make 0.1 + 0.2 = 0.3
Therefore, I am looking for a solution that can take code similar to the below example with type annotations for Decimal (using the TypeScript syntax).
var a:Decimal, b:Decimal, c:Decimal;
a = 0.1;
b = 0.2;
c = a + b;
The compile/macro expansion process could output this code which uses a JavaScript Decimal library (e.g. decimal.js), so c = 0.3
and not 0.30000000000000004
var a, b, c;
a = new Decimal(0.1);
b = new Decimal(0.2);
c = a.plus(b);
Mozilla sweet.js macros seems to support custom operators which "let you define your own operators or override the built-in operators."
Question: Is this behavior possible with sweet.js? Are there any similar examples?
TypeScript transpiles source code and this concept has been asked about before with TypeScript:
Question: Can the TypeScript compiler be extended to support this use case? Would this be appropriate for a feature request?
Additionally, a blog post in a series about cross-compiling ActionScript to JavaScript concluded they wanted "same wrong result in ActionScript and JavaScript" for consistency reasons, but that did not assume additional type annotations.
Question: Are there other strategies or compile-to-JavaScript options to achieve this goal?