???

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

    #1

    ???

    Hi all,

    does any body know what going on?

    float f1 = 0.1f;

    float f2 = 0.2f;

    f2 - f1 = 0.100000098

    if I check the number like this:

    if( f2 = = 0.1f )
    ...............

    NEVER WORKED !!!


  • Mr.Tickle

    #2
    Re: ???

    u cant do exact testing on floats can u ?

    "Kate Luu" <cql90@yahoo.co m> wrote in message
    news:eUS8SJHfDH A.1760@TK2MSFTN GP09.phx.gbl...[color=blue]
    > Hi all,
    >
    > does any body know what going on?
    >
    > float f1 = 0.1f;
    >
    > float f2 = 0.2f;
    >
    > f2 - f1 = 0.100000098
    >
    > if I check the number like this:
    >
    > if( f2 = = 0.1f )
    > ..............
    >
    > NEVER WORKED !!!
    >
    >[/color]


    Comment

    • Kate Luu

      #3
      Re: ???

      When we use the computer, we need the precisely, absolute accuracy, if the
      computer wasn't precise as it is, who will use the computer for
      calculation???
      don't tell me 2+2=5 is OK for you.

      so 0.2 - 0.1 should be = 0.1, not 0.1000000098;

      For scientific, we need the result must be very accurate.

      0.2 - 0.1 is nothing. Do you ever work for government???



      "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
      news:e$9G5MHfDH A.2356@TK2MSFTN GP12.phx.gbl...[color=blue]
      > u cant do exact testing on floats can u ?
      >
      > "Kate Luu" <cql90@yahoo.co m> wrote in message
      > news:eUS8SJHfDH A.1760@TK2MSFTN GP09.phx.gbl...[color=green]
      > > Hi all,
      > >
      > > does any body know what going on?
      > >
      > > float f1 = 0.1f;
      > >
      > > float f2 = 0.2f;
      > >
      > > f2 - f1 = 0.100000098
      > >
      > > if I check the number like this:
      > >
      > > if( f2 = = 0.1f )
      > > ..............
      > >
      > > NEVER WORKED !!!
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Tim Mulholland

        #4
        Re: ???

        If i'm not mistaken, you can't get .1 exactly with floating point numbers
        because of the way they are implemented, right?

        <quote from http://grouper.ieee.or g/groups/754/faq.html>
        Binary floating-point numbers consist of signed integers multiplied by
        powers of two. When fractional, you can also consider them as integers
        divided by powers of two. The decimal number 0.1, or 1/10, is not an integer
        over a power of two. The 854 standard encompasses decimal arithmetic, but
        there is little hardware support outside of desktop calculators.

        </quote>

        "Kate Luu" <cql90@yahoo.co m> wrote in message
        news:uP2nZYHfDH A.3248@tk2msftn gp13.phx.gbl...[color=blue]
        > When we use the computer, we need the precisely, absolute accuracy, if the
        > computer wasn't precise as it is, who will use the computer for
        > calculation???
        > don't tell me 2+2=5 is OK for you.
        >
        > so 0.2 - 0.1 should be = 0.1, not 0.1000000098;
        >
        > For scientific, we need the result must be very accurate.
        >
        > 0.2 - 0.1 is nothing. Do you ever work for government???
        >
        >
        >
        > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
        > news:e$9G5MHfDH A.2356@TK2MSFTN GP12.phx.gbl...[color=green]
        > > u cant do exact testing on floats can u ?
        > >
        > > "Kate Luu" <cql90@yahoo.co m> wrote in message
        > > news:eUS8SJHfDH A.1760@TK2MSFTN GP09.phx.gbl...[color=darkred]
        > > > Hi all,
        > > >
        > > > does any body know what going on?
        > > >
        > > > float f1 = 0.1f;
        > > >
        > > > float f2 = 0.2f;
        > > >
        > > > f2 - f1 = 0.100000098
        > > >
        > > > if I check the number like this:
        > > >
        > > > if( f2 = = 0.1f )
        > > > ..............
        > > >
        > > > NEVER WORKED !!!
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Kate Luu

          #5
          Re: ???

          Thanks you so much, Tickle and Time. I'm very appreciated for you all help.
          Have a nice day...

          Kate Luu

          "Tim Mulholland" <tim@eyerespons e.com> wrote in message
          news:urp07iHfDH A.2364@TK2MSFTN GP09.phx.gbl...[color=blue]
          > If i'm not mistaken, you can't get .1 exactly with floating point numbers
          > because of the way they are implemented, right?
          >
          > <quote from http://grouper.ieee.or g/groups/754/faq.html>
          > Binary floating-point numbers consist of signed integers multiplied by
          > powers of two. When fractional, you can also consider them as integers
          > divided by powers of two. The decimal number 0.1, or 1/10, is not an[/color]
          integer[color=blue]
          > over a power of two. The 854 standard encompasses decimal arithmetic, but
          > there is little hardware support outside of desktop calculators.
          >
          > </quote>
          >
          > "Kate Luu" <cql90@yahoo.co m> wrote in message
          > news:uP2nZYHfDH A.3248@tk2msftn gp13.phx.gbl...[color=green]
          > > When we use the computer, we need the precisely, absolute accuracy, if[/color][/color]
          the[color=blue][color=green]
          > > computer wasn't precise as it is, who will use the computer for
          > > calculation???
          > > don't tell me 2+2=5 is OK for you.
          > >
          > > so 0.2 - 0.1 should be = 0.1, not 0.1000000098;
          > >
          > > For scientific, we need the result must be very accurate.
          > >
          > > 0.2 - 0.1 is nothing. Do you ever work for government???
          > >
          > >
          > >
          > > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
          > > news:e$9G5MHfDH A.2356@TK2MSFTN GP12.phx.gbl...[color=darkred]
          > > > u cant do exact testing on floats can u ?
          > > >
          > > > "Kate Luu" <cql90@yahoo.co m> wrote in message
          > > > news:eUS8SJHfDH A.1760@TK2MSFTN GP09.phx.gbl...
          > > > > Hi all,
          > > > >
          > > > > does any body know what going on?
          > > > >
          > > > > float f1 = 0.1f;
          > > > >
          > > > > float f2 = 0.2f;
          > > > >
          > > > > f2 - f1 = 0.100000098
          > > > >
          > > > > if I check the number like this:
          > > > >
          > > > > if( f2 = = 0.1f )
          > > > > ..............
          > > > >
          > > > > NEVER WORKED !!!
          > > > >
          > > > >
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Morten Wennevik

            #6
            Re: ???

            If only that were true

            float isn't very precise, and banks use decimal numbers that are many times
            more precise than float, but they aren't very precise either.

            I think someone mentioned that rounding 5.555 didn't lead to 5.56 but 5.55,
            because the nearest number to 5.555 was in fact
            5.5549999999999 99214916394143 or something. So you can't use exact
            comparison for floats.

            Try turning it into an int by multiplying the numbers with 10000 or
            something and check if (int)(f2*10000) == 1000

            --
            Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

            Comment

            • Nicole Calinoiu

              #7
              Re: ???

              Or just use the Decimal type if appropriate...

              "Morten Wennevik" <mortenwennevik @hotmail.com> wrote in message
              news:oprvltw2h3 ge0n9a@localhos t...[color=blue]
              > If only that were true
              >
              > float isn't very precise, and banks use decimal numbers that are many[/color]
              times[color=blue]
              > more precise than float, but they aren't very precise either.
              >
              > I think someone mentioned that rounding 5.555 didn't lead to 5.56 but[/color]
              5.55,[color=blue]
              > because the nearest number to 5.555 was in fact
              > 5.5549999999999 99214916394143 or something. So you can't use exact
              > comparison for floats.
              >
              > Try turning it into an int by multiplying the numbers with 10000 or
              > something and check if (int)(f2*10000) == 1000
              >
              > --
              > Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/[/color]


              Comment

              • Kate Luu

                #8
                Re: ???

                Thanks you all very much. Have a wonderful day...

                Kate

                "Nicole Calinoiu" <nicolec@somewh ere.net> wrote in message
                news:#E6mb6HfDH A.3616@TK2MSFTN GP11.phx.gbl...[color=blue]
                > Or just use the Decimal type if appropriate...
                >
                > "Morten Wennevik" <mortenwennevik @hotmail.com> wrote in message
                > news:oprvltw2h3 ge0n9a@localhos t...[color=green]
                > > If only that were true
                > >
                > > float isn't very precise, and banks use decimal numbers that are many[/color]
                > times[color=green]
                > > more precise than float, but they aren't very precise either.
                > >
                > > I think someone mentioned that rounding 5.555 didn't lead to 5.56 but[/color]
                > 5.55,[color=green]
                > > because the nearest number to 5.555 was in fact
                > > 5.5549999999999 99214916394143 or something. So you can't use exact
                > > comparison for floats.
                > >
                > > Try turning it into an int by multiplying the numbers with 10000 or
                > > something and check if (int)(f2*10000) == 1000
                > >
                > > --
                > > Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/[/color]
                >
                >[/color]


                Comment

                • Jon Skeet

                  #9
                  Re: ???

                  Kate Luu <cql90@yahoo.co m> wrote:[color=blue]
                  > does any body know what going on?[/color]

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

                  • Ravichandran J.V.

                    #10
                    Re: ???



                    with regards,


                    J.V.Ravichandra n
                    - http://www.geocities.com/
                    jvravichandran
                    - http://www.411asp.net/func/search?
                    qry=Ravichandra n+J.V.&cob=aspn etpro
                    - http://www.southasianoutlook.com
                    - http://www.MSDNAA.Net
                    - http://www.csharphelp.com
                    - http://www.poetry.com/Publications/
                    display.asp?ID= P3966388&BN=999 &PN=2
                    - Or, just search on "J.V.Ravichandr an"
                    at http://www.Google.com

                    *** Sent via Developersdex http://www.developersdex.com ***
                    Don't just participate in USENET...get rewarded for it!

                    Comment

                    Working...