
Using Float / decimal in Jscript & JavaScript
This could seem banal, but I need to substract 2 float numbers, but I don't seem to get the proper answer:<BR><BR>2.5  2.4 = 0.10000000000000009<BR><BR>even if I use <BR>var answer = 2.5  2.4<BR>or the eval function, still doesn't work<BR>isn't it suppose to give me 0.1 ???<BR><BR>thanks for any advices,<BR><BR><BR>ejparle<BR>Montreal, Can, Go Habs Go!

No different than other languages...
Welcome to the world of binary computers!<BR><BR>Just as you can't represent 1/3 exactly in decimal notation (you end up with an infinite string of digits, 0.333333333....), you can't represent 1/10 exactly in binary notation, as used inside 99% of all computers in use today. (Okay, maybe there are enough old COBOL programs out there that it's only 90% or so... But aside from COBOL and a handful of other old languages...)<BR><BR>So the answer you are getting is as close as the computer can compute it, given the limitations of binary arithmetic.<BR><BR>You can't *EVER* get *exactly* an answer of 0.1 with float/double numbers. You can get close.<BR><BR>

I don't get it...
I just tried that in c++:<BR><BR>main()<BR>{<BR> float z = 2.5  2.4;<BR> cout << "z: " << z << endl;<BR>}<BR><BR>and when I execute the program, I get:<BR>> z: 0.1<BR><BR>that's what I need in JavaScript, but that's not what I get...<BR>if it's not possible to have 0.1 as the result of 2.5  2.4 , is<BR>there a function to truncate the result to the significant number, or I need to convert it to a string and truncate it myself? I know about the round function, but I need something like this for float.

Because your C++ test is bogus
C++ is simply ROUNDING THE DISPLAYED result!<BR><BR>C++ is *LYING* to you!<BR><BR>Hard to believe you learned C++ and never learned the fundamentals of floating point arithmetic. (I don't blame you; I blame whomever or whatever you learned from. Because it's such a very very important topic!)<BR><BR>Try this in C++:<BR><BR>main( )<BR>{<BR> double tenth = 2.5  2.4;<BR> double sum = 0.0;<BR> for ( int i = 1; i <= 100; ++i ) sum += tenth;<BR> cout << "sum: " << sum << endl << "sum minus 10: " << (sum10.0) << endl;<BR>}<BR><BR>See what *that* shows you!<BR><BR>cout in C++, especially, does a good job of rounding off numbers and thus lying to you. VBScript, incidentally, does the same thing. JavaScript is an unsophisticated language, so it doesn't lie to you.<BR><BR><BR>

now I get it, but then how can javascript lie to m
ok, I get it... so then how do I make javascript lie to me?<BR>Is there a function or something to end up with the answer 0.1?<BR>

Well...
...you'll never get a *NUMERICAL* answer of 0.1, as I noted.<BR><BR>But you can get JS to give you an answer accurate to N digits, which is really what C++ is doing with cout.<BR><BR>If you know how many digits you want, you can do something like this:<BR><BR>function RoundTwo( num )<BR>{<BR> var sgn = num < 0 ? "" : "";<BR> num = Math.round( Math.abs( 100 * num ) );<BR> if ( num < 10 ) return sgn + "0.0" + num;<BR> if ( num < 100 ) return sgn + "0." + num;<BR> num = sgn + num; // convert it to a string<BR> return num.substring(0,num.length2) + "." + num.substring(num.length2);<BR>}<BR><BR>It's easy to create a function that rounds to any specific number of decimal places. It's tougher to create one that rounds to an arbitrary number, but not at all impossible. I'll point you to:<BR>http://www.irt.org and the JavaScript FAQ<BR>and<BR>http://javascript.internet.com [sister site of this place]<BR><BR>You can surely find functions to do most anything you want at those two (and other JS) sites.<BR><BR>

Thanks!
Thanks a lot, you've been very helpfull. I'll go check those links. :)<BR><BR><BR>ejparle

To add to the explanation
It got me as well last year... you may want to look at why it happens:<BR><BR>http://www.aspmessageboard.com/forum/showMessage.asp?F=27&M=278406&P=1

You do keep so odd links...
...handy, don't you?<BR><BR>Maybe I should package that sequence up into an article. Only a hundred people would read itand only seven would carebut it might be fun, anyway.<BR><BR>

RE: You do keep so odd links...
Actually, I had to search for it... just searched for "***" in the advanced forum.<BR><BR>You could make it into an article, but since the majority of people on here use VB/VBS, it's probably not going to make a difference to 'em.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

