Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Math Tests

  1. #1
    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 Math Tests

    1. Code:
      <script type="text/javascript">
      alert(184.64-165.52)
      </script>
    2. Code:
      <script type="text/javascript">
      alert((280.84*100-165.52*100)/100)
      </script>


    On my lap top, even though these sorts of calculations usually provide the correct answer, neither of the above do. Any ideas?

    Interestingly, if I do the first one with the multiply and divide stuff from the second, it comes out right. And if I do the second one as just a straight subtraction without the multiply and divide stuff, it also comes out right.

    Win XP Media Edition - Opera 9.01, IE 7, and FF 2.0.0.7 all make the exact same errors.
    - John
    ________________________

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

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Strange. What error/answer does it give you?
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    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

    Try 'em out and see what you get, but I'm getting:

    1. 19.119999999999976

    2. 115.31999999999996
    - John
    ________________________

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

  4. #4
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Same here. I'd say just round them.
    JS maths is slightly less accurate than the maths done by the calculator, I guess. Try having a look at this.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  5. #5
    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

    That's what I'm doing, rounding them. I wasn't really having a problem, so much as I wanted to know if anybody else was getting the same thing. I thought they probably would and that it was weird.

    I discovered this because I have a complex script and batch file that generates from a csv file a printable data sheet for a client in the form of an HTML page that I can then attach to an email. Usually I just take the values as given in the text strings, but this time I need to subtract the previous balance of 165.52 to get the actual totals for this category of transaction.

    I'm using:

    Code:
    money(Math.round((window[i][16]-165.52)*100)/100)
    where window[i][16] represents the running balance with the previous balance included. My money() function is:

    Code:
    function money (n){
    var c=n.toString(10).split('.')[1];
    return n.toString(10)+(c&&c.length==2? '' : c&&c.length==1? '0' : '.00');
    }
    Seems to work out.

    Notes: I only need this in FF, once I get the page in it, I use the Developer Extension to grab the generated source which is then cross browser. As a result, I've no idea (and don't need to) how cross browser any of the above code might or might not be.
    - John
    ________________________

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

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

    Default

    This will be the same in most languages. Floats in programming languages are stored in a binary form (as with any digital data), and since they can be infinite (1/3 anyone?) they get truncated. However, they're truncated in their binary representation, not their decimal one, so usually the truncated version isn't a clean round in decimal. This is why you should never compare two floats:
    Code:
    102.23952 === 102.23952 // WRONG
    ... because depending on how large a float representation the interpreter/compiler uses, one of them may not be what you expect it to be. Instead, you should round both or test a range (> 102 and < 103, for example). See IEEE-754.
    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!

  7. #7
    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 since found a much easier way of dealing with this (with US currency, at least):

    Code:
    (window[i][16]-165.52).toFixed(2)
    - John
    ________________________

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

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

    Default

    Be aware that this yields a string.
    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. #9
    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

    Quote Originally Posted by Twey View Post
    Be aware that this yields a string.
    And requires a number. It's tailor made for what I'm doing. I start out with a string that is formatted as US currency without the dollar sign (xx.xx, no comas). It becomes a number by virtue of subtracting 165.52 from it, then toFixed(2) turns it back into a rounded value to 2 decimal places string. Then I just slap on the dollar sign and it is ready for the report.

    Supported since Javascript 1.5 - your (referring to everyone here) results may vary.
    Last edited by jscheuer1; 10-16-2007 at 09:41 PM. Reason: usage
    - John
    ________________________

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

  10. #10
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    If it is important enought, you could make your own number object that can hold any real-world rational number in it and do base 10 calculations (or something). Of course, the object could conceivably be rather inefficient.
    Trinithis

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
  •