1

Jint is a Javascript interpreter for .NET. It will convert JavaScript values to CLR objects like below. https://github.com/sebastienros/jint/blob/master/README.md

JINT converts JavaScript values to CLR objects

Object -> expando objects ( IDictionary and dynamic)
Array -> object[]
Date -> DateTime
number -> double
string -> string
boolean -> bool
Regex -> RegExp
Function -> Delegate

It seems that JavaScript in general only supports 64-point floating point numbers and no other number type. https://www.w3schools.com/js/js_numbers.asp That means there would likely be a need for some functions/libraries to ensure consistent handling for numbers that required fixed precisions during processing.

My questions is if JINT supports 128-bit floating point calculation. We have a need to keep numbers in System.Decimal. Let's say I pass in two decimals as parameters to calculate the sum or the product. Is there a way to do this in JINT without losing precisions?

If there is no support out-of-the-box from JINT, is there any work around?

Appreciate your answers!

tonyjy
  • 1,359
  • 3
  • 12
  • 24
  • First of all, what are you doing in JINT that you couldn't do in .NET? Couldn't you simply delegate all the calculations to .NET instead? Second, what precision do you *really* need? JavaScript can represent all integers up to 2^53 exactly, which is 15 digits of precision if you used a fixed scale. That's not enough to represent the U.S. national debt in cents, but it may be good enough for many other calculations. – Jeroen Mostert Jul 11 '17 at 11:22
  • @JeroenMostert I am trying to calculate text-based-expression in C# code. NCalc and Dynamic Linq can all do similar job. The syntax for NCalc and Dynamic Linq are not that clean comparing to JINT. JINT provides full support for ECMA 5.1 and it supports dynamic object. That's why I am very interested in JINT. The only concern is Jint converts System.Int or System.Decimal to System.Double. For certain calculation, Decimal is preferred. https://stackoverflow.com/questions/618535/difference-between-decimal-float-and-double-in-net. – tonyjy Jul 11 '17 at 12:32
  • 1
    Yes, I'm aware of how `Decimal` works. My point is that not all calculations where you would naturally use `Decimal` in C# actually *need* the full precision of `Decimal`. For instance, you can implement fixed-point arithmetic using integers. 100.345 * 23.21 = 2329.00745. JavaScript can calculate this exactly if you rewrite it as 100345 * 2321 and shifting the decimal point. For simple calculations (like financial ones), this is easy enough to do without pulling in a library. (Although [there are libraries](http://jsfromhell.com/classes/bignumber).) – Jeroen Mostert Jul 11 '17 at 14:29

0 Answers0