Confusion over unexpected Math.cos behavior in Javascript -


this question has answer here:

the cosine of 90 degrees 0.

i understand javascript's math.cos takes radians, i've tried:

math.cos(90 * math.pi / 180) 

why yield 6.123233995736766e-17 instead of 0?

6.123233995736766e-17 0. it's 0.00000000000000006123233995736766. kind of minor error normal when working ieee floating point numbers.

the solution never compare numbers exactly, compare if within range expect. eg.

var result = math.cos(90 * math.pi / 180); if ( math.abs( result - 0 ) < 1e-16 ) {     // test passed, result 0 } 

the - 0 nothing, more - x how compare x. trying compare 0, used - 0, leave off , same result.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -