PDA

View Full Version : Resolved subtracting float



abs0lut
01-09-2009, 09:32 AM
result=eval(parseFloat(n1)-parseFloat(n2));
I have problem in subtraction, the result is not exact. Could you please help me?

rangana
01-09-2009, 09:41 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?

abs0lut
01-09-2009, 10:09 AM
n1=2.55
n2=2
and the result is 0.5499999999999998

rangana
01-09-2009, 10:24 AM
Of course that's right.

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

You could use toFixed() (http://www.w3schools.com/jsref/jsref_tofixed.asp) method.

abs0lut
01-09-2009, 12:18 PM
thank you

jscheuer1
01-09-2009, 01:46 PM
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.

Twey
01-10-2009, 03:50 AM
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.

jscheuer1
01-10-2009, 04:02 AM
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.

Twey
01-10-2009, 09:37 AM
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.5499999999999998I'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.

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.

jscheuer1
01-10-2009, 02:43 PM
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.

Twey
01-10-2009, 03:37 PM
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.Not quite. It's interesting to experiment with this to see how smart the various interpreters are. In all of them, I should think, typing 0.55 directly will produce exactly 0.55. Spidermonkey at least can also handle subtracting 0.01: 0.56 - 0.01 yields 0.55, but 0.57 - 0.02 yields 0.5499999999999998.

Here is how they look to me: http://i44.tinypic.com/2hdp0s2.jpg. The original is the same height as a capital letter or a letter with an ascender, and the width is equal to the height.

From your description, it sounds as though the only font on your system which has this character is very different to the font being used to render the rest of the text.
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.It doesn't quite work like that. :) It is certainly standard (Unicode 3.0, VULGAR FRACTION ONE THIRD, U+2153), but your system and/or application may not support it — it must both support a Unicode encoding (or an encoding with an equivalent character) and have and be able to use a font with the appropriate glyph. If you're getting a question mark (an actual question mark), the usual explanation is that your editor is using a different encoding to your browser (presumably one which does not have the character) and a transcoding error has occurred during the data transfer. Most applications nowadays support Unicode, but your editor may not be able to find an appropriate font, especially if it has been restricted to only look for monospace fonts (which would make sense for an editor).

jscheuer1
01-10-2009, 04:22 PM
Still sounds a little non-standard, at least less standard than an a-z letter character.

Anyways, I think the thing is (as for legibility) that you don't have, are overriding, or have a slightly different version of the css style font for the forum. Because, if I make up a page, set its font to sans-serif or serif, and do a:


<script type="text/javascript">
document.write('\u2153');
</script>

It looks perfectly fine in all of my browsers.

Also, from your images, it doesn't appear to be too bad the way that I edited it, though certainly out of place next to the other text.

All pretty interesting stuff.

Oh and getting back to the math. I believe I originally said it depends upon the browser and/or CPU, I should probably have added - possibly also the OS.

Twey
01-10-2009, 04:28 PM
Still sounds a little non-standard, at least less standard than an a-z letter character.They're equally standard: both are backed by official organisations and clever people. Unicode is obviously not as widely supported as ASCII, which has been heralded as 'the most successful computer standard ever made', but it is nevertheless supported on most modern systems, to one extent or another.
Anyways, I think the thing is (as for legibility) that you don't have, are overriding, or have a slightly different version of the css style font for the forum.I'm not overriding anything: what you see is nothing more than the default style I receive for this forum. As I said, it's most likely a font issue on your part, if what you see is less legible than what I see.

jscheuer1
01-10-2009, 04:31 PM
Then why is it fine in my default fonts?

Anyways, I was thinking. It's true it has something to do with the fonts on my system visa vis screen size and resolution. However, what I have is about as basic as you can get. Users shouldn't have to modify that stuff just to read the web. And for 99.9999999999999998% :) of the stuff I view, it's fine.

Twey
01-10-2009, 08:31 PM
It depends on your font setup which fonts actually get selected in what order. The font-family declarations for vBulletin contain some fairly uncommon choices, and I'm sure that our configurations probably differ considerably in which of those are supported.
However, what I have is about as basic as you can get. Users shouldn't have to modify that stuff just to read the web. And for 99.9999999999999998% :) of the stuff I view, it's fine.Heh :D Rounding errors aside, the key phrase is 'of the stuff I view'. Presumably you don't see one-third characters every day. I seem to remember you had similar issues the first time you encountered Japanese text. Unicode fonts are big things, so it's understandable that Microsoft would leave it up to the user to choose them rather than bundling them all in. Perhaps they would have done better to take a more maximalist approach, perhaps not; either way, it's obviously a conscious design decision on their part.

Snookerman
01-10-2009, 08:46 PM
For the record, the ⅓ looks huge in all my browsers, including Opera.

jscheuer1
01-10-2009, 11:24 PM
For the record, the ⅓ looks huge in all my browsers, including Opera.

Which one(s)?

@ Twey, it's one thing to have to install fonts to see something. I've no problem with that. It's another to have a character that your computer already renders fine look weird. And I'm not blaming anybody. This seems to me to be a case though of certain things being incapable of being relied upon in a diverse environment like the web. Neither you nor I can control how things look on other folk's systems. Because of that, I probably never should have bothered with editing it. But it does show how when someone posts something into an environment like a forum over which they have no control, it only compounds the issue.

And I agree that in this case it's the differences in how various systems interpret and present the css, which for a forum is probably designed with universal legibility in mind. Just looks like this one slipped through the cracks.

In fact, while writing this I looked at the computed style in the DOM inspector*, it's the Verdana font that is causing this, as when I add that to my test page, the 1/3 looks just as poorly as it does on the forum.

Twey, does your system support Verdana? From your images, it doesn't look like the Verdana I know. Little matter, it's only in Opera here that it's such an issue, the others are passable.


*
font-family: verdana,geneva,lucida,'lucida grande',arial,helvetica,sans-serif;

Snookerman
01-10-2009, 11:37 PM
Which one(s)?
I looked at it in Fx3, IE6-8, Op9.63 and Chrome. The one I posted (⅓) looks fine. Here's Opera:
2420

magicyte
01-10-2009, 11:54 PM
(well I probably shouldn't be saying this, but geez! It's just a '⅓' character!)

Anyway, that's weird how browsers compute things differently in math... Any explanation? [It] turns out that computer AREN'T perfect! (but maybe you knew that; I'm really ignorant anyway ;):D)

-magicyte

Nile
01-10-2009, 11:56 PM
For me - it also looks the same in every browser. =/ That's weird.

jscheuer1
01-10-2009, 11:59 PM
I looked at it in Fx3, IE6-8, Op9.63 and Chrome. The one I posted (⅓) looks fine. Here's Opera:
2420

I thought you meant Huge, not just a bit big. I think I'll go back and fix it though, now that I know what to do that shouldn't adversely effect anyone, and that will make it OK on my system too.

Twey
01-11-2009, 06:06 PM
The blame presumably lies with vBulletin: Verdana isn't a good Web font (http://www.xs4all.nl/~sbpoley/webmatters/verdana.html), although the problem is usually the opposite. Oddly, though, I do indeed have Verdana installed, which leaves me a touch baffled as to why one system was affected and the other wasn't.

jscheuer1
01-11-2009, 06:21 PM
And though the effect is noticeable here in IE and FF, it is really only a serious issue in Opera. Is it possible that some browsers would render special characters differently than the font in use?

One thing I know about Opera is that it rounds font sizes given in ems or % differently, all browsers appear to. In Opera, this usually results in a smaller size, relatively speaking. However, if you set the % or ems dead on (so that they work out to an even number of pixels) they do appear the same across browsers.

I looked through my Opera config, and was unable to see an easy way to override Verdana, though I'm sure that there is some way, as Opera allows for user defined stylesheets.

I liked the critique on Verdana BTW.

Twey
01-11-2009, 08:14 PM
I'm not entirely sure... I'll have a look around for someone who knows.

jscheuer1
01-12-2009, 05:08 AM
To give you an idea of what I see:

http://home.comcast.net/~jscheuer1/side/frac/

Just imagine how illegible the 1/3 character would be in Opera with Verdana 100%.

Twey
01-12-2009, 08:26 AM
Looks just about legible to me (I zoomed out four times). The width is the main thing, but it's still possible to work it out from the outlines of the characters.

jscheuer1
01-12-2009, 08:48 AM
Well it gets smudged at 100% in Opera:

http://home.comcast.net/~jscheuer1/side/frac/frac_5_o.png

Twey
01-12-2009, 12:11 PM
Oh, yuck. That is quite terrible, indeed. Doesn't occur for me in Opera, though it does some sort of font enhancement that probably accounts for the blur (but it just makes the text look bold here).