Re: Problems to calculate sin
Steve70 wrote:
And why Turbo Pascal calculate sin(30*PI/180)=0.5?
Does it? Does it really?
You can make the Java result of Math.sin( 30.0 * Math.PI / 180.0 ) look like
it returns 0.5, too. The significant thing is that the result of that
calculation has guaranteed results, precision and accuracy in Java. Your
Pascal or C implementations will vary in their internal precision, accuracy
and output.
As pointed out quite frequently in these newsgroups, floating point
expressions in a binary device will generally be inaccurate. The science of
numerical analysis allows limits on that inaccuracy, and yields algorithms to
minimize it. Programmers need to have some knowledge of that science in order
to understand, and more importantly, control what their code does.
Note the definition of Java's Math.PI:
<http://java.sun.com/javase/6/docs/api/java/lang/Math.html#PI>
The double value that is closer than any other to pi,
the ratio of the circumference of a circle to its diameter.
If PI cannot be exactly represented, then it is not reasonable to expect that
the result of a calculation on that approximation involving division by 180.0,
whose result generally will also be inaccurate, much less the result of an
approximation of a transcendental function of that calculation, will be
exactly representable in the binary floating-point format, much less exactly
accurate.
--
Lew