divide-by-zero

Inconsistency in divide-by-zero behavior between different value types

孤人 提交于 2019-11-26 06:37:50
问题 Please consider the following code and comments: Console.WriteLine(1 / 0); // will not compile, error: Division by constant zero int i = 0; Console.WriteLine(1 / i); // compiles, runs, throws: DivideByZeroException double d = 0; Console.WriteLine(1 / d); // compiles, runs, results in: Infinity I can understand the compiler actively checking for division by zero constant and the DivideByZeroException at runtime but: Why would using a double in a divide-by-zero return Infinity rather than

PHP, How to catch a division by zero?

好久不见. 提交于 2019-11-26 05:22:34
I have a large mathematical expression that has to be created dynamically. For example, once I have parsed "something" the result will be a string like: "$foo+$bar/$baz"; . So, for calculating the result of that expression I'm using the eval function... something like this: eval("\$result = $expresion;"); echo "The result is: $result"; The problem here is that sometimes I get errors that says there was a division by zero, and I don't know how to catch that Exception. I have tried things like: eval("try{\$result = $expresion;}catch(Exception \$e){\$result = 0;}"); echo "The result is: $result";

TSQL divide by zero encountered despite no columns containing 0

喜夏-厌秋 提交于 2019-11-26 04:27:28
问题 I\'ve been trying to understand why I get a \"divide by zero encountered\" (Msg 8134) with my SQL query, but I must be missing something. I would like like to know the why for the specific case below, I am not looking for NULLIF , CASE WHEN... or similar as I already know about them (and can of course use them in a situation as the one below). I have an SQL statement with a computed column similar to SELECT TotalSize, FreeSpace, (FreeSpace / TotalSize * 100) FROM tblComputer ...[ couple of

Can't Mod Zero?

谁说我不能喝 提交于 2019-11-26 03:36:54
问题 Why is X % 0 an invalid expression? I always thought X % 0 should equal X. Since you can\'t divide by zero, shouldn\'t the answer naturally be the remainder, X (everything left over)? 回答1: The C++ Standard(2003) says in §5.6/4, [...] If the second operand of / or % is zero the behavior is undefined ; [...] That is, following expressions invoke undefined-behavior(UB): X / 0; //UB X % 0; //UB Note also that -5 % 2 is NOT equal to -(5 % 2) (as Petar seems to suggest in his comment to his answer)

Why doesn't Java throw an Exception when dividing by 0.0?

妖精的绣舞 提交于 2019-11-26 03:26:51
问题 I have code to calculate the percentage difference between 2 numbers - (oldNum - newNum) / oldNum * 100; - where both of the numbers are double s. I expected to have to add some sort of checking / exception handling in case oldNum is 0. However, when I did a test run with values of 0.0 for both oldNum and newNum, execution continued as if nothing had happened and no error was thrown. Running this code with int s would definitely cause an arithmetic division-by-zero exception. Why does Java

PHP, How to catch a division by zero?

可紊 提交于 2019-11-26 03:25:36
问题 I have a large mathematical expression that has to be created dynamically. For example, once I have parsed \"something\" the result will be a string like: \"$foo+$bar/$baz\"; . So, for calculating the result of that expression I\'m using the eval function... something like this: eval(\"\\$result = $expresion;\"); echo \"The result is: $result\"; The problem here is that sometimes I get errors that says there was a division by zero, and I don\'t know how to catch that Exception. I have tried

How to return 0 with divide by zero

白昼怎懂夜的黑 提交于 2019-11-26 03:09:17
问题 I\'m trying to perform an element wise divide in python, but if a zero is encountered, I need the quotient to just be zero. For example: array1 = np.array([0, 1, 2]) array2 = np.array([0, 1, 1]) array1 / array2 # should be np.array([0, 1, 2]) I could always just use a for-loop through my data, but to really utilize numpy\'s optimizations, I need the divide function to return 0 upon divide by zero errors instead of ignoring the error. Unless I\'m missing something, it doesn\'t seem numpy

How do I catch a numpy warning like it's an exception (not just for testing)?

戏子无情 提交于 2019-11-26 03:06:36
问题 I have to make a Lagrange polynomial in Python for a project I\'m doing. I\'m doing a barycentric style one to avoid using an explicit for-loop as opposed to a Newton\'s divided difference style one. The problem I have is that I need to catch a division by zero, but Python (or maybe numpy) just makes it a warning instead of a normal exception. So, what I need to know how to do is to catch this warning as if it were an exception. The related questions to this I found on this site were answered

On which platforms does integer divide by zero trigger a floating point exception?

心不动则不痛 提交于 2019-11-26 02:38:56
问题 In another question, someone was wondering why they were getting a \"floating point error\" when in fact they had an integer divide-by-zero in their C++ program. A discussion arose around this, with some asserting that floating point exceptions are in fact never raised for float divide by zero, but only arise on integer divide by zero. This sounds strange to me, because I know that: MSVC-compiled code on x86 and x64 on all Windows platforms reports an int divide by zero as \"0xc0000094: