In a simple geometric program written in Javascript and Canvas, when I set the angle to 270 Β° (1 & frac12; & pi;), I expected Math.cos (& theta;) to reach zero. The vector is right down from the center, there is no distance on the carted grid. Instead, I get the following:
demo_angle = 270 ang = demo_angle * Math.PI / 180 x = Math.cos(ang) console.log(x) > -1.836909530733566e-16
To view the output of math functions, view the console. The source code is displayed (in coffeescript) one level up at the URL.
I needed to define in my code, "Any number whose absolute value is less than 1e-15 should be considered zero", but this is really unsatisfactory. Of course, when I try to do the math with a lower x value, especially since I try to use x as the denominator in calculating the slope, and then doing quadratic manipulations, I end up coming up with numbers that exceed Number.MAX_VALUE (or Number.MIN_VALUE )
I know floating point math, at the assembler level, a bit of a dark art, but results like this seem weirder than acceptable. Any hints on what I should do?
source share