Detect division by zero
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Write a function to detect a divide by zero error without checking if the denominator is zero.
D
The following checks a floating number's normality by Floating Point Comparisons in D.
For integral type, either traps exception or pre-checks zero denominator.
module divzero ; import std.stdio ; import std.format ; // check if unordered (NaN) bool isNaN(T)(T a) { return a !<>= T.min ; } // check for +ve & -ve infinity bool isInf(T)(T a) { return a == T.infinity || -a == T.infinity ; } // check if neither NaN nor Inf bool isNormal(T)(T a) { return !(a !<> T.infinity || -a !<> T.infinity) ; } string divCheck(T)(T numer, T denom) { T result ; string msg = "" ; static if (is(T : long)) { // Integral Type try { result = numer / denom ; } catch(Exception e) { msg = "! " ~ e.msg ~"(by Exception)" ; result = T.max ; } } else { // Float Type result = numer / denom ; if(isNormal(numer) && isInf(result)) msg = "! Division by Zero" ; else if (!isNormal(result)) { if (isNaN(numer)) msg = "! NaN numerator" ; else if(isNaN(denom)) msg = "! NaN denominator" ; else if(isInf(numer)) msg = "! Inf numerator" ; else msg = "! <should never displayed this>" ; } } return std.format.format("%5s %s", std.format.format("%1.1g", cast(real)result), msg) ; } void main() { writefln("Div with Check") ; writefln("int 1/ 0 : %s", divCheck(1, 0)) ; writefln("ubyte 1/ 0 : %s", divCheck(cast(ubyte)1, cast(ubyte)0)) ; writefln("real 1/ 0 : %s", divCheck(1.0L, 0.0L)) ; writefln("real -1/ 0 : %s", divCheck(-1.0L, 0.0L)) ; writefln() ; writefln("real -4/-2 : %s", divCheck(-4.0L,-2.0L)) ; real inf = -1.0L / 0.0L ; // make an infinity float writefln("real 2/-inf: %s", divCheck(2.0L, inf)) ; writefln() ; writefln("real -inf/-2 : %s", divCheck(inf, -2.0L)) ; writefln("real +inf/-2 : %s", divCheck(real.infinity, -2.0L)) ; writefln("real nan/-2 : %s", divCheck(real.nan, -2.0L)) ; writefln("real -2/ nan: %s", divCheck(-2.0L, real.nan)) ; writefln("real inf/ inf: %s", divCheck(real.infinity, real.infinity)) ; writefln("real nan/ nan: %s", divCheck(real.nan, real.nan)) ; }
Output:
Div with Check int 1/ 0 : 2e+09 ! Integer Divide by Zero(by Exception) ubyte 1/ 0 : 3e+02 ! Integer Divide by Zero(by Exception) real 1/ 0 : inf ! Division by Zero real -1/ 0 : -inf ! Division by Zero real -4/-2 : 2 real 2/-inf: -0 real -inf/-2 : inf ! Inf numerator real +inf/-2 : -inf ! Inf numerator real nan/-2 : nan ! NaN numerator real -2/ nan: nan ! NaN denominator real inf/ inf: -nan ! Inf numerator real nan/ nan: nan ! NaN numerator
Java
Two ways to accomplish this task are presented here. They each return true if there is a division by zero or if Double.POSITIVE_INFINITY is used as a numerator.
One way to do this check in Java is to use the isInfinite function from the Double class:
public static boolean infinity(double numer, double denom){ return Double.isInfinite(numer/denom); }
Another way is to use the ArithmeticException as a check (which is not preferred because it expects an exception):
public static boolean except(double numer, double denom){ try{ int dummy = (int)numer / (int)denom;//ArithmeticException is only thrown from integer math return false; }catch(ArithmeticException e){return true;} }