Page 1 of 3 123 LastLast
Results 1 to 10 of 28

Thread: subtracting float

  1. #1
    Join Date
    Aug 2008
    Posts
    23
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default subtracting float

    result=eval(parseFloat(n1)-parseFloat(n2));
    I have problem in subtraction, the result is not exact. Could you please help me?
    Last edited by abs0lut; 01-10-2009 at 12:34 AM.

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    What's the value of n1 and n2?

    Also, don't use eval, no need.

    When you said, "not exact", what exactly do you mean?
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    Aug 2008
    Posts
    23
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default

    n1=2.55
    n2=2
    and the result is 0.5499999999999998

  4. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Of course that's right.

    ...you're expecting what value? 0.55?

    You could use toFixed() method.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  5. The Following User Says Thank You to rangana For This Useful Post:

    abs0lut (01-09-2009)

  6. #5
    Join Date
    Aug 2008
    Posts
    23
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default

    thank you

  7. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Always be aware that browser and/or CPU math can be wrong. This is a good example of the fact. Also a good solution for this case.

    A lot depends though upon the ultimate use of the result. If it was just to set a style property, the browser will round the value for you to its allowed decimal places for it.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    This is the binary equivalent of a number like — some numbers cannot be represented precisely as floats. For this reason, never compare floats directly for equality: always round first.

    Also, why are you using eval here? For almost all use cases, if you're calling eval (or otherwise representing code as strings) then you're doing it wrong. Just about the only exception is JSON, for which the engine's built-in parser is a useful and efficient alternative.
    Last edited by jscheuer1; 01-11-2009 at 12:00 AM. Reason: font for 1/3
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Apologies Twey if my edit of your post made your 1/3 character too large in your browser, I'm betting that it will be for the best in most folk's browsers.

    Anyways, this was not an issue of 1/3. And perhaps not an issue at all on the type of CPU/OS combination you run. But that sad fact is that in windows, most browsers will flub something as simple as:

    2.55 - 2 = 0.55

    reporting the result totally erroneously as:

    0.5499999999999998

    No parsing required to get that bad result.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Anyways, this was not an issue of 1/3. And perhaps not an issue at all on the type of CPU/OS combination you run. But that sad fact is that in windows, most browsers will flub something as simple as:

    2.55 - 2 = 0.55

    reporting the result totally erroneously as:

    0.5499999999999998
    I'm afraid you misunderstood me. Obviously it is not ⅓, but just as ⅓ can't be represented accurately (in finite space) in decimal, certain numbers cannot be represented accurately (in finite space) in binary — such as, for example, 0.55. If you enter them directly then rounding (or a BCD encoding) will be applied to hide this, but when performing the calculation the computer cannot be sure what you meant, and therefore only approximates it to the accuracy permitted by the amount of memory allocated for that number, rather than filling up all the computer's memory in an attempt to represent it perfectly.

    Edit: Regarding your edit: I don't like resizing things for readability purposes. Browsers nowadays have perfectly good text-resizing capabilities at the fingertips of the user. If a user has a problem reading the text, they can resize it as they desire. The original is quite readable to me, and, being of capital height and equal width, should be to anyone with a reasonable font size set.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  11. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm not sure what you mean by 'enter them directly'. Using the numbers directly in javascript with no parse still gives the same 'wrong' answer.

    About the font, I use the default fonts that 'came with' in all my browsers. They are times new roman for serif, arial for sans-serif and courier new for monospace. I believe the forum is in arial or some other sans-serif font, as it would otherwise be serif/times (the ultimate fall back when no font is specified) in my browsers. With that (in Opera, my usual browser) your 1/3 character is an illegible smudge, not much taller than a lower case x, not much wider than a semicolon. If I zoom 5 times, it's good though light (as though with an ultra fine point pen), while everything else is huge.

    Is it outrageously large in your browser as I edited it?

    Looks OK, if you like eyestrain, in FF. IE is somewhere in between the two.

    It's also at least a bit non-standard in some way, in that if I copy and paste it to my text editor (which does support some fraction characters), I get a ? character.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •