benphp Posted March 6, 2009 Share Posted March 6, 2009 var numerator = 186100488800821781.9216; var denominator = 496; var quot1 = (numerator / denominator); document.form.quotient.value = quot1; The result is: 375202598388753.6 but the real number (using a calculator) is 375202598388753.59258387096774194 It's been rounded off - without even using Math.Round. How do I get the actual number? Link to comment https://forums.phpfreaks.com/topic/148275-why-is-my-math-getting-rounded/ Share on other sites More sharing options...
Mchl Posted March 6, 2009 Share Posted March 6, 2009 http://en.wikipedia.org/wiki/Floating_point Link to comment https://forums.phpfreaks.com/topic/148275-why-is-my-math-getting-rounded/#findComment-778413 Share on other sites More sharing options...
benphp Posted March 6, 2009 Author Share Posted March 6, 2009 Thanks. Do you know how to fix it? Link to comment https://forums.phpfreaks.com/topic/148275-why-is-my-math-getting-rounded/#findComment-778421 Share on other sites More sharing options...
Mchl Posted March 6, 2009 Share Posted March 6, 2009 That's the precision limit for 32 bit (single) floating point number. Not sure if you can have any more precision in JavaScript. [edit] 32bits is single precision of course. Link to comment https://forums.phpfreaks.com/topic/148275-why-is-my-math-getting-rounded/#findComment-778425 Share on other sites More sharing options...
Daniel0 Posted March 6, 2009 Share Posted March 6, 2009 As soon as you take any calculus classes you'll realize that absolutely precision is unnecessary, and indeed in many cases impossible. Which number is for instance closest to 0? 1? How about 0.1, 0.01, 0.001, etc.? Even the number you you say is the "real number" is in fact not "real" in the sense you use the word. It's just a matter of how accurate you bother to be. Anyway, check this out: http://www-cs-students.stanford.edu/~tjw/jsbn/. I haven't used it myself, but it came up on a Google search result page. Link to comment https://forums.phpfreaks.com/topic/148275-why-is-my-math-getting-rounded/#findComment-778428 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.