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

 

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:
Posted in

Leave a Reply