php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45897 Invalid valid return for modulus
Submitted: 2008-08-23 13:09 UTC Modified: 2008-08-27 09:26 UTC
Votes:9
Avg. Score:1.4 ± 1.3
Reproduced:8 of 8 (100.0%)
Same Version:5 (62.5%)
Same OS:3 (37.5%)
From: hengkhee at gmail dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.4 OS: UBuntu 8.04, CentOS 5.0
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: hengkhee at gmail dot com
New email:
PHP Version: OS:

 

 [2008-08-23 13:09 UTC] hengkhee at gmail dot com
Description:
------------
- Modulus error, please refer to code.
- Using default package/installation from Ubuntu 8.04 Server/Desktop and CentOS 5.0
- Using default php.ini from distro.

Reproduce code:
---------------
$var1 = 0.15 * 3 * 100;
echo ($var1 % 5);		# Output "4", unexpected result
echo ((0.15 * 3 * 100) % 5);	# Output "4", unexpected result
echo (45.00 % 5);		# Output "0", correct result
echo (45 % 5);			# Output "0", correct result
echo (($var1."") % 5);		# Output "0", correct result

Expected result:
----------------
Correct result = "0" for all above sample.

Actual result:
--------------
Please refer to above sample code.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-23 13:14 UTC] hengkhee at gmail dot com
The php version on 

------------
Ubuntu 8.04:
------------
PHP 5.2.4-2ubuntu5.3 with Suhosin-Patch 0.9.6.2 (cli) (built: Jul 23 2008 06:46:18) 
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies

------------
CentOS:
------------
PHP 5.2.6 (cli) (built: Jul 29 2008 20:48:49) 
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
    with Zend Extension Manager v1.2.2, Copyright (c) 2003-2007, by Zend Technologies
    with Zend Optimizer v3.3.3, Copyright (c) 1998-2007, by Zend Technologies
 [2008-08-26 22:41 UTC] jani@php.net
Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is, read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.



$ php -dprecision=32 -r 'var_dump(0.15*3*100);'
float(44.999999999999992894572642398998)

 [2008-08-27 09:26 UTC] hengkhee at gmail dot com
Thank you for your reply,

I still not very clear about why this happen, as for a PHP beginner, the arithmetic below is very straight, but output the unexpected result:

$var1 = 0.15 * 3 * 100;
$var2 = $var1 + 1.0 - 1.0; #Perform some dummy matematic operation
$var3 = $var2 % 5;
var_dump($var3); # Still unexpected result, int(4)

For same arithmetic, Java is ok:
    float f, result;
    f = (float)(0.15 * 3 * 100);
    result = f%5;
    System.out.println(result);

My question is:
- Don't PHP processor should handle this situation? 
- So this consider not a bug of PHP? Or is weaknesses of it?
- Any work around? My work around is concat an string with the variable..
  May be it is a stupid way, but it works fine for me (until current version of PHP).

Thank you for your discussion and advice, I really appreaciated.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 21 14:01:33 2024 UTC