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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bindul Bhowmik

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

    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.
  • Jon Skeet [C# MVP]

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

    Bindul Bhowmik <bindul_bhowmik @mindtree.com> wrote:[color=blue]
    > 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.[/color]

    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?

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Hans Kesting

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


      "Bindul Bhowmik" <bindul_bhowmik @mindtree.com> wrote in message
      news:0a8e01c3ae 8d$f8b9e520$a40 1280a@phx.gbl.. .[color=blue]
      > 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.[/color]

      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


      Comment

      • Ted Miller

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

        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.

        "Hans Kesting" <news.2.hansdk@ spamgourmet.com > wrote in message
        news:OhjjLcqrDH A.4056@TK2MSFTN GP11.phx.gbl...[color=blue]
        >
        > "Bindul Bhowmik" <bindul_bhowmik @mindtree.com> wrote in message
        > news:0a8e01c3ae 8d$f8b9e520$a40 1280a@phx.gbl.. .[color=green]
        > > 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.[/color]
        >
        > 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
        >
        >[/color]


        Comment

        • Hans Kesting

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


          "Ted Miller" <ted@nwlink.com > wrote in message
          news:vro0kmt9rg f522@corp.super news.com...[color=blue]
          > Well, since 90/180 = 1/2 which is precisely 2^-1, isn't this value[/color]
          precisely[color=blue]
          > representable in an x86 float or double?
          >[/color]

          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


          Comment

          • Jon Skeet [C# MVP]

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

            Hans Kesting <news.2.hansdk@ spamgourmet.com > wrote:[color=blue]
            >
            > "Ted Miller" <ted@nwlink.com > wrote in message
            > news:vro0kmt9rg f522@corp.super news.com...[color=green]
            > > Well, since 90/180 = 1/2 which is precisely 2^-1, isn't this value[/color]
            > precisely[color=green]
            > > representable in an x86 float or double?
            > >[/color]
            >
            > 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.[/color]

            I suspect Ted was querying the word "about" in your previous post.

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Hans Kesting

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

              .... eh, you could be right (now that I re-read his post), in which case he
              was right. (right? :-)

              Hans Kesting


              Comment

              • Ted Miller

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

                I understand that, but one poster said essentially that 90.0 / 180.0 is
                "approximat ely" .5.

                --

                "Hans Kesting" <news.2.hansdk@ spamgourmet.com > wrote in message
                news:%230Sv8H0r DHA.2028@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                >
                > "Ted Miller" <ted@nwlink.com > wrote in message
                > news:vro0kmt9rg f522@corp.super news.com...[color=green]
                > > Well, since 90/180 = 1/2 which is precisely 2^-1, isn't this value[/color]
                > precisely[color=green]
                > > representable in an x86 float or double?
                > >[/color]
                >
                > 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
                >
                >[/color]


                Comment

                Working...