Arithmetic operation on exponential values

时间:2016-04-25 08:49:06

标签: javascript

Is there any limit(minimum value) for arithmetic(+, -) operation on exponential values? I need to perform arithmetic operation with exponential values, while performing addition on certain exponential values, the operation is not performed. Consider the following example,

  var a=1.797693134862314e+308,
      q =5e+291;
  q=a+q;
  if(a!= q)
  alert("Value has changed");

In this i have used the alert box for ensuring the operation is performed or not,since it is larger value. In the above mentioned values the addition operation is not performed, when the value for "q" variable is changed to "5e+292" the operation will be performed.

Here is the fiddle link:

Sample working

Sample not working

I need to know is there any minimum values for arithmetic(+,-) operation for exponential values. Kindly help me in resolving this issue.

Thanks,

Dharani.

1 个答案:

答案 0 :(得分:1)

Double can only represent numbers to a certain precision. In Java, the next number representable after 1.797693134862314e+308 can be found by:

double nextAfter = Math.nextAfter(1.797693134862314e+308, 1.0);

which is

1.7976931348623137E308

i.e. they only differ by 3 at the 17th significant figure.

In other words, if you try to add a number which only affects the (308-291+1)=18th significant figure in the larger operand, double simply can't represent the difference, so they appear to be the same.

Now, your question is (now) about Javascript, but Javascript floats are IEEE754 64 bit floating point numbers, just like Java doubles. As such, there is no difference in the precision which can be represented.