mgmarcum Posted May 19, 2010 Share Posted May 19, 2010 So I'm sure someone has seen this before, but I couldn't find a mention of it anywhere. I've replicated this on 3 different php installs: echo (206.8008-15.7608-191.04); the output is: 2.84217094304E-14 wtf?!?! This is simple subtraction! I've also tried echo 206.8008-15.7608-191.04; and echo ((206.8008-15.7608)-191.04); all of them output 2.84217094304E-14 Ok, so this is some type of float/double conversion type bug right? echo (((double)206.8008-(double)15.7608)-(double)191.04); echo (((float)206.8008-(float)15.7608)-(float)191.04); same output. and jst for the hell of it $var= (((float)206.8008-(float)15.7608)-(float)191.04); echo $var; same thing: 2.84217094304E-14 funny thing is that echo (206.8008-15.7608) gives 191.04 so you'd think echo (206.8008-15.7608)-191.04 would output the correct answer. I noticed this because I was using money_format and it was returning -0.00 (yes with the negative sign in front) which is a little closer to the real answer but still incredibly dumb. started trying to debug this and when I echoed out the math that was getting passed to money format I nearly spilled my coffee... verified on these two php versions (cli and apache module) PHP 5.3.2-1ubuntu4 with Suhosin-Patch (cli) (built: Apr 9 2010 08:18:14) PHP 5.1.6 (cli) (built: Jan 4 2010 10:45:55) Can someone tell me what the hell is going here? I'm trying to do simple tax calculations on an ecommerce site. I've been doing php developement for about 7 years now and this is the first time I've ever seen anything like this. All the other php devs in my office are stumped and amazed too. Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/ Share on other sites More sharing options...
Daniel0 Posted May 19, 2010 Share Posted May 19, 2010 It's an inherent issue with floating point numbers. See: http://docs.sun.com/source/806-3568/ncg_goldberg.html You'll have to either use integers only or use an arbitrary precision library such as BC Math. Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/#findComment-1060694 Share on other sites More sharing options...
Mchl Posted May 19, 2010 Share Posted May 19, 2010 And just to make it clear: it's not a PHP issue. That will happen anywhere you use floating point variables. Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/#findComment-1060752 Share on other sites More sharing options...
mgmarcum Posted May 19, 2010 Author Share Posted May 19, 2010 This is Insane, All I want to do is subtract three floating point numbers and get the right answer. So does openoffice spreadsheet and my linux calculator use a special library? Whats the point of using an ieee compliant floating point processor if its going to give the wrong answer? Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/#findComment-1060768 Share on other sites More sharing options...
mgmarcum Posted May 19, 2010 Author Share Posted May 19, 2010 Last reply - and I promise I will never post here again: mmarcum@blackbird:~$ cat test.cc #include <iostream> using namespace std; int main() { cout << (206.8008-15.7608-191.04) << endl; return 0; } mmarcum@blackbird:~$ g++ test.cc mmarcum@blackbird:~$ ./a.out 2.84217e-14 mmarcum@blackbird:~$ Unfrickinbelievable Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/#findComment-1060769 Share on other sites More sharing options...
Daniel0 Posted May 19, 2010 Share Posted May 19, 2010 How is it unbelievable? It's quite obvious to me. What's more unbelievable to me is that you've managed to write programs for seven years and not know about floating point limitations (no offense) and that none of your coworkers know about it either. You should never use floats for things where you can't live with an approximation. Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/#findComment-1060773 Share on other sites More sharing options...
Mchl Posted May 19, 2010 Share Posted May 19, 2010 Applications like Calc, Excel or even simple calculators usually do not use floating point values. Exactly for this reason. Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/#findComment-1060778 Share on other sites More sharing options...
GoneNowBye Posted June 5, 2010 Share Posted June 5, 2010 You will need an arbitary precission system, to write one yourself, or *(10^decimal places) and treat is as an integer. Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/#findComment-1068305 Share on other sites More sharing options...
ignace Posted June 5, 2010 Share Posted June 5, 2010 What's more unbelievable to me is that you've managed to write programs for seven years and not know about floating point limitations (no offense) and that none of your coworkers know about it either. Exactly my thoughts. Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/#findComment-1068314 Share on other sites More sharing options...
GoneNowBye Posted June 5, 2010 Share Posted June 5, 2010 binary 16,8,4,2,1 and if you make it bigger by going left the capactiy doubles -1, (max of 4 bit is 1+2+4+8 = 15 then add one colomn (16) you get 15*2 + 1, 31 so forth) and you give it more bits you make it hold bigger numbers floats go the other way. 16,8,4,2,1,0.5,0.25,0.125 and if you want to express 0.6 for example, using the number system on the left you have 00000101 no 16's no 8's no 4's no 2's no 1's 0.5 no 0.25's 0.125 which is 0.625 not 0.6 thats how it aproxmates. helped? Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/#findComment-1068388 Share on other sites More sharing options...
Mchl Posted June 5, 2010 Share Posted June 5, 2010 That's not exactly how it works. Numbers are in general represented approximately to a fixed number of significant digits and scaled using an exponent. The base for the scaling is normally 2, 10 or 16. The typical number that can be represented exactly is of the form: significant digits × baseexponent Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/#findComment-1068394 Share on other sites More sharing options...
GoneNowBye Posted June 5, 2010 Share Posted June 5, 2010 its a context thing, simplfied thats floating point maths Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/#findComment-1068399 Share on other sites More sharing options...
Mchl Posted June 5, 2010 Share Posted June 5, 2010 That's overly simplified to my liking. Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/#findComment-1068401 Share on other sites More sharing options...
GoneNowBye Posted June 5, 2010 Share Posted June 5, 2010 if it was complicated clearly the OP wouldn't understand it. Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/#findComment-1068403 Share on other sites More sharing options...
Mchl Posted June 5, 2010 Share Posted June 5, 2010 Why you're assuming that? Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/#findComment-1068405 Share on other sites More sharing options...
GoneNowBye Posted June 5, 2010 Share Posted June 5, 2010 What's more unbelievable to me is that you've managed to write programs for seven years and not know about floating point limitations (no offense) and that none of your coworkers know about it either. Exactly my thoughts. cos of that Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/#findComment-1068413 Share on other sites More sharing options...
Mchl Posted June 6, 2010 Share Posted June 6, 2010 Just because he did not know that, does not mean he can not grasp it Quote Link to comment https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/#findComment-1068473 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.