php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67257 Edge case error when casting to integer
Submitted: 2014-05-12 11:24 UTC Modified: 2014-05-12 16:23 UTC
From: cc at altruja dot de Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: 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: cc at altruja dot de
New email:
PHP Version: OS:

 

 [2014-05-12 11:24 UTC] cc at altruja dot de
Description:
------------
There seems to be an oddity in an edge case of casting float to integer after multiplying it with a multiple of 10. It only seems to occur with .4 after the decimal point, and only between 256.4 and 328.4. 

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

var_dump((integer) (255.4 * 100));
var_dump((integer) (256.4 * 100));
var_dump((integer) (256.3 * 100));
var_dump((integer) (327.4 * 100));
var_dump((integer) (328.4 * 100));

Expected result:
----------------
int(25540)
int(25640)
int(25630)
int(32740)
int(32840)

Actual result:
--------------
int(25540)
int(25639)
int(25630)
int(32739)
int(32840)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-05-12 16:23 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-05-12 16:23 UTC] requinix@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://www.floating-point-gui.de/

Thank you for your interest in PHP.

256.4 is represented internally like 256.39999999999999998 and multiplying exposes that error.

Do a round(256.4 * 100, -1) to get your expected result.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 10:01:33 2025 UTC