difference between php integer division and c integer division

We know the result of C integer division is an integer: 1/2=0, 1/3=0, 3/2=1. To get the remainder of integer division, you need to use %. To get the float result of C integer division, you should convert the numerator or denominator or both to float/double before doing the division such as 1.0/2, 1/3.0, 3.0/2.0.

On the other hand, php integer division result is a double, i.e., 1/2=0.5. If you want php division integer result, you need to use the intval function such as intval(1/2)=0. This is like the C integer division. You may think the floor function can achieve the same. This is only true for positive result. For negative division result, intval is not the same as floor. For example, intval(-1/2)=0 while floor(-1/2)=-1. You can use the round function to round up/down the php integer division result: round(1/2)=1, round(1/3)=0, round(-1/2)=-1, round(-1/3)=0.

reference:https://stackoverflow.com/questions/12832557/divide-integer-and-get-integer-value

 

If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!
Posted in

Leave a Reply