Strange behaviour, multiplying decimals (IE)
|
|
Thread rating:  |
craigslist.jg@gmail.com - 26 Sep 2007 19:20 GMT Hi,
I'm building an IE-centric portal for an intranet so I've only had to develop for IE 6+
My version is: 6.00290.2180
Here's what I'm seeing:
0.25 * 100 = 25 0.26 * 100 = 26 0.27 * 100 = 27
ok so far....
0.28 * 100 = 28.000000000000003 0.29 * 100 = 28.999999999999996
Not what I expected.
curiously, 0.299 yields what I'd expect, 29.9 Also, 0.299 * 1000 = 299, which is correct!
I iterated from 0.01 to 0.99 and these other combinations produce a similar output:
0.07 * 100 = 7.000000000000001 0.14 * 100 = 14.000000000000001 0.55 * 100 = 55.00000000000001 0.56 * 100 = 56.00000000000001 0.57 * 100 = 56.99999999999999 0.58 * 100 = 57.99999999999999
Anyone know of a safe way of doing multiplication which will produce the correct result and the right number of digits?
Thanks!
Evertjan. - 26 Sep 2007 19:51 GMT wrote on 26 sep 2007 in comp.lang.javascript:
> 0.28 * 100 = 28.000000000000003 > 0.29 * 100 = 28.999999999999996 <http://www.jibbering.com/faq/#FAQ4_7>
 Signature Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
craigslist.jg@gmail.com - 26 Sep 2007 20:28 GMT > wrote on 26 sep 2007 in comp.lang.javascript: > [quoted text clipped - 7 lines] > The Netherlands. > (Please change the x'es to dots in my emailaddress) Initially I couldn't figure out how to use the code in 4.6. I finally realized that the toFixed() protype was being overriden and all I had to do was use it as follows:
before: .29 * 100 = 28.999999999999996
after: (.29 * 100).toFixed(0) = 29. (then I regex the decimal out)
Perhaps an example or an explanation is needed in the faq?
thanks!
Dr J R Stockton - 26 Sep 2007 21:47 GMT In comp.lang.javascript message <1190834902.925514.8460@57g2000hsv.googl egroups.com>, Wed, 26 Sep 2007 12:28:22, craigslist.jg@gmail.com posted:
>> wrote on 26 sep 2007 in comp.lang.javascript: >> >> > 0.28 * 100 = 28.000000000000003 >> > 0.29 * 100 = 28.999999999999996 >> >> <http://www.jibbering.com/faq/#FAQ4_7> The reference in that section is all very well, though the references to Jscript may confuse the unwary a bit. But there should be a more user- friendly URL that could be given as well.
>> -- >> Evertjan. >> The Netherlands. >> (Please change the x'es to dots in my emailaddress) Don't quote signatures,
>Initially I couldn't figure out how to use the code in 4.6. Evertjan cited 4.6. He did not cite 4.6.
> I finally >realized that the toFixed() protype was being overriden and all I had [quoted text clipped - 5 lines] >after: >(.29 * 100).toFixed(0) = 29. (then I regex the decimal out) If you know you want an integer, don't use toFixed or StrS; use Math.round.
>Perhaps an example or an explanation is needed in the faq? The version of function StrU in the FAQ is well-known to be out-of-date.
Method toFixed (bugs in the original apart) is OK for those who love Methods. You can use StrS itself directly. Or StrU StrT StrW.
<FAQENTRY> To FAQ 4.7, new para 2.5 : "Otherwise, use Math.round on the results of expressions which should be of integer value."; and move the last sentence of 2.0 to precede that in 2.5.
 Signature (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME. Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links. Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036) Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
Randy Webb - 27 Sep 2007 01:17 GMT Dr J R Stockton said the following on 9/26/2007 4:47 PM:
> In comp.lang.javascript message <1190834902.925514.8460@57g2000hsv.googl > egroups.com>, Wed, 26 Sep 2007 12:28:22, craigslist.jg@gmail.com posted: [quoted text clipped - 3 lines] >>>> 0.29 * 100 = 28.999999999999996 >>> <http://www.jibbering.com/faq/#FAQ4_7> <snip>
>> Initially I couldn't figure out how to use the code in 4.6. > > Evertjan cited 4.6. He did not cite 4.6. I am curious how you cite something you did not cite.
 Signature Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Evertjan. - 27 Sep 2007 13:24 GMT Randy Webb wrote on 27 sep 2007 in comp.lang.javascript:
John wrote:
>> Evertjan cited 4.6. He did not cite 4.6. > > I am curious how you cite something you did not cite. The exegesis of the faq offers hidden gems.
 Signature Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
Dr J R Stockton - 27 Sep 2007 22:36 GMT In comp.lang.javascript message <Xns99B892942FBE9eejj99@194.109.133.242> , Thu, 27 Sep 2007 12:24:33, Evertjan. <exjxw.hannivoort@interxnl.net> posted:
>Randy Webb wrote on 27 sep 2007 in comp.lang.javascript: > [quoted text clipped - 4 lines] > >The exegesis of the faq offers hidden gems. Regrettably, the 6 in the main block my keyboard is dangerously close to the 7. I should have used the keypad.
Let us hope that Randy has taken due cognisance of the rest of the article.
 Signature (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME. Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links; Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc. No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Randy Webb - 28 Sep 2007 01:01 GMT Dr J R Stockton said the following on 9/27/2007 5:36 PM:
> In comp.lang.javascript message <Xns99B892942FBE9eejj99@194.109.133.242> > , Thu, 27 Sep 2007 12:24:33, Evertjan. <exjxw.hannivoort@interxnl.net> [quoted text clipped - 11 lines] > Let us hope that Randy has taken due cognisance of the rest of the > article. I did. :-)
 Signature Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
FAQEditor - 28 Sep 2007 01:05 GMT Dr J R Stockton said the following on 9/26/2007 4:47 PM:
<snip>
> The version of function StrU in the FAQ is well-known to be out-of-date. Then perhaps you would care to post a new updated version? And I will be happy to add it. I am not going to hunt it down and guess at it though.
> <FAQENTRY> To FAQ 4.7, new para 2.5 : "Otherwise, use Math.round on the > results of expressions which should be of integer value."; Added locally.
> and move the last sentence of 2.0 to precede that in 2.5. I have to assume that your "2.0" is a typo as I have no 2.0 in my local version. It would be even better if you simply did as you did above and simply say "move this line to this location" instead of all the cryptic crap.
 Signature Randy comp.lang.javascript FAQ - http://jibbering.com/faq/index.html FAQ Notes: http://www.jibbering.com/faq/faq_notes/faq_notes.html ECMAScript Language Specification via FAQ2.6
Dr J R Stockton - 28 Sep 2007 22:07 GMT >Dr J R Stockton said the following on 9/26/2007 4:47 PM: No; I posted it at Wed, 26 Sep 2007 21:47:19 +0100, having written it before that time.
>> The version of function StrU in the FAQ is well-known to be out-of-date. > >Then perhaps you would care to post a new updated version? And I will >be happy to add it. I am not going to hunt it down and guess at it >though. If you find the link in the FAQ inadequate, please explain why. It's only one obvious step from there to js-rndg1.htm#GC .
>> <FAQENTRY> To FAQ 4.7, new para 2.5 : "Otherwise, use Math.round on the >> results of expressions which should be of integer value."; [quoted text clipped - 5 lines] >I have to assume that your "2.0" is a typo as I have no 2.0 in my local >version. If you understand "2.5", you should be able to understand "2.0". The recommendation amounts to shifting a paragraph break.
 Signature (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME. Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links. Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036) Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
Randy Webb - 29 Sep 2007 04:52 GMT Dr J R Stockton said the following on 9/28/2007 5:07 PM:
>> Dr J R Stockton said the following on 9/26/2007 4:47 PM: > > No; I posted it at Wed, 26 Sep 2007 21:47:19 +0100, having written it > before that time. You should endeavor to understand how screen readers work. But, are we back to the attribution BS again?
>>> The version of function StrU in the FAQ is well-known to be out-of-date. >> Then perhaps you would care to post a new updated version? And I will >> be happy to add it. I am not going to hunt it down and guess at it >> though. > > If you find the link in the FAQ inadequate, please explain why. Thanks, but no thanks. If you find the FAQ code inadequate and want it changed, then you post a URL, or the code, to the group and then I will change it.
> It's only one obvious step from there to js-rndg1.htm#GC . And in the time you have spent waffling about posting it you could have simply posted the new code, it would have gotten updated locally - probably online as well - and all would be moving along nicely. Instead, we are having a conversation about steps and what is obvious to find in a site that I find a nightmare to try to traverse.
>>> <FAQENTRY> To FAQ 4.7, new para 2.5 : "Otherwise, use Math.round on the >>> results of expressions which should be of integer value."; [quoted text clipped - 6 lines] > If you understand "2.5", you should be able to understand "2.0". The > recommendation amounts to shifting a paragraph break. If you can't understand that there is no 2.0 in the FAQ, then nothing else would make sense to you either. Again, if you would simply state what you think should be changed instead of being so moronically obtuse about it then things would get updated a lot quicker than they do. Try it sometime.
 Signature Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Evertjan. - 26 Sep 2007 21:48 GMT wrote on 26 sep 2007 in comp.lang.javascript:
>> wrote on 26 sep 2007 in comp.lang.javascript: >> >> > 0.28 * 100 = 28.000000000000003 >> > 0.29 * 100 = 28.999999999999996 >> >> <http://www.jibbering.com/faq/#FAQ4_7> [please do not quote signatures on usenet, any decent newsreader will skip those for you automagically]
> Initially I couldn't figure out how to use the code in 4.6. I finally > realized that the toFixed() protype was being overriden and all I had > to do was use it as follows: No, the default toFixed(n) was overridden, as it has a bug [in IE?]
> before: > .29 * 100 = 28.999999999999996 [quoted text clipped - 3 lines] > > Perhaps an example or an explanation is needed in the faq? 4.6 indirectly states that that toFixed(n) needs n>0
Will this save your day?
alert( +(.29 * 100).toFixed(0) );
[Look, no Regex]
 Signature Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
Dr J R Stockton - 27 Sep 2007 11:06 GMT In comp.lang.javascript message <Xns99B7E7F0BF507eejj99@194.109.133.242> , Wed, 26 Sep 2007 20:48:02, Evertjan. <exjxw.hannivoort@interxnl.net> posted:
> wrote on 26 sep 2007 in comp.lang.javascript:
>No, the default toFixed(n) was overridden, as it has a bug [in IE?] The functions StrU StrS StrT StrW are more capable than method toFixed, as they allow fixing the number of characters before the decimal point, which is useful in Tables. Replacing default toFixed is a minor addition. That should be emphasised in the FAQ, perhaps by adding a comment line before the last line of code : // StrS can be used to implement a better toFixed method
>> before: >> .29 * 100 = 28.999999999999996 >> >> after: >> (.29 * 100).toFixed(0) = 29. (then I regex the decimal out) Method replace does not require a RegExp. With a string for its first parameter it is twice as fast, and code seems then of similar speed to code using substring.
>> Perhaps an example or an explanation is needed in the faq? > >4.6 indirectly states that that toFixed(n) needs n>0 ISO/IEC 16262 allows n=0. However, it requires a decimal point, giving a result incompatible with general good practice (IUPAP/SUNAMCO, IIRC, refers) which is to have at least one digit on each side of the point; that is why 4.6 disdains n=0. It does not state that n>0 is needed.
>Will this save your day? > >alert( +(.29 * 100).toFixed(0) ); > >[Look, no Regex] But it converts a Number to a String with toFixed, then back to a Number with unary plus (which alert converts to String display). Math.round should be much faster (nearly 4 times so, in IE6, it seems).
This function Sign(X) { return X>0 ? "+" : X<0 ? "-" : " " } would be better than the Sign in the FAQ, particularly for use in Tables (a sensible font will have plus minus and space all of the same width as a digit). A newbie can more easily remove unwanted parts than add missing ones.
It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 Signature (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6 news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>. <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources. <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Evertjan. - 27 Sep 2007 22:17 GMT Dr J R Stockton wrote on 27 sep 2007 in comp.lang.javascript:
>>4.6 indirectly states that that toFixed(n) needs n>0 > > ISO/IEC 16262 allows n=0. We were discussing another toFixed(n), not the default one.
 Signature Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
Dr J R Stockton - 28 Sep 2007 12:43 GMT In comp.lang.javascript message <Xns99B8ECF42C4F4eejj99@194.109.133.242> , Thu, 27 Sep 2007 21:17:36, Evertjan. <exjxw.hannivoort@interxnl.net> posted:
>Dr J R Stockton wrote on 27 sep 2007 in comp.lang.javascript: > [quoted text clipped - 4 lines] >We were discussing another toFixed(n), >not the default one. From the above, and from the code for the substitute toFixed in the FAQ, one can see that n=0 is allowed in both cases.
But n=0 should not be used in either case, as it gives a string ending in a decimal point, unless some constraint forces that to be used.
 Signature (c) John Stockton, Surrey, UK. *@merlyn.demon.co.uk / ??.Stockton@physics.org Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links. Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036) Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)
FAQEditor - 28 Sep 2007 01:00 GMT Dr J R Stockton said the following on 9/27/2007 6:06 AM:
<snip>
> This function Sign(X) { return X>0 ? "+" : X<0 ? "-" : " " } > would be better than the Sign in the FAQ, Replaced locally.
 Signature Randy comp.lang.javascript FAQ - http://jibbering.com/faq/index.html FAQ Notes: http://www.jibbering.com/faq/faq_notes/faq_notes.html ECMAScript Language Specification via FAQ2.6
Evertjan. - 28 Sep 2007 16:26 GMT FAQEditor wrote on 28 sep 2007 in comp.lang.javascript:
> Dr J R Stockton said the following on 9/27/2007 6:06 AM: > [quoted text clipped - 4 lines] > > Replaced locally. Just playing:
function Sign(X) {return ' -+'.charAt(!X?0:X<0?1:2)}
function Sign(X) {return ' -+'.split('')[!X?0:X<0?1:2]}
 Signature Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
Dr J R Stockton - 28 Sep 2007 22:07 GMT In comp.lang.javascript message <Xns99B9B158210ACeejj99@194.109.133.242> , Fri, 28 Sep 2007 15:26:01, Evertjan. <exjxw.hannivoort@interxnl.net> posted:
>function Sign(X) {return ' -+'.charAt(!X?0:X<0?1:2)} function Sign(X) {return '+ -'.charAt(X<0?2:!X)} // is shorter
 Signature (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6. Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Evertjan. - 29 Sep 2007 09:33 GMT Dr J R Stockton wrote on 28 sep 2007 in comp.lang.javascript:
> In comp.lang.javascript message Evertjan. posted: >> >>function Sign(X) {return ' -+'.charAt(!X?0:X<0?1:2)} > > function Sign(X) {return '+ -'.charAt(X<0?2:!X)} // is shorter Indeed.
This one is longer, but does not use any tertiary operator:
function Sign(X) {return ' +-'.charAt(!!X+(X<0))}
would it be faster?
 Signature Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
Evertjan. - 29 Sep 2007 09:47 GMT Evertjan. wrote on 29 sep 2007 in comp.lang.javascript:
> Dr J R Stockton wrote on 28 sep 2007 in comp.lang.javascript: >> In comp.lang.javascript message Evertjan. posted: [quoted text clipped - 8 lines] > > function Sign(X) {return ' +-'.charAt(!!X+(X<0))} Or we could compute the characters from their unicode value:
function Sign(X) {return String.fromCharCode(43-!X*11+(X<0)*2)}
 Signature Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
Dr J R Stockton - 29 Sep 2007 22:36 GMT In comp.lang.javascript message <Xns99BA6B539424Feejj99@194.109.133.242> , Sat, 29 Sep 2007 08:33:02, Evertjan. <exjxw.hannivoort@interxnl.net> posted:
>Dr J R Stockton wrote on 28 sep 2007 in comp.lang.javascript: >> In comp.lang.javascript message Evertjan. posted: >>> >>>function Sign(X) {return ' -+'.charAt(!X?0:X<0?1:2)} >> >> function Sign(X) {return '+ -'.charAt(X<0?2:!X)} // is shorter
>This one is longer, but does not use any tertiary operator: > >function Sign(X) {return ' +-'.charAt(!!X+(X<0))} > >would it be faster? Even including the one you posted next, the differences are insignificant, and will depend on the sign of the argument and therefore, overall, on the proportion of positive, negative, zero.
As most results are positive (IMHO), this should be insignificantly better :
function Sign(X) {return '- +'.charAt(X>0?2:!X)}
 Signature (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME. Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links; Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc. No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
craigslist.jg@gmail.com - 27 Sep 2007 14:26 GMT > wrote on 26 sep 2007 in comp.lang.javascript: > [quoted text clipped - 7 lines] > [please do not quote signatures on usenet, > any decent newsreader will skip those for you automagically] That's the magic of Google.
> > Initially I couldn't figure out how to use the code in 4.6. I finally > > realized that the toFixed() protype was being overriden and all I had [quoted text clipped - 17 lines] > > [Look, no Regex] Yea, that's much nicer and it works.. thanks.
|
|
|