Jump to content

Recommended Posts

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.

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/202285-php-cant-do-third-grade-math/
Share on other sites

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?

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

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.

  • 3 weeks later...

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?

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.