php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41714 Problem with intval()
Submitted: 2007-06-16 21:18 UTC Modified: 2007-06-17 21:19 UTC
From: bartlomiejbak at gmail dot com Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 5.2.3 OS: ubuntu
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bartlomiejbak at gmail dot com
New email:
PHP Version: OS:

 

 [2007-06-16 21:18 UTC] bartlomiejbak at gmail dot com
Description:
------------
Hi. I've got a little bit problem
this code should show me result of equation 20x + 27y = 1
for ($y = 0; $y < 1000; $y++) 
{
  $x = (1/20) - (27/20) * $y;
  if ($x == intval($x)) $out .= "x: $x,y: $y\n";
}

I need only int values of X and Y but intval() sometimes return wrong value. Result should be (-4,3),(-31,23),(-58,43) etc. but php is starting showing from (-31,23)...





Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-06-17 09:34 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.

.
 [2007-06-17 21:19 UTC] bartlomiejbak at gmail dot com
so. if you are right look at results of this code

function isInt($value) { return preg_match("/^-?[0-9]+$/",$value); }
for ($y = 0; $y < 100; $y++)
{
  $x = (1/20) - (27/20) * $y;
  if (isInt($x)) 
  {
    echo "x = $x; intval(x) = ".intval($x)."; var_dump(x == 
intval(x)) = ";
    var_dump($x == intval($x));
    echo "\n";
  }
}

first result
// x = -4; intval(x) = -4; var_dump(x == intval(x)) = bool(false)

why?
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Oct 28 04:01:23 2020 UTC