Float/Double arithmetic precision error

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

    Float/Double arithmetic precision error

    Hi all,
    I had problem regarding float/double arithmetic only with + and -
    operations, which gives inaccurate precisions. I would like to know how the
    arithmetic operations are internally handled by C# or they are hardware
    (processor) dependent. Basic addition operation errors, for ex:
    3.6 - 2.4 = 1.19999999999 or 1.20000000003
    There are the erroneous values I'm getting. I'm using C#.Net v1.1 Please
    reply me how these operations are handled internally.
    Thanks in Advance,
    Madan.


  • Eric Gunnerson [MS]

    #2
    Re: Float/Double arithmetic precision error

    Floating point representations are inherently imprecise for many numbers.
    This paper does a good job explaining it:



    --
    Eric Gunnerson

    Visit the C# product team at http://www.csharp.net
    Eric's blog is at http://blogs.gotdotnet.com/ericgu/

    This posting is provided "AS IS" with no warranties, and confers no rights.
    "Madan" <s011b043@jntu. ac.in> wrote in message
    news:ukg8FTQuDH A.1196@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Hi all,
    > I had problem regarding float/double arithmetic only with + and -
    > operations, which gives inaccurate precisions. I would like to know how[/color]
    the[color=blue]
    > arithmetic operations are internally handled by C# or they are hardware
    > (processor) dependent. Basic addition operation errors, for ex:
    > 3.6 - 2.4 = 1.19999999999 or 1.20000000003
    > There are the erroneous values I'm getting. I'm using C#.Net v1.1 Please
    > reply me how these operations are handled internally.
    > Thanks in Advance,
    > Madan.
    >
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Float/Double arithmetic precision error

      Madan <s011b043@jntu. ac.in> wrote:[color=blue]
      > I had problem regarding float/double arithmetic only with + and -
      > operations, which gives inaccurate precisions. I would like to know how the
      > arithmetic operations are internally handled by C# or they are hardware
      > (processor) dependent. Basic addition operation errors, for ex:
      > 3.6 - 2.4 = 1.19999999999 or 1.20000000003
      > There are the erroneous values I'm getting. I'm using C#.Net v1.1 Please
      > reply me how these operations are handled internally.[/color]

      See http://www.pobox.com/~skeet/csharp/floatingpoint.html

      --
      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

      • Randy A. Ynchausti

        #4
        Re: Float/Double arithmetic precision error

        Madan,
        [color=blue][color=green]
        > > I had problem regarding float/double arithmetic only with + and -
        > > operations, which gives inaccurate precisions.[/color][/color]

        It may be symantics, but to me the representation of floating-point
        precision numbers in a "binary" machine is a the problem, not necessarily
        the precision of the number, unless you want to specify exactly how many
        decimal places are significant for every floating point number and any
        calculation that involves floating point numbers.
        [color=blue][color=green]
        > > I would like to know how the
        > > arithmetic operations are internally handled by C# or they are hardware
        > > (processor) dependent.[/color][/color]

        The problems with floating-point number representation are not specific to a
        particular language. They are specific to representing floating-point
        number on binary computers. Essentially, you are representing a continuous
        number in a discrete environment. The effective approach is to break up the
        range of values that a floating-point number can have into tiny intervals
        (the precision the machine you are running the code on.).
        [color=blue][color=green]
        > > Basic addition operation errors, for ex:
        > > 3.6 - 2.4 = 1.19999999999 or 1.20000000003
        > > There are the erroneous values I'm getting.[/color][/color]

        Before declaring these erroneous, you should specify what precision you are
        looking for. They are correct to 10 decimal places. If you want higher
        precision, use a double. If you want exact precision, use the
        System.Decimal class and specifiy the number of decimal places.
        [color=blue][color=green]
        > > I'm using C#.Net v1.1 Please
        > > reply me how these operations are handled internally.[/color][/color]

        It is bad programming form to compare floating-point numbers and results
        using the traditional "==" operator. You should calculate the machine
        precision and then check to see if the difference of two floating-point
        numbers is smaller than some small value based on the machine precision.

        Hope that helps.

        Regards,

        Randy


        Comment

        Working...