Hardware: 32-bit Intel P4 cpu
The following examples below show what I consider
to be ‘mathematical’ errors (to distinguish from
‘arithmetic’ errors) for sin, cos, and tan.
The problem is that for cos(x)/sin(x), from the
mathematical perspective, and their requirements,
whatever value of x that causes sin/[cos] = -1/+1
REQUIRES cos/[sin] of x MUST BE ZERO - cos/[sin]=0
Ruby 1.9.1p243 output below
1)onedegree =2*PI/360 =>0.0241660973353061
2)cos onedgree =>0.999847695156391
3)sin onedgree =>0.0241637452361323
4)cos onedegree/1e05 =>0.999999999999971
5)sin onedegree/1e05 =>2.41660973353059e-07
6)cos onedegree/1e06 =>1.0
7)sin onedegree/1e06 =>2.41660973353061e-08
8)cos onedegree/1e07 =>1.0
9)sin onedegree/1e07 =>2.41660973353061e-09
10)cos 0 =>1.0
11)sin 0 =>0.0
12)cos PI/2 =>6.123023176911189e-17
13)sin PI/2 =>1.0
14)cos PI =>-1.0
15)sin PI => 1.22460635382238e-16
16)cos 3PI/2 =>-1.83690953073357e-16
17)sin 3PI/2 =>-1.0
18)cos 2PI => 1.0
19)sin 2PI =>-2.44921970764475e-16
Examples 4)-9) show that at some point for cos a
decision was made to fix (roundoff.truncation,?)
cos(of a very smal number) = 1.0, however,
the implementation doesn’t make that (necessary)
decision to set the sin of that angle equal to 0.
In fact, I gave up after sin(onedegree/1e100) to
see if the answer would ever give the answer ‘0’.
10)-19) show the errors for cos/sin for angles
that correspond to the x/y axis as the angle traverses
the unit circle ccw from 0 - 2*PI. These errors also
affect the tan functions in the same manner.
It would greatly enhance the use of Ruby in scientific
and engineering domains if Ruby implemented the trig
functions so that MATH done with the trig functions
produce exact results for all angles on the axis.
The values in 12), 15), 16) and 19) are so small they
represent |delta angles| that would hardly be encountered
to represent real physical events, even in astrophysics,
or quantum mechanics. And if someone REALLY needed to calculate trig
values for angles that small they can
use Mathmematica, etc, or better SAGE (OSS symbolic
math program, done in Python: www.sagemath.org/).
These fixes should be fairly simple to implement by
putting the relevant checks on the inputs to the trig
functions to set the outputs to ‘0’ for the values of
the angles = n*PI/2 for n = any integer, when needed.
It may be as simple as representing cos(x) = sin(x+PI/2)
or vice versa. Or even simpler, check the output of the
current functions and set to zero if the absolute value
is smaller than some real-life epsilon (1e-10 ?)
Just make the defaults give the correct results.
Yes, it’s a little more work in the core.
Yes, it may make for slower functions, if anyone cares.
But…being able to do ACCURATE MATH creates benefits
that far supercede any hassles to write the core to do it.
I can see Ruby’s use in academia, science, and engineering being more
accepting if it reduces unnecessary quirks with trig functions, so
people get what they expect when doing basic operations. It may even
impress people to see Ruby’s concern for detail placed in such high
Finally, this violates the POLS for trigs, because, after
all, the whole point is to make the language make me happy, not the
other way around.