Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Division by zero error while using modulus

Tags:

php

Modulus operator is supposed to show the remainder. Like for echo(34%100) outputs 34. But why do i get a "Division by zero" error for this code echo(34%4294967296)

like image 826
Nok Imchen Avatar asked Aug 06 '13 00:08

Nok Imchen


People also ask

What is the error when you divide by zero?

error when a number is divided by zero (0). It happens when you enter a simple formula like =5/0, or when a formula refers to a cell that has 0 or is blank, as shown in this picture. To correct the error, do any of the following: Make sure the divisor in the function or formula isn't zero or a blank cell.

How do you fix ZeroDivisionError integer division or modulo by zero?

Conclusion # The Python "ZeroDivisionError: integer division or modulo by zero" occurs when we use the modulo % operator with an integer and a zero. To solve the error, figure out where the 0 comes from and correct the assignment.

How do you get rid of the divide by zero error in Python?

The Python "ZeroDivisionError: float division by zero" occurs when we try to divide a floating-point number by 0 . To solve the error, use an if statement to check if the number you are dividing by is not zero, or handle the error in a try/except block.

How can we avoid divide by zero error in php?

What should that user's ratio be in this case? The most sane would be to check if the divisor is zero before doing the division. Otherwise, you could create a custom function for division like mydivide() and handle the zero case there.


1 Answers

4294967296 is 2^32 and cannot be represented as 32 bit number - it wraps back to 0. If you use 64-bit version of PHP, it may work.

You might be able to use floating point modulus fmod to get what you want without overflowing.

like image 193
mvp Avatar answered Nov 13 '22 20:11

mvp