Javascript floating point








To define a floating-point value, you must include a decimal point and at least one number after the decimal point.

An integer is required before a decimal point, but it is recommended.

Here are some examples:


var floatNum1 = 1.1;
console.log(floatNum1);
var floatNum2 = 0.1;
console.log(floatNum2);
var floatNum3 = .1;     //valid, but not recommended
console.log(floatNum3);

The code above generates the following result.

When there is no digit after the decimal point, the number becomes an integer.

If the number being represented is a whole number (such as 1.0), it will be converted into an integer, as in this example:

var floatNum1 = 1.;     //missing digit after decimal - interpreted as integer 1
console.log(floatNum1);
var floatNum2 = 10.0;   //whole number - interpreted as integer 10
console.log(floatNum2);




Scientific notation

Floating-point values can be represented using e-notation.

E-notation indicates a number that should be multiplied by 10 raised to a given power.

The format of e-notation in Javascript is to have a number, integer or floating-point, followed by e or E, than by the power of 10 to multiply by.

Consider the following:


var floatNum = 3.125e7;    //equal to 31250000
console.log(floatNum);

In this example, floatNum is equal to 31,250,000. The notation essentially says, "Take 3.125 and multiply it by 10 7."

The code above generates the following result.

E-notation can also be used to represent very small numbers, such as 0.00000000000000003, which can be written more succinctly as 3e-17.

By default, Javascript converts any floating-point value with at least six zeros after the decimal point into e-notation.





float point rounding error

Floating-point values are not as accurate as whole numbers in arithmetic computations.

For instance, adding 0.1 and 0.2 yields 0.30000000000000004 instead of 0.3.

These small rounding errors make it difficult to test for specific floating-point values.


var a = 0.1;
var b = 0.2;
console.log(a + b);/*from   w w w.  j ava 2  s.  c o m*/
if (a + b == 0.3){        
    console.log("You got 0.3.");
}

a = 0.05;
b = 0.25;
console.log(a + b);
if (a + b == 0.3){       
    console.log("You got 0.3.");
}

The code above generates the following result.

You should avoid testing for specific floating-point values.

The rounding errors are a side effect of IEEE-754-based numbers.

float point value range

Not all numbers in the world can be represented in Javascript. The value range are identified by four special values in Javascript.

  • Number.MIN_VALUE represents the smallest value in JavaScript.
  • Number.MAX_VALUE stores the largest number.
  • Number.NEGATIVE_INFINITY is the negative infinity.
  • Number.POSITIVE_INFINITY is the positive infinity.

var result = Number.MAX_VALUE + Number.MAX_VALUE; 
console.log(result); //from w w w . j  a va 2 s . co  m

console.log(Number.MAX_VALUE); 
console.log(Number.MIN_VALUE); 
console.log(Number.NEGATIVE_INFINITY); 
console.log(Number.POSITIVE_INFINITY); 


if((Math.sqrt(-2)) != Number.NEGATIVE_INFINITY_){
    console.log("This is not equal to NEGATIVE_INFINITY");
}
else{
    console.log("This is equal to NEGATIVE_INFINITY");
}

if((Math.exp(999)) <= Number.POSITIVE_INFINITY_){
   console.log("This is less than positive infinity");
}
else{
   console.log("This is greater than POSITIVE_INFINITY");
}

if((99999*99999) <= Number.MAX_VALUE){
   console.log("greater than the maximum value");
}
if((0.0000000001) >= Number.MIN_VALUE){
   console.log("The number is not the minimum value");
}

The code above generates the following result.