Velocity Reviews > Java > Problems to calculate sin

# Problems to calculate sin

Steve70
Guest
Posts: n/a

 03-04-2008
I must create a program that use trigonometry function.
I know sin(30)=0.5 but when I use Math.sin() I can't get it

Math.sin(30*Math.PI/180)=0.49999999999999994

What's the problem?

Thank you

Stefano Buscherini

bugbear
Guest
Posts: n/a

 03-04-2008
Steve70 wrote:
> I must create a program that use trigonometry function.
> I know sin(30)=0.5 but when I use Math.sin() I can't get it
>
> Math.sin(30*Math.PI/180)=0.49999999999999994

That *is* 0.5, at least to rather good tolerance.

BugBear

Sanny
Guest
Posts: n/a

 03-04-2008
On Mar 4, 3:40*pm, Steve70 <(E-Mail Removed)> wrote:
> I must create a program that use trigonometry function.
> I know sin(30)=0.5 but when I use Math.sin() I can't get it
>
> Math.sin(30*Math.PI/180)=0.49999999999999994
>
> What's the problem?
>
> Thank you
>
> Stefano Buscherini

Math.sin uses Log tables to calculate value of Sin So they are
approximate to 0.0000000000001 Value.

You can use this value in your Calculations with error of
0.0000000000001 Thats not a big difference unless you are using it
for Rocket/ Missile Launching Program. Then you may create a function
to calculate Sin value yourself.

I suppose you know how to create Sin() using Mathematical Formulla.
Give me \$15 and I will give you the Formula.

Bye
Sanny

Alex Mizrahi
Guest
Posts: n/a

 03-04-2008
S> I must create a program that use trigonometry function.
S> I know sin(30)=0.5 but when I use Math.sin() I can't get it

S> Math.sin(30*Math.PI/180)=0.49999999999999994

S> What's the problem?

you're using floating point numbers. their precision is quite limited, and
they cannot represent all numbers exactly. (it's not possible with fixed
amount of bits).
so, you should not depend on exact results. use rounding when doing output.
expect some degree of error when doing comparison.

using floating point numbers correctly (so you have minimal errors) might be
quite complex thing. for introduction, read
http://en.wikipedia.org/wiki/Floating_point

Roedy Green
Guest
Posts: n/a

 03-04-2008
On Tue, 4 Mar 2008 02:40:23 -0800 (PST), Steve70 <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>I must create a program that use trigonometry function.
>I know sin(30)=0.5 but when I use Math.sin() I can't get it
>
>Math.sin(30*Math.PI/180)=0.49999999999999994

see http://mindprod.com/jgloss/floatingpoint.html
http://mindprod.com/jgloss/trigonometry.html

If you asked a carpenter for a table .5 meters long and it was
0.49999999999999994, how would you notice?
--

The Java Glossary
http://mindprod.com

Lew
Guest
Posts: n/a

 03-04-2008
Sanny wrote:
> I suppose you know how to create Sin() using Mathematical Formulla.
> Give me \$15 and I will give you the Formula.

Come on, Sanny, do you really think anyone is going to give you cash for that
kind of thing?

"sin", lower-case "m" in "mathematical", "formula" is spelled with one "l"
and, in that context, lower-case "f". No charge for the information.

Are you this careful with the expression of your formulas, too?

--
Lew

Thomas.a.mcglynn@nasa.gov
Guest
Posts: n/a

 03-04-2008
On Mar 4, 5:40 am, Steve70 <(E-Mail Removed)> wrote:
> I must create a program that use trigonometry function.
> I know sin(30)=0.5 but when I use Math.sin() I can't get it
>
> Math.sin(30*Math.PI/180)=0.49999999999999994
>
> What's the problem?
>
> Thank you
>
> Stefano Buscherini

A few of the other posters have commented that you need to understand
the limits of floating point arithmetic. Note that there are at least
three ways that comes into play in this problem.

Mathematically we know
sin(pi/6) = 1/2
exactly. However Math.PI is an approximation of the value of PI so
30*Math.PI/180 is an approximation of pi/6. So when we take the sine
we are taking the sine of a number that's a little different from the
number we really wanted. That's one source of error. You can see
this most easily by looking at the value
of
Math.sin(Math.PI)
It is not 0 but a value of about 10^-16. This is not an error in the
computation of the sine, it represents the difference between Java's
approximation to pi and the true value. This source of error is
sometimes a little surprising to users.

The second source of error is in the arithmetic within the
parentheses. Anytime you operate on floating point numbers, and
especially if you are dividing, you are likely to get an answer that
is not exact. For division this is easy to see. You cannot write
10./3 in any finite decimal representation. While a computer users
binary rather than decimal, the same issue arises. If we combine this
with the first error it's possible that there is a number that Java
could represent that is closer to the value pi/6 than the value you
will actually get using 60*Math.PI/180.

The last source is in the computation of the sine itself. Java
permits (but does not require except in strictmath) mathematical
functions to have very small errors in the calculations. If the
correct answer is x, the result of the computation can be either x, or
the smallest number the computer can distinguish from x that is larger
than x, or the largest number the computer can distinguish from x that
is smaller than x. In practice this means that the answer is within
about 10^-16 x of the true value.

Regards,
Tom McGlynn

Patricia Shanahan
Guest
Posts: n/a

 03-04-2008
Sanny wrote:
> On Mar 4, 3:40 pm, Steve70 <(E-Mail Removed)> wrote:
>> I must create a program that use trigonometry function.
>> I know sin(30)=0.5 but when I use Math.sin() I can't get it
>>
>> Math.sin(30*Math.PI/180)=0.49999999999999994
>>
>> What's the problem?
>>
>> Thank you
>>
>> Stefano Buscherini

>
> Math.sin uses Log tables to calculate value of Sin So they are
> approximate to 0.0000000000001 Value.

I'm curious. Why log tables?

I don't know how Math.sin is implemented, and don't even assume it is
implemented the same way in all JVMs, but if I had to guess I would have
expected some sort of truncated Taylor series.

Patricia

Sanny
Guest
Posts: n/a

 03-04-2008
On Mar 4, 6:29*pm, Patricia Shanahan <(E-Mail Removed)> wrote:
> Sanny wrote:
> > On Mar 4, 3:40 pm, Steve70 <(E-Mail Removed)> wrote:
> >> I must create a program that use trigonometry function.
> >> I know sin(30)=0.5 but when I use Math.sin() I can't get it

>
> >> Math.sin(30*Math.PI/180)=0.49999999999999994

>
> >> What's the problem?

>
> >> Thank you

>
> >> Stefano Buscherini

>
> > Math.sin uses Log tables to calculate value of Sin So they are
> > approximate to 0.0000000000001 Value.

>
> I'm curious. Why log tables?
>
> I don't know how Math.sin is implemented, and don't even assume it is
> implemented the same way in all JVMs, but if I had to guess I would have
> expected some sort of truncated Taylor series.
>
> Patricia

Using Tables are faster than using a formula to compute a value.

By log table I just mean a Table for Sine. Log tables are more
familiar than Log Tables.

Bye
Sanny

bugbear
Guest
Posts: n/a

 03-04-2008
Sanny wrote:
> On Mar 4, 6:29 pm, Patricia Shanahan <(E-Mail Removed)> wrote:
>> Sanny wrote:
>>> On Mar 4, 3:40 pm, Steve70 <(E-Mail Removed)> wrote:
>>>> I must create a program that use trigonometry function.
>>>> I know sin(30)=0.5 but when I use Math.sin() I can't get it
>>>> Math.sin(30*Math.PI/180)=0.49999999999999994
>>>> What's the problem?
>>>> Thank you
>>>> Stefano Buscherini
>>> Math.sin uses Log tables to calculate value of Sin So they are
>>> approximate to 0.0000000000001 Value.

>> I'm curious. Why log tables?
>>
>> I don't know how Math.sin is implemented, and don't even assume it is
>> implemented the same way in all JVMs, but if I had to guess I would have
>> expected some sort of truncated Taylor series.
>>
>> Patricia

>
> Using Tables are faster than using a formula to compute a value.
>
> By log table I just mean a Table for Sine.

(chuckle)

BugBear