PHP - Why floor() give me another result of sams calculation [duplicate] - php

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
for($i=0;$i<=15120;$i+=504){
echo ($i/60/8.4) . " - " .floor($i/60/8.4)."<br>";
}
Result (i make ** at problem) :
0 - 0
1 - 1
2 - 2
3 - 3
4 - 4
5 - 5
6 - 6
7 - 6
8 - 8
9 - 8
10 - 10
11 - 11
12 - 12
13 - 13
14 - 13
15 - 15
16 - 16
17 - 17
18 - 17
etc...
At start i think i apply "Floo" at all calculation but (for line "7-6") also $i = 3528 :
3528 / 60 = 58,8 == Floor ==> 58 / 8 = 7.25
floor of 7.25 =/= 6

it because of PHPs floating point precision
so your 8th calculation for example will not return 7, but something like 6.9999999999999999999999999999
as this is a double, it will be rounded to 7 on output, try this out:
$x = 6.9999999999999999999999999999999999999999999999999999999;
echo $x;
when you use floor() it will (correctly) round it down to 6

Related

Bitwise Left Shift operator [duplicate]

This question already has answers here:
What are bitwise shift (bit-shift) operators and how do they work?
(10 answers)
Closed 4 years ago.
$a = 23;
$res = $a << 4
print($res);
For the code snippet pasted above, the output is 368. How is it being calculated?
I expected 92.
a is 23
a in base 2 is 10111
so a in 8 bits is 00010111
Left shift 4 is 01110000
It is - 92
Can some body explain me ?
This is why it is returning 368 because $a << $b Shift the bits of $a $b steps to the left
23
256 128 64 32 16 8 4 2 1
X X X X 1 0 1 1 1
after $a << 4
368
256 128 64 32 16 8 4 2 1
1 0 1 1 1 0 0 0 0
Check bitwise operator manual here
Convert 23 in binary and it is 10111 and when you perform shift left bitwise operator, its value will be 101110000 and when you convert it to decimal it becomes 368.

7001 write 8? round(7001/1000)

I have to write numbers in groups of 1000.
so, if I have 7000, I want to write:
1 2 3 4 5 6 7
but If I have 7001 or more, I want to write:
1 2 3 4 5 6 7 8
php
$num = 22501;
$num = round($num/1000);
for ($i=1; $i<=$num; $i++){
echo $i;
}
if my number is 22501 I will write 1 to 23. but if it is 22001 it will write 1 to 22.
I want to write 1 to 23. how can I do this?

How to calculate personal number by date of birth using php

I want to make the calculation of personal number by date of birth.
The calculation is done in this manner:
Ex. 8 (day) +12 (month) + 1 + 9 + 7 + 1 (year) = 38 = 3 + 8 = 11 = 1 + 1 = 2
(the final number)
This final number must not be greater than nine.
So:
The first number comes is 38 greater than 9 and it should make 3 + 8
The second number comes is 11 greater than 9 it should make 1 + 1
The third number comes is 2 less than 9 so it is the final number.
Taking all these calculations should let out the number 2.
How can I get it with php calculation?
I suppose, you can split date to array. Then
$arr = array(8,12,1,9,7,1);
// sum array, split sum to array per digit untill more than 1 digit in sum
while (count($arr = str_split(array_sum($arr))) != 1) {}
echo $arr[0]; // 2

proper arrangement in foreach

$random = rand(4, 23);
$range = range(1, $random );
HI.. guys
I have a random range value here in foreach function i want to display with below
rules.. my aim is to display like a square box
if i get range 1 to 3 it has to display table like this
1 2
3
if range from 1 to 6
1 2 3
4 5 6
if range from 1 to 19
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19
get the ceil of the square root of the number of records, and then anytime you're at an index with a mod of that value that is equal to 0, start a new line. Since you already have $random something like:
$dim = ceil(sqrt($random));
foreach ($range as $index => $number) {
print $number;
if (!(($index + 1) % $dim)) {
print "\n";
}
else {
print " ";
}
}
May need some adjustment (I'm not in PHP mode atm) and also doesn't factor in the padding but that should be straightforward.

How does this php code return odd numbers? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Understanding PHP's & operator
I was just looking at array_filter() function doc and they had the following code to return odd numbers...
<?php
function odd($var)
{
// returns whether the input integer is odd
return($var & 1);
}
?>
Why does $var & 1 return odd number? how does that work?
& is bitwise and. It acts as a mask on the bits of $var. All odd numbers end with 1
no bit &1
1 001 1
2 010 0
3 011 1
4 100 0
5 101 1
6 110 0
7 111 1
You are using a bitwise function with always returns 1 when anded with an odd number.
A few examples:
11 = 3
01 = 1
----
01 = odd -- return 1 (true)
100 = 4
01 = 1
-----
000 = even -- return 0 (false)
One more:
10101 = 21
01 = 1
-------
00001 = odd -- return 1 (true)
That function return 1 if var is an odd number, 0 otherwise. "&" is the AND binary operator, so it considers the last binary digit of a number.
For example:
5 in binary is 101 -> 101 & 1 = 1 -> odd number.
8 in binary is 1000 -> 1000 & 1 = 0 -> even number.

Categories