How do I determine if a digit divided by 8 is an integer?
for example:
32 % 8 = 4, it is an integer.
25 % 8 = 3.125, it is not an integer.
I need a working code like:
if ($str % 8 == integer){
// continue working
}
the % operator, modulo, will ALWAYS return an integer - it's the remainder of the division.
If you mean you want to check if a number is evenly divisible by 8, then do
if ($str % 8 == 0) {
... evenly divisible by 8 ...
}
you can go with if (val % 8 == 0) or with more tricky ways, like val & 0x0FFF == 0 by using bitwise operators.
Somehow they work in the same way: first snippet checks if the remainder of the division by 8 is zero while second checks if number doesn't have any binary digit for 1, 2, or 4, which would make the number not divisible by 8.
I think this is what you need:
if(is_int($integer)) {
// do something with integer
}
Related
In php:
the Modulus % gives Remainder of $x divided by $y.
I have tryed this code:
<?php
print(100000000165 % 5);
result is 2
since it should be 0
This happens because you're working on a 32bit system.
The largest integer number in 32bit php is 2147483647. That means after that (beginning with 2147483648) it's an overflow and wraps.
Your number is greater than that and so the result is: (100000000165 % 2147483648) % 5 => 1215752357 % 5 => 2
Addition: You can build the modulus function by yourself and deal with floats
$largeNumberThatBreaksInteger = 10000000000000000000165;
$modulus = $largeNumberThatBreaksInteger / PHP_INT_MAX - (int)($largeNumberThatBreaksInteger / PHP_INT_MAX) * PHP_INT_MAX;
// results in something like -9.9981352879506E+21. So you can compare it with an epsilon and know if it's 0 or not.
Dealing with floats and epsilon
I've always used the following in order to find even and odd numbers:
if( $num % 2 ) { echo "odd"; }
if( !($num % 2) ) { echo "even"; }
But recently I stumbled upon with the following code that works exactly the same:
if( $num & 1 ) { echo "odd"; }
if( !($num & 1) ) { echo "even; }
What's the logic behind the "&" in the second method?
I went to check the PHP: Arithmetic Operators and the ampersand is not part of the options.
Thanks.
It is the bitwise-AND operator. Remember that in the computer, every integer is stored in binary form, and the lowest-significance binary digit is 2^0 == 1. So, every odd number will have the lowest binary digit = 1.
So, the bitwise AND operator compares your value bit-by-bit with the constant 1. Bits that are 1 in both operands are set to 1 in the result, but bits that are 0 in either operand are set to 0 in the result. The final result (which will be either 1 or 0) is coerced to boolean by PHP because you are using it as the clause in an if() statement.
There is a very good reason for checking evenness with & instead of %: Speed! The % operator requires a division operation so the remainder can be calculated, which is computationally much, much more expensive than just comparing the bits directly.
An example:
$num = 9; // 9 == 8 + 1 == 2^3 + 2^0 == 1001b
echo (string)($num & 1); // 1001b & 0001b = 0001b - prints '1'
$num = 10; // 10 == 8 + 2 == 2^3 + 2^1 == 1010b
echo (string)($num & 1); // 1010b & 0001b = 0000b - prints '0'
& is the binary AND.
The binary value of an odd number AND 1 will be 1, and the binary value of an even number AND 1 will be 0.
This happens because the binary value of an odd number always ends with 1 and the binary value of an even number ends with 0. So...
10101101 & 00000001 = 00000001 in the case of an odd number and,
10101100 & 00000000 = 00000000 in the case of an even number.
Just out of curiosity, how can you tell if a number x is a power of two (x = 2^n) without using recursion.
Thanks
One way is to use bitwise AND. If a number $x is a power of two (e.g., 8=1000), it will have no bits in common with its predecessor (7=0111). So you can write:
($x & ($x - 1)) == 0
Note: This will give a false positive for $x == 0.
Subtract 1 from the number, then and it with the original number. If the result is zero, it was a power of two.
if (((n-1) & n) == 0) {
// power of two!
}
(sorry, my PHP is rusty...)
If it's a power of 2? Well, one way is to convert it to binary, and verify the presence of only 1 1...:
$bin = decbin($number);
if (preg_match('/^0*10*$/', $bin)) {
//Even Power Of 2
}
For completeness, if the number is a float, you can test if it's a power of two by chacking if the mantissa is all zeros:
<?php
$number = 1.2379400392853803e27;
$d = unpack("h*", pack("d", $number)); $d = reset($d);
$isPowerOfTwo = substr($d, 0, 13) == "0000000000000";
var_dump($isPowerOfTwo); // bool(true)
Exercise for the reader: corner cases and big-endian machines.
In a binary equivalent of any decimal number which is a power of two will have only one occurrence of 1 in its binary equivalent.
<?php
$number = 4096;
$bin = decbin($number);
if ($number != 1 && substr_count($bin,1) == 1) {
echo "Yes";
} else {
echo "No";
}
?>
The top answer:
($x & ($x - 1)) == 0
seemed to have issues with larger numbers for me, this works well for larger numbers using the same logic but with GMP:
gmp_strval(gmp_and($x, gmp_sub($x, 1))) == 0
use mod 2 to determine if a number is a power of 2
def is_power_of_2(n):
if n == 0:
return False
while n % 2 == 0:
n = n / 2
return n == 1
I tried to implement the same thing without bitwise operators. Finally, I ended up with
return (fmod(log($x, 2), 1) === 0.00)
(In PHP)
Math.log(x)/Math.log(2) == Math.floor(Math.log(x)/Math.log(2))
I am trying to set a variable based on some maths logic (to wrap specific html around elements).
I worked half the problem, to hit 0, 3, 6, 9, 12
if(($i % 3) == 0) { // blah }
Now I need to hit the following numbers, 2, 5, 8, 11, 14, etc
What possible maths operation could I do to hit this sequence?
if($i % 3 == 1)
if($i % 3 == 2)
Modulo returns the remainder, so when you match the 0, you get the 3rd, 6th, 9th, etc, because 0 is left in the division.
So just check for when 1 remains and 2 remains.
Along with Tor Valamo's answer you can notice the pattern of (3 * $i) - 1
(3*1)-1 = 2
(3*2)-1 = 5
(3*3)-1 = 8
...
if((($i-2) % 3) == 0) { // blah }
What exactly does this mean?
$number = ( 3 - 2 + 7 ) % 7;
It's the modulus operator, as mentioned, which returns the remainder of a division operation.
Examples: 3%5 returns 3, as 3 divided by 5 is 0 with a remainder of 3.
5 % 10 returns 5, for the same reason, 10 goes into 5 zero times with a remainder of 5.
10 % 5 returns 0, as 10 divided by 5 goes exactly 2 times with no remainder.
In the example you posted, (3 - 2 + 7) works out to 8, giving you 8 % 7, so $number will be 1, which is the remainder of 8/7.
It is the modulus operator:
$a % $b = Remainder of $a
divided by $b.
It is often used to get "one element every N elements". For instance, to only get one element each three elements:
for ($i=0 ; $i<10 ; $i++) {
if ($i % 3 === 0) {
echo $i . '<br />';
}
}
Which gets this output:
0
3
6
9
(Yeah, OK, $i+=3 would have done the trick; but this was just a demo.)
It is the modulus operator. In the statement $a % $b the result is the remainder when $a is divided by $b
Using this operator one can easily calculate odd or even days in month for example, if needed for schedule or something:
<?php echo (date(j) % 2 == 0) ? 'Today is even date' : 'Today is odd date'; ?>
% means modulus.
Modulus is the fancy name for "remainder after divide" in mathematics.
(numerator) mod (denominator) = (remainder)
In PHP
<?php
$n = 13;
$d = 7
$r = "$n % $d";
echo "$r is ($n mod $d).";
?>
In this case, this script will echo
6 is (13 mod 7).
Where $r is for the remainder (answer), $n for the numerator and $d for the denominator. The modulus operator is commonly used in public-key cryptography due to its special characteristic as a one-way function.
Since so many people say "modulus finds the remainder of the divisor", let's start by defining exactly what a remainder is.
In mathematics, the remainder is the amount "left over" after
performing some computation. In arithmetic, the remainder is the
integer "left over" after dividing one integer by another to produce
an integer quotient (integer division).
See: http://en.wikipedia.org/wiki/Remainder
So % (integer modulus) is a simple way of asking, "How much of the divisor is left over after dividing?"
To use the OP's computation of (3 - 2 + 7) = 8 % 7 = 1:
It can be broken down into:
(3 - 2 + 7) = 8
8 / 7 = 1.143 #Rounded up
.143 * 7 = 1.001 #Which results in an integer of 1
7 can go into 8 1 time with .14 of 7 leftover
That's all there is to it. I hope this helps to simplify how exactly modulus works.
Additional examples using different divisors with 21.
Breakdown of 21 % 3 = 0:
21 / 3 = 7.0
3 * 0 = 0
(3 can go into 21 7 times with 0 of 3 leftover)
Breakdown of 21 % 6 = 3:
21 / 6 = 3.5
.5 * 6 = 3
(6 can go into 21 3 times with .5 of 6 leftover)
Breakdown of 21 % 8 = 5:
21 / 8 = 2.625
.625 * 8 = 5
(8 can go into 21 2 times with .625 of 8 leftover)