Float/Double Substraction Bug ?

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

    #1

    Float/Double Substraction Bug ?

    Hello,

    I have the strange beeing :

    float f1 = 108.88F;
    float f2 = 111.11F;
    float f3 = f1 - f2;
    double d1 = 108.88;
    double d2 = 111.11;
    double d3 = d1 - d2;

    f3 contains -2.23000336
    d3 contains -2.2300000000000 04

    instead of -2.23.


    Am i missing something ?


    Regards,
    Cybertof.
  • Miha Markic

    #2
    Re: Float/Double Substraction Bug ?

    Hi cybertor,

    This is normal behavious of binary based floating point variables.
    They are only rounded to the value you want.
    You might try using decimal variables instead.

    --
    Miha Markic - RightHand .NET consulting & development
    miha at rthand com

    "cybertof" <cybertof2003no spam@gmx.net> wrote in message
    news:MPG.1a2f00 c2c899749989680 @msnews.microso ft.com...[color=blue]
    > Hello,
    >
    > I have the strange beeing :
    >
    > float f1 = 108.88F;
    > float f2 = 111.11F;
    > float f3 = f1 - f2;
    > double d1 = 108.88;
    > double d2 = 111.11;
    > double d3 = d1 - d2;
    >
    > f3 contains -2.23000336
    > d3 contains -2.2300000000000 04
    >
    > instead of -2.23.
    >
    >
    > Am i missing something ?
    >
    >
    > Regards,
    > Cybertof.[/color]


    Comment

    • cybertof

      #3
      Re: Float/Double Substraction Bug ?

      Hi Miha !

      How do you explain ?
      Does it mean that with single & double, you always get calculation
      errors ?

      When can you use binary based floating point variables if you cannot
      make correct calculations with them ?


      Regards,
      Cybertof.



      In article <O1cVNcFtDHA.37 44@TK2MSFTNGP11 .phx.gbl>, "Miha Markic" <miha
      at rthand com> says...[color=blue]
      > Hi cybertor,
      >
      > This is normal behavious of binary based floating point variables.
      > They are only rounded to the value you want.
      > You might try using decimal variables instead.
      >
      >[/color]

      Comment

      • Eric Gunnerson [MS]

        #4
        Re: Float/Double Substraction Bug ?

        This document may help explain it:

        http://portal.acm.org/citation.cfm?i...TOKEN=87893990

        --
        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.
        "cybertof" <cybertof2003no spam@gmx.net> wrote in message
        news:MPG.1a2f28 b75cc6e14398968 1@msnews.micros oft.com...[color=blue]
        > Hi Miha !
        >
        > How do you explain ?
        > Does it mean that with single & double, you always get calculation
        > errors ?
        >
        > When can you use binary based floating point variables if you cannot
        > make correct calculations with them ?
        >
        >
        > Regards,
        > Cybertof.
        >
        >
        >
        > In article <O1cVNcFtDHA.37 44@TK2MSFTNGP11 .phx.gbl>, "Miha Markic" <miha
        > at rthand com> says...[color=green]
        > > Hi cybertor,
        > >
        > > This is normal behavious of binary based floating point variables.
        > > They are only rounded to the value you want.
        > > You might try using decimal variables instead.
        > >
        > >[/color][/color]


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Float/Double Substraction Bug ?

          cybertof <cybertof2003no spam@gmx.net> wrote:[color=blue]
          > How do you explain ?
          > Does it mean that with single & double, you always get calculation
          > errors ?[/color]

          Often, yes - but not always.
          [color=blue]
          > When can you use binary based floating point variables if you cannot
          > make correct calculations with them ?[/color]

          In fact, the calculations may well be performing without any error, but
          with the initial values not being what you think they were.

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

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

          Working...