cunoodle2 Posted June 27, 2009 Share Posted June 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/163919-solved-php-modulus-not-working/ Share on other sites More sharing options...
Daniel0 Posted June 27, 2009 Share Posted June 27, 2009 For some reason, PHP's modulo operator can only return an integer. (int).25===0. You can use fmod to get a float instead. Edit: To clarify, the operands of % get converted to an integer, not the result. This means that for all integers a, a%1=0. Quote Link to comment https://forums.phpfreaks.com/topic/163919-solved-php-modulus-not-working/#findComment-864820 Share on other sites More sharing options...
.josh Posted June 28, 2009 Share Posted June 28, 2009 probably a stupid question, but why not just inc by .5 instead? for($a = 1; $a <= 3; $a+= .5) Quote Link to comment https://forums.phpfreaks.com/topic/163919-solved-php-modulus-not-working/#findComment-864878 Share on other sites More sharing options...
.josh Posted June 28, 2009 Share Posted June 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/163919-solved-php-modulus-not-working/#findComment-864879 Share on other sites More sharing options...
Daniel0 Posted June 28, 2009 Share Posted June 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/163919-solved-php-modulus-not-working/#findComment-864949 Share on other sites More sharing options...
.josh Posted June 28, 2009 Share Posted June 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/163919-solved-php-modulus-not-working/#findComment-864954 Share on other sites More sharing options...
Daniel0 Posted June 28, 2009 Share Posted June 28, 2009 There is no reason why it cannot be expanded to cover all real numbers, hence the reason why it has been done in some fields outside of number theory. It's not just some BS I made up. Quote Link to comment https://forums.phpfreaks.com/topic/163919-solved-php-modulus-not-working/#findComment-864965 Share on other sites More sharing options...
dzelenika Posted June 28, 2009 Share Posted June 28, 2009 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 %) Quote Link to comment https://forums.phpfreaks.com/topic/163919-solved-php-modulus-not-working/#findComment-865014 Share on other sites More sharing options...
cunoodle2 Posted June 28, 2009 Author Share Posted June 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/163919-solved-php-modulus-not-working/#findComment-865195 Share on other sites More sharing options...
.josh Posted June 28, 2009 Share Posted June 28, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/163919-solved-php-modulus-not-working/#findComment-865229 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.