php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27433 (float) to (int) error
Submitted: 2004-02-28 21:39 UTC Modified: 2004-02-29 03:24 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: ghetalion at ghetalion dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 4.3.4 OS: Linux
Private report: No CVE-ID: None
 [2004-02-28 21:39 UTC] ghetalion at ghetalion dot com
Description:
------------
When attempting to take a float variable and typecasting it into an int, I receive a magnificent error that is unexplainable.

Reproduce code:
---------------
function TimeToText($timestamp)
{
	$day = strftime("%d", $timestamp);

	$factor = $day/10;
	
	$factor = (int)(($factor- ((int)($factor)))*10);
	var_dump($factor);
}

Expected result:
----------------
int(3)

Actual result:
--------------
int(2)

Instead of truncating all decimal places, converting a float into an int is changing the entire whole-number value!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-02-28 21:43 UTC] ghetalion at ghetalion dot com
Set this into the function when declaring it:

TimeToText(1072224000);

And you will get the expected/actual result I defined.
 [2004-02-28 21:49 UTC] alindeman@php.net
If it's not clear already, if you remove the (int) cast, you get float(3); if the cast is there, it's int(2).  This may be some sort of obscure floating point rounding error.
 [2004-02-29 03:24 UTC] derick@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.
 
Thank you for your interest in PHP.

No bug.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 16:01:29 2024 UTC