Using Math.Cos function in C# for value 90 degree.

  • Thread starter Thread starter Bindul Bhowmik
  • Start date Start date
B

Bindul Bhowmik

I am in a project and have to find out the value of cos of
an angle. The user will input the value of the angle only
in degrees. I am using the following expression to get the
radian equivalent of the input :
(Input Value / 180) * Math.PI

To find the Cos I am using the Function Math.Cos. But the
problem comes when I get an input value of 90 degrees, and
the cos dosent come out to be 0.

If anyone has worked on a similar problem, or has any idea
how to get around this problem, please reply.
 
Bindul Bhowmik said:
I am in a project and have to find out the value of cos of
an angle. The user will input the value of the angle only
in degrees. I am using the following expression to get the
radian equivalent of the input :
(Input Value / 180) * Math.PI

To find the Cos I am using the Function Math.Cos. But the
problem comes when I get an input value of 90 degrees, and
the cos dosent come out to be 0.

If anyone has worked on a similar problem, or has any idea
how to get around this problem, please reply.

On my box it comes out to a value very, very close to 0 - namely about
6x10^-17.

The problem is that pi/2 isn't exactly representable (in either decimal
or binary). You run into the same problems as you do for any other
floating point number. See
http://www.pobox.com/~skeet/csharp/floatingpoint.html for more details
of these.

Is something being 0 to 16 significant digits really not good enough?
 
Bindul Bhowmik said:
I am in a project and have to find out the value of cos of
an angle. The user will input the value of the angle only
in degrees. I am using the following expression to get the
radian equivalent of the input :
(Input Value / 180) * Math.PI

To find the Cos I am using the Function Math.Cos. But the
problem comes when I get an input value of 90 degrees, and
the cos dosent come out to be 0.

If anyone has worked on a similar problem, or has any idea
how to get around this problem, please reply.

Is your "input value" an int? Then you will get unexpected results!
90/180 == 0

you could use
90/180.0 which will be (about) 0.5

Hans Kesting
 
Well, since 90/180 = 1/2 which is precisely 2^-1, isn't this value precisely
representable in an x86 float or double?

Just asking.
 
Ted Miller said:
Well, since 90/180 = 1/2 which is precisely 2^-1, isn't this value precisely
representable in an x86 float or double?

The problem is that it is: _int_ 90 divided by _int_ 180, which will lead
to an _int_ result of 0.
Change one of the operands to double (by using 180.0 for example) and you
get a double result.

Hans Kesting
 
Hans Kesting said:
The problem is that it is: _int_ 90 divided by _int_ 180, which will lead
to an _int_ result of 0.
Change one of the operands to double (by using 180.0 for example) and you
get a double result.

I suspect Ted was querying the word "about" in your previous post.
 
.... eh, you could be right (now that I re-read his post), in which case he
was right. (right? :-)

Hans Kesting
 
I understand that, but one poster said essentially that 90.0 / 180.0 is
"approximately" .5.

--
 
Back
Top