When the argument of trigonometric functions (sin cos tan) is sufficiently large (>= 1e19) in module, the output is wrong. In particular sin and cos output the argument instead of the function. cos(1e19) = 1e19
code example:
OPEN "cos.txt" FOR OUTPUT AS #1
Dim m as long
FOR m = 1 TO 100
PRINT #1, cos(10^(m)), cos(-10^(m)), sin(10^(m)),sin(-10^(m)), tan(10^(m)), tan(-10^(m))
next m
CLOSE #1
The functions break at 2^63. At this point, double-precision values have a granularity of 1024. Since sin and cos have a periodicity of ~6.28 (and tan a periodicity of half that), this renders the values pretty much meaningless anyway.
I'd suggest a way of calculating the sine/cosine of very large angles using recursion and the double angle formula, but in a quick experiment it seems the error tends to blow up, so it would become unreliable pretty quickly.
Am I right thinking this is the normal behaviour then? (i.e. we can't really fix this, without using floats with more precision than 64 bits)
It's part of the x87 specification. I couldn't find an "official" link in Google, but these two pages among others support it:
To fix it we'd have to reduce values modulo Pi/4*2^63 for a more "meaningful" result. If we care about accuracy, we'd have to implement long divison with a value of Pi up to about 1024+64 bits precision, otherwise really high values would degenerate into pseudorandom noise.
Another solution might be to, as the first page says, check the C2 flag and then e.g. change results to sin 0 | cos 0 | tan 0.
By the way, I added notes to the Sin/Cos/Tan wiki pages.
Last edit: Matthew 2013-10-05