How decimals are calculated in javascript -



How decimals are calculated in javascript -

i have calculation in decimals in application using javascript result unexpected.

the result of below statement 1.707 [as expected]

var total=1.17+0.237+0.3;

also when add together numbers below, getting 0.7070000000000001 [unexpected]

var totalnew=0.17+0.237+0.3;

also, look (0.1+0.2===0.3) returns false[unexpected], (1.1+0.2===1.3) returns true[as expected] . why happening , workaround this.

floating point notoriously tricky. boils downwards there infinite amount of values between 2 real numbers, impossible represent them correctly in computer.

if want print number suggest:

total.tofixed();

which give 3 decimal places. , when want check if 2 floats same need this:

function nearlyequal(a, b, epsilon) { var absa = math.abs(a); var absb = math.abs(b); var diff = math.abs(a - b); var minnormal = 1.1754943508222875e-38; if (a == b) { // shortcut, handles infinities homecoming true; } else if (a == 0 || b == 0 || diff < minnormal) { // or b 0 or both extremely close // relative error less meaningful here homecoming diff < (epsilon * minnormal); } else { // utilize relative error homecoming diff / (absa + absb) < epsilon; } } nearlyequal(0.1+0.2, 0.3, 0.0001);

as suggested here how should floating point comparison?

javascript

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -