php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54164 Wrong Output Values
Submitted: 2011-03-04 23:17 UTC Modified: 2011-03-04 23:33 UTC
From: eskolife at hotmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.5 OS: Windows XP SP3
Private report: No CVE-ID: None
 [2011-03-04 23:17 UTC] eskolife at hotmail dot com
Description:
------------
$Val = cos(deg2rad(60));
$Val = $Val * 10;
echo $Val.'---'.intval($Val).'<br>';
echo $Val.'---'.floor($Val);

then the output of this simple code should be:
5---5
5---5

but the real output is:
5---4
5---4


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

  $Val = cos(deg2rad(60)); 
  $Val = $Val * 10;
  echo $Val.'---'.intval($Val).'<br>';
  echo $Val.'---'.floor($Val);

?>

Expected result:
----------------
5---5
5---5

Actual result:
--------------
5---4
5---4

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-04 23:33 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2011-03-04 23:33 UTC] rasmus@php.net
Truncating floating point values is always dangerous because computers do not 
store floating point values precisely, so when you do floor(4.999999999999999) you 
are going to get 4, not 5. Use round() instead. You can read more about it here: 
http://en.wikipedia.org/wiki/IEEE_754-2008
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 13:01:33 2024 UTC