I am presently translating some code from C++ to Javascript and it involves quite a few floating point calculations. The program uses several built-in machine constants to help it quantify and limit computer round-off error. For example, it includes the constants DBL_MIN and DBL_EPSILON from the <cfloat> library within C++.

I am now wondering: does Javascript offer these constants as built-in values too? I suppose I could calculate them myself, by brute force, but if these values are already available, it would help me keep the Javascript version shorter.

Does Javascript provide built-in, comparable values for DBL_MIN and DBL_EPSILON? (If so, how do I access them?)



Recommended Answers

All 2 Replies

You can always peek into the header files of the compiler you are using to find out the value of those in-built constants and put the same in a JS file and include that file.

Something like:

// Vars.js
var DB_EPSILON = 0.00000000123
var DB_MIN = 0.125
  <script src="Vars.js"></script>
<!-- code -->

If you want to know the actual minimum value of the smallest value the current running JavaScript can actually use, it will tell you.

Use: Number.MIN_VALUE JS provides the following constants: Number.MIN_VALUE :
The smallest positive value which can be stored. It is close to 5E-324 Number.MAX_VALUE :
The largest positive value which can be stored. It is close to 1.79E+308 Number.NEGATIVE_INFINITY :
Returns the internal representation for negative infinity Number.POSITIVE_INFINITY :
Returns the internal representation for positive infinity Math.E :
Returns the natural base. Math.LN10 :
Returns the natural log of 10. Math.LN2 :
Returns the natural log of 2. Math.LOG10E :
Returns the common log of the natural base. Math.LOG2E :
Returns the binary-base log of the natural base. Math.PI :
Returns the value of pi. Math.SQRT1_2 :
Returns the square root of one half. Math.SQRT2 :
Returns the sqiare root of 2.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.