php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42658 floats not converted to ints correctly
Submitted: 2007-09-13 14:06 UTC Modified: 2007-09-18 11:01 UTC
From: contax at phrenetic dot org Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.4 OS: Darwin
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: contax at phrenetic dot org
New email:
PHP Version: OS:

 

 [2007-09-13 14:06 UTC] contax at phrenetic dot org
Description:
------------
When converting specific numbers to integers via strings things seem to go a little wrong. (I have also tested this on 5.2.0 on both FreeBSD and Debian)

Reproduce code:
---------------
<?php
$no1 = '18.74';
$no2 = $no1 * 100;
$no3 = (int) $no2;
$no4 = intval($no2);
var_dump($no1);
var_dump($no2);
var_dump($no3);
var_dump($no4);
?>

Expected result:
----------------
string(5) "18.74"
float(1874)
int(1874)
int(1874)

Actual result:
--------------
string(5) "18.74"
float(1874)
int(1873)
int(1873)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-09-18 11:01 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 Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 01:01:30 2024 UTC