php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51320 Modulus Fails with the number 2.32
Submitted: 2010-03-18 10:53 UTC Modified: 2010-03-22 06:13 UTC
Votes:5
Avg. Score:4.6 ± 0.8
Reproduced:5 of 5 (100.0%)
Same Version:2 (40.0%)
Same OS:3 (60.0%)
From: woody dot gilk at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: OSX, Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: woody dot gilk at gmail dot com
New email:
PHP Version: OS:

 

 [2010-03-18 10:53 UTC] woody dot gilk at gmail dot com
Description:
------------
Tested on the following PHP versions:

5.3.2 (OSX)
5.2.5 (Linux, Codepad.org)
5.2.4 (Ubuntu)

The following script improperly reports the modulus as "31" instead of "32", but 
doesn't seem to happen with any other numbers.

Test script:
---------------
<?php

$number = 3.32; echo ($number * 100) % 100;

Expected result:
----------------
32

Actual result:
--------------
31

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-18 10:55 UTC] woody dot gilk at gmail dot com
Sorry, the test script $number value should be "2.32" not "3.32", the improper 
value of "31" is only reported for "2.32", no other numbers.
 [2010-03-21 20:04 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-03-21 20:04 UTC] johannes@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.

.
 [2010-03-21 20:15 UTC] woody dot gilk at gmail dot com
I don't believe this is actually bogus, but may rather be an error in the way that 
PHP converts from float point to integer. Consider:

var_dump(((int) (100 * 2.32)) % 100);
var_dump(232 % 100);

Even though the value of (100 * 2.32) is converted to an integer with a value of 
"232", the modulus still fails. Maybe this is the bug that really exists?
 [2010-03-21 20:30 UTC] rasmus@php.net
You didn't check your integer conversion.

> var_dump(((int) (100 * 2.32)) % 100);
int(231)

2.32 cannot be represented accurately in a computer.  It is actually 
2.319999999999 so whenever you do things like floor() or modulus that relies on 
decimals being precise down to a very low precision you have to account for 
that.  round() it first to be safe.
 [2010-03-21 22:00 UTC] rasmus@php.net
I meant:

> var_dump(((int) (100 * 2.32)))

there
 [2010-03-22 06:13 UTC] woody dot gilk at gmail dot com
Thanks for the clear explanation Rasmus, you were correct. I assumed that "2.32 * 
100" would be "232" but that is not the case.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Aug 17 03:00:03 2025 UTC