result=eval(parseFloat(n1)-parseFloat(n2));
I have problem in subtraction, the result is not exact. Could you please help me?
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.
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!
n1=2.55
n2=2
and the result is 0.5499999999999998
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!
abs0lut (01-09-2009)
thank you
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
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 usingevalhere? For almost all use cases, if you're callingeval(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!
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
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.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
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!
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