0

I write numerical routines for students of science and engineering (although they are freely available for use by anybody else as well) and am wondering how to properly use machine constants in a JavaScript program, or if they are even applicable.

For example, say I am writing a program in C++ that numerically computes the roots of the following equation:

exp(-0.7x) + sin(3x) - 1.2x + 0.3546 = 0

A root-finding routine should be able to compute roots to within the machine epsilon. In C++, this value is specified by the language: DBL_EPSILON. C++ also specifies the smallest and largest values that can be held by a float or double variable.

However, how does this convert to JavaScript? Since a Javascript program runs in a web browser, and I don't know what kind of computer will run the program, and JavaScript does not have corresponding predefined values for these quantities, how can I implement my own version of these constants so that my programs compute results to as much accuracy as allowed on the computer running the web browser?

My first draft is to simply copy over the literal constants from C++:

FLT_MIN: 1.17549435082229e-038
FLT_MAX: 3.40282346638529e+038
DBL_EPSILON: 2.2204460492503131e-16

I am also willing to write small code blocks that could compute these values for each machine on which the program is run. That way, a supercomputer might compute results to a higher accuracy than an old, low-level, PC.

BUT, I don't know if such a routine would actually reach the computer, in which case, I would be wasting my time.

Anybody here know how to compute and use (in Javascript) values that correspond to machine constants in a compiled language?

Is it worth my time to write small programs in Javascript that compute DBL_EPSILON, FLT_MIN, FLT_MIN, etc. for use in numerical routines? Or am I better off simply assigning literal constants that come straight from C++ on a standard Windows PC?

  • 3
    The issue is actually the opposite of what you state. C++ is the language where you can't depend on particular values for those metrics because floating point implementations vary while JavaScript mandates 64-bit floating point values based on IEEE 754. – Steven Burnap Jun 6 '14 at 23:49
  • Hi, Steve. Thanks for replying. I can't seem to find these values for JavaScript, whereas I can find these values for C++ easily. Can you recommend a JavaScript resource that discusses these numbers? And provides numeric values? – DavidB2013 Jun 7 '14 at 19:02
1

The issue is actually the opposite of what you state. C++ is the language where you can't depend on particular values for those metrics because floating point implementations vary while JavaScript mandates 64-bit floating point values based on IEEE 754.

Very few people do this sort of work in JavaScript, so I doubt you'll find anything that helps you that's directly tied to the language, but metrics for 64-bit floating point using IEEE 754 can be found on wikipedia.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.