Floating point numbers.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wyred
Forum Commoner
Posts: 86
Joined: Mon Dec 20, 2004 1:59 am
Location: Singapore

Floating point numbers.

Post by wyred »

A friend discovered this in VB, I did a quick test in PHP and found similar results. What could be wrong?

Here's the code:

Code: Select all

$value1 = 1;
$value2 = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1;

echo 'Value 1:'.$value1.'<br />';
echo 'Value 2:'.$value2.'<br />';
echo ($value1 == $value2)?'same':'diff';
Output:
Value 1:1
Value 2:1
diff
Discuss! :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

welcome to the wonderful world of floating point arithmetic errors.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

I don't get it feyd, shouldn't PHP convert the integer to a float and then return true for that comparison? It's not like he used ===

can you elaborate on these errors?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Note: for precision math look at http://ca3.php.net/manual/en/ref.bc.php
Last edited by John Cartwright on Sun Sep 04, 2005 11:48 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

[feyd@home]>php -r "$v1 = 1; $v2 = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1; printf('%.20f|%.20f',$v1,$v2);"
1.00000000000000000000|0.99999999999999982236

[feyd@home]>php -r "$v1 = 1; $v2 = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1; var_export(array_map('bin2hex',unpack('A8v1/A8v2',pack('d*',$v1,$v2))));"
array (
  'v1' => '000000000000f03f',
  'v2' => 'ffffffffffffef3f',
)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

$v1 = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1; 
$v2 = 0.99999999999999982236;
$v3 = 1;
var_dump(bccomp($v1, $v2, 20));


var_dump(bccomp($v1, $v3, 20));


var_dump(bccomp($v2, $v3, 20));
Now that's just silly
wyred
Forum Commoner
Posts: 86
Joined: Mon Dec 20, 2004 1:59 am
Location: Singapore

Post by wyred »

So what's the cause of all these errors? Anyway, here's another interesting set of results.

Code:

Code: Select all

$value3 = 0.1;
printf('Value 3: %.25f <br />',$value3);
$value3 += 0.1;
printf('Value 3: %.25f <br />',$value3);
$value3 += 0.1;
printf('Value 3: %.25f <br />',$value3);
$value3 += 0.1;
printf('Value 3: %.25f <br />',$value3);
$value3 += 0.1;
printf('Value 3: %.25f <br />',$value3);
$value3 += 0.1;
printf('Value 3: %.25f <br />',$value3);
$value3 += 0.1;
printf('Value 3: %.25f <br />',$value3);
$value3 += 0.1;
printf('Value 3: %.25f <br />',$value3);
$value3 += 0.1;
printf('Value 3: %.25f <br />',$value3);
$value3 += 0.1;
printf('Value 3: %.25f <br />',$value3);
Results:
Value 3: 0.1000000000000000000000000
Value 3: 0.2000000000000000000000000
Value 3: 0.3000000000000000444089210
Value 3: 0.4000000000000000000000000
Value 3: 0.5000000000000000000000000
Value 3: 0.6000000000000000000000000
Value 3: 0.7000000000000000000000000
Value 3: 0.7999999999999999111821580
Value 3: 0.9000000000000000000000000
Value 3: 0.9999999999999998223643161
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

http://docs.sun.com/source/806-3568/ncg_goldberg.html

found by: Google :: floating point error


basically, it's an intrinsic part of using floating point values. There's nothing you can do about them other than use arbitrary length computations.
Post Reply