"Greater than" issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fangorn
Forum Commoner
Posts: 41
Joined: Fri May 21, 2004 9:04 am

"Greater than" issue

Post by fangorn »

I've been using PHP for 3 years now and haven't ever posted, but I'm stumped on this one and need some help.

I've got a big loop going through an array. What I'm seeing is that one 1 certain set of values, an expression like x>y evaluates to true even though x = y. Completely wrong! Here's my code snippet.

Code: Select all

for ($j=0;$j<$totalcount;$j++) {
  if (($x[$j] > $ymin) && ($x[$j] <= $ymax)) {
     $num++;
  }
}
So, what it's supposed to do is that if x > the min and x <= the max, increase num.

This code works great except in one case where $x = 2.982 and $ymin = 2.982. In that case, this equation evaluates to true and $num is increased.

So, I thought maybe had an issue with the logical && (even though it's so simple code), so I added a statement directly above the ymin,ymax code:

Code: Select all

if ($x[$j] > $ymin) {
  echo "hello";
}
This evaluates to true only when both of those numbers = 2.982. What am I missing? 2.982 is not greater than itself, is it?
Last edited by fangorn on Fri May 21, 2004 9:12 am, edited 1 time in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

i just ran this code on my server

Code: Select all

$x = 2.982;
$ymin = 2.982;


if ($x > $ymin) { 
	echo "hello"; 
}

// Outputs nothing
What version of PHP you using?

Mark
leenoble_uk
Forum Contributor
Posts: 108
Joined: Fri May 03, 2002 10:33 am
Location: Cheshire
Contact:

Post by leenoble_uk »

Where does the 2.982 figure come from?
Is it calculated or hard coded?

I ask because maybe each 2.982 is calculated a different way and that maybe one of them is 2.982000000001 or something like that because of that whole binary decimals thing.

2.982 = 10.11111....... and some other numbers.
fangorn
Forum Commoner
Posts: 41
Joined: Fri May 21, 2004 9:04 am

Post by fangorn »

Yes, I ran your code and agree that it works. You've populated the vars differently than I have though. Mine are all coming from a database. I found the fix, I have to use the trim function on both $x[$j] and $ymin. I guess PHP gets confused about treating these values as floats. I even tried the floatval function, but it was the trim that did the trick.
fangorn
Forum Commoner
Posts: 41
Joined: Fri May 21, 2004 9:04 am

Post by fangorn »

leenoble_uk - I thought about that. I've seen that problem in C before. But when I printed the var in PHP I expected to see any "hidden" decimal places. I didn't see any. The function trim works which means that I'm using the values as strings for the conditional case. Seems weird to me, but now I know more about PHP! Thanks.
Post Reply