divide-by-zero

Ignore divide by 0 warning in NumPy

蹲街弑〆低调 提交于 2019-11-27 01:46:45
问题 I have a function for statistic issues: import numpy as np from scipy.special import gamma as Gamma def Foo(xdata): ... return x1 * ( ( #R is a numpy vector ( ((R - x2)/beta) ** (x3 -1) ) * ( np.exp( - ((R - x2) / x4) ) ) / ( x4 * Gamma(x3)) ).real ) Sometimes I get from the shell the following warning: RuntimeWarning: divide by zero encountered in... I use the numpy isinf function to correct the results of the function in other files, so I do not need this warning. Is there a way to ignore

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

耗尽温柔 提交于 2019-11-26 22:22:37
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 throwing an exception? Is this by design or is it a bug? Just for kicks, I did this in VB.NET as well, with

Is 1/0 a legal Java expression?

落爺英雄遲暮 提交于 2019-11-26 20:20:33
The following compiles fine in my Eclipse: final int j = 1/0; // compiles fine!!! // throws ArithmeticException: / by zero at run-time Java prevents many "dumb code" from even compiling in the first place (e.g. "Five" instanceof Number doesn't compile!), so the fact this didn't even generate as much as a warning was very surprising to me. The intrigue deepens when you consider the fact that constant expressions are allowed to be optimized at compile time: public class Div0 { public static void main(String[] args) { final int i = 2+3; final int j = 1/0; final int k = 9/2; } } Compiled in

c++ division by 0

回眸只為那壹抹淺笑 提交于 2019-11-26 16:34:48
问题 I am running long simulations. I record the results into a vector to compute statistics about the data. I realized that, in theory, those samples could be the result of a division by zero; this is only theoretical, I am pretty sure it's not the case. In order to avoid rerunning the simulation after modifying the code, I was wondering what happens in that case. Would I be able to realize whether a division by 0 has occurred or not? Will I get error messages? (Exceptions are not being handled

Best way to prevent/handle divide by 0 in javascript

耗尽温柔 提交于 2019-11-26 16:07:23
问题 What is the best way to prevent divide by 0 in javascript that is accepting user inputs. If there is no particular way to achieve this what would be the best way to handle such a situation so as to not prevent other scripts from executing? Any insights are much appreciated. 回答1: There is no way to do that with the normal / and /= operators. The best way to do what you want is with guards: function notZero(n) { n = +n; // Coerce to number. if (!n) { // Matches +0, -0, NaN throw new Error(

TSQL divide by zero encountered despite no columns containing 0

一个人想着一个人 提交于 2019-11-26 15:29:44
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 joins ]... WHERE SomeCondition = SomeValue Running this statement errors with the above mentioned error

How to return 0 with divide by zero

删除回忆录丶 提交于 2019-11-26 14:23:32
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.seterr() can return values upon errors. Does anyone have any other suggestions on how I could get the best out

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

僤鯓⒐⒋嵵緔 提交于 2019-11-26 13:47:32
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: Integer division by zero", and float divide by zero as 0xC000008E "Floating-point division by zero" (when

Can't Mod Zero?

有些话、适合烂在心里 提交于 2019-11-26 13:47:00
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)? 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). It's implementation-defined. The spec says (§5.6/4), [...] If both operands are nonnegative then the

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

放肆的年华 提交于 2019-11-26 08:27:26
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 ignore it when it comes to double s? Kris The result of division by zero is, mathematically speaking,