Jump to content

[SOLVED] php modulus not working


cunoodle2

Recommended Posts

What am I doing wrong here?  I'm trying to get a list printed out but DON'T want anything ending in ".25" to be printed to the screen so I'm using php modulus to try to filter this out.  I've even tried it out with floatval to make sure that the variable wasn't a string or something.

 

<?php
for($a = 1; $a <= 3; $a+= .25)
{
if ($a % 1 != .25)
{
	$mod = $a%1;
	echo "Value: $a Mod: $mod<br />\n";
}
}

echo "<br />Using FLOATVAL.<br />\n";
for($a = 1; $a <= 3; $a+= .25)
{
if ((floatval($a) % 1) != .25)
{
	$mod = floatval($a)%1;
	echo "Value: $a Mod: $mod<br />\n";
}
}
?>

 

The above code produces the following output...

Value: 1 Mod: 0

Value: 1.25 Mod: 0

Value: 1.5 Mod: 0

Value: 1.75 Mod: 0

Value: 2 Mod: 0

Value: 2.25 Mod: 0

Value: 2.5 Mod: 0

Value: 2.75 Mod: 0

Value: 3 Mod: 0

 

Using FLOATVAL.

Value: 1 Mod: 0

Value: 1.25 Mod: 0

Value: 1.5 Mod: 0

Value: 1.75 Mod: 0

Value: 2 Mod: 0

Value: 2.25 Mod: 0

Value: 2.5 Mod: 0

Value: 2.75 Mod: 0

Value: 3 Mod: 0

 

Why is mod always zero???  Pretty basic thing here but I can't get it to work.

Link to comment
Share on other sites

also, the reason a modulo operator can only return an integer, is because that's how it works.  It takes x, finds out how many times it can divide by y, and gives you the remainder.  So for instance, 10 % 3 will be 1 because 3 can go into 10 3 times with a remainder of 1. So the remainder will always be a whole number.

Link to comment
Share on other sites

How many times does 1 go into 1.25? 1 time with a remainder of 0.25. How many times does 0.2 go into 1.1? 5 times with a remainder of 0.1. It can easily be extended to support real numbers as well.

 

well yeah, but that's not the point of modulus. The point of modulus is to give the whole number remainder. If you want a fraction remainder you'd just straight divide.

Link to comment
Share on other sites

How many times does 1 go into 1.25? 1 time with a remainder of 0.25. How many times does 0.2 go into 1.1? 5 times with a remainder of 0.1. It can easily be extended to support real numbers as well.

Q: How many times does 1 go into 1.25?

A: 1.25 times.

I think that meaning of the modulo is dealing with integers.

If someone wants to deal with real numbers then should make function for that like fmod (which is not operator %)

Link to comment
Share on other sites

probably a stupid question, but why not just inc by .5 instead?

 

for($a = 1; $a <= 3; $a+= .5)

Can't just use .5 because I need 1.75, 2.75 etc to show up in the list.  I just don't want anything that specifically ends in ".25" to print.  The fmod() totally did the trick.  Thanks for everyone's assistance.
Link to comment
Share on other sites

probably a stupid question, but why not just inc by .5 instead?

 

for($a = 1; $a <= 3; $a+= .5)

Can't just use .5 because I need 1.75, 2.75 etc to show up in the list.  I just don't want anything that specifically ends in ".25" to print.  The fmod() totally did the trick.  Thanks for everyone's assistance.

 

Well there you have it.  I knew I was asking a stupid question but couldn't quite put my finger on why, so I posted it anyways :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.