Confusion over unexpected Math.cos behavior in Javascript -
this question has answer here:
- is floating point math broken? 20 answers
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
Post a Comment