question about integer

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

    question about integer

    hello there,


    i found a interesting problem here.

    i transform a string data "180" to int and then multiple by 0.7
    (Integer.parseI nt("180"))*0.7

    and i got the result of "125.9999999999 999999".


    why?? @_@"

    thanks :)


    Castle


  • Brad BARCLAY

    #2
    Re: question about integer

    Castle Wu wrote:
    [color=blue]
    > i found a interesting problem here.
    >
    > i transform a string data "180" to int and then multiple by 0.7
    > (Integer.parseI nt("180"))*0.7
    >
    > and i got the result of "125.9999999999 999999".
    >
    > why?? @_@"[/color]

    0.7 doesn't represent too well as a floating point binary value.
    While it's a fairly simple value in decimal, in binary it's one of the
    infinately repeating numbers (something akin to the square root of 2 in
    decimal). Welcome to working with floating point values in the computing
    world.

    Most systems have code to account for this somewhat, but this tends to
    work best when the two values are of the same type. Try your test again
    with the 180 represented as a double, and you'll get the correct result.

    Brad BARCLAY

    --
    =-=-=-=-=-=-=-=-=
    From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
    The jSyncManager Project: http://www.jsyncmanager.org

    Comment

    Working...