Detect division by zero

From Rosetta Code
Jump to: navigation, search
Task
Detect division by zero
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.

Contents

[edit] ABAP

report zdiv_zero
data x type i.
try.
x = 1 / 0.
catch CX_SY_ZERODIVIDE.
write 'Divide by zero.'.
endtry.
 

[edit] Ada

-- Divide By Zero Detection
 
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
 
procedure Divide_By_Zero is
Fnum : Float := 1.0;
Fdenom : Float := 0.0;
Fresult : Float;
Inum : Integer := 1;
Idenom : Integer := 0;
Iresult : Integer;
begin
begin
Put("Integer divide by zero: ");
Iresult := Inum / Idenom;
Put(Item => Iresult);
exception
when Constraint_Error =>
Put("Division by zero detected.");
end;
New_Line;
Put("Floating point divide by zero: ");
Fresult := Fnum / Fdenom;
if Fresult > Float'Last then
Put("Division by zero detected (infinite value).");
else
Put(Item => Fresult, Aft => 9, Exp => 0);
end if;
New_Line;
end Divide_By_Zero;
Output:
Integer divide by zero: Division by zero detected.
Floating point divide by zero: Division by zero detected (infinite value).

[edit] Aime

integer
divide(integer n, integer d)
{
return n / d;
}
 
integer
can_divide(integer n, integer d)
{
return !trap(divide, n, d);
}
 
integer
main(void)
{
if (!can_divide(9, 0)) {
o_text("Division by zero.\n");
}
 
return 0;
}
Output:
Division by zero.

The Aime interpreter reports execution errors by default, printing on standard error:

aime: can_divide: 4: division by zero

[edit] ALGOL 68

The USSR's ALGOL 68 had a "GOST 27975-88 Programming language ALGOL 68 extended (Язык программирования АЛГОЛ 68 расширенный)" that included additional keywords on, exception, raise. This was an extension, and probably made only an appearance in the Leningrad compiler (Алгола 68 Ленинград).

The following code sample implements zero division, without using language extensions or access to hardware interrupts.

Translation of: C
Works with: ALGOL 68 version Standard - no extensions to language used
Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386
Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386
PROC raise exception= ([]STRING args)VOID: (
put(stand error, ("Exception: ",args, newline));
stop
);
 
PROC raise zero division error := VOID:
raise exception("integer division or modulo by zero");
 
PROC int div = (INT a,b)REAL: a/b;
PROC int over = (INT a,b)INT: a%b;
PROC int mod = (INT a,b)INT: a%*b;
 
BEGIN
OP / = (INT a,b)REAL: ( b = 0 | raise zero division error; SKIP | int div (a,b) );
OP % = (INT a,b)INT: ( b = 0 | raise zero division error; SKIP | int over(a,b) );
OP %* = (INT a,b)INT: ( b = 0 | raise zero division error; SKIP | int mod (a,b) );
 
PROC a different handler = VOID: (
put(stand error,("caught division by zero",new line));
stop
);
 
INT x:=1, y:=0;
raise zero division error := a different handler;
print(x/y)
END
Output:
caught division by zero

[edit] AutoHotkey

ZeroDiv(num1, num2) {
If ((num1/num2) != "")
MsgBox % num1/num2
Else
MsgBox, 48, Warning, The result is not valid (Divide By Zero).
}
ZeroDiv(0, 3) ; is ok
ZeroDiv(3, 0) ; divize by zero alert

[edit] BASIC

[edit] Applesoft BASIC

The error code for division by zero is 133. There is a good overview of Applesoft ONERR GOTO handling here: http://newsgroups.derkeiler.com/Archive/Comp/comp.sys.apple2.programmer/2010-04/msg00000.html

 100  REM TRY
110 ONERR GOTO 200
120 D = - 44 / 0
190 END
200 REM CATCH
210 E = PEEK (222) < > 133
220 POKE 216,0: REM ONERR OFF
230 IF E THEN RESUME
240 CALL - 3288: REM RECOVER
250 PRINT "DIVISION BY ZERO"
 

[edit] BBC BASIC

      PROCdivide(-44, 0)
PROCdivide(-44, 5)
PROCdivide(0, 5)
PROCdivide(5, 0)
END
 
DEF PROCdivide(numerator, denominator)
ON ERROR LOCAL IF FALSE THEN
REM 'Try' clause:
PRINT numerator / denominator
ELSE
REM 'Catch' clause:
CASE ERR OF
WHEN 18: PRINT "Division by zero"
WHEN 20: PRINT "Number too big"
OTHERWISE RESTORE LOCAL : ERROR ERR, REPORT$
ENDCASE
ENDIF
ENDPROC

[edit] Liberty BASIC

result = DetectDividebyZero(1, 0)
 
 
Function DetectDividebyZero(a, b)
On Error GoTo [Error]
DetectDividebyZero= (a/ b)
Exit Function
[Error]
If Err = 11 Then '11 is the error number raised when divide by zero occurs
Notice "Divide by Zero Detected!"
End If
End Function

[edit] Locomotive Basic

10 ON ERROR GOTO 60
20 PRINT 2/3
30 PRINT 3/5
40 PRINT 4/0
50 END
60 IF ERR=11 THEN PRINT "Division by zero in line"ERL:RESUME 50
Output:
 0.666666667 
 0.6 
Division by zero in line 40 

[edit] PureBasic

PureBasic can be compiled with the OnError library included which gives a way to track program errors without losing speed, doing so gives support for the following functions;

  • ErrorAddress()
  • ErrorCode()
  • ErrorFile()
  • ErrorLine()
  • ErrorMessage()
  • ErrorRegister()
  • ErrorTargetAddress()
  • ExamineAssembly()
  • InstructionAddress()
  • InstructionString()
  • NextInstruction()
  • OnErrorCall()
  • OnErrorDefault()
  • OnErrorExit()
  • OnErrorGoto()
  • RaiseError()

This way the final version of a program can still intercept program errors and provide some function, or information about the error to the user so he can report it back to the developer.


With Integers & OnError Library

;Set up a Procedure to handle any Error
Procedure MyErrorHandler()
Define txt$="The following error happened."+#CRLF$+ ErrorMessage()+"at line "+Str(ErrorLine())
MessageRequester("OnError test", txt$)
EndProcedure
 
; Tell where to go if an Error happens
OnErrorCall(@MyErrorHandler())
 
;Now, do something very stupid so that we may see an Error...
Repeat
A=Random(100)/Random(100)
ForEver

OnError.png


With Floats, and without OnError library

Define.d a, b
Debug a/b

Results in; -1.#IND

[edit] Run BASIC

on error goto [error]
a = 1 / 0
wait
 
[error] ' error 11 is division by zero err number
If err = 11 Then print "Division by Zero"
wait

[edit] TI-89 BASIC

1/0 = undef is true.

[edit] C

[edit]
Library: POSIX

Some systems will raise SIGFPE if a program divides by zero.

#include <limits.h>	/* INT_MIN */
#include <setjmp.h> /* siglongjmp(), sigsetjmp() */
#include <stdio.h> /* perror(), printf() */
#include <stdlib.h> /* exit() */
#include <signal.h> /* sigaction(), sigemptyset() */
 
static sigjmp_buf fpe_env;
 
/*
* This SIGFPE handler jumps to fpe_env.
*
* A SIGFPE handler must not return, because the program might retry
* the division, which might cause an infinite loop. The only safe
* options are to _exit() the program or to siglongjmp() out.
*/

static void
fpe_handler(int signal, siginfo_t *w, void *a)
{
siglongjmp(fpe_env, w->si_code);
/* NOTREACHED */
}
 
/*
* Try to do x / y, but catch attempts to divide by zero.
*/

void
try_division(int x, int y)
{
struct sigaction act, old;
int code;
/*
* The result must be volatile, else C compiler might delay
* division until after sigaction() restores old handler.
*/

volatile int result;
 
/*
* Save fpe_env so that fpe_handler() can jump back here.
* sigsetjmp() returns zero.
*/

code = sigsetjmp(fpe_env, 1);
if (code == 0) {
/* Install fpe_handler() to trap SIGFPE. */
act.sa_sigaction = fpe_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
if (sigaction(SIGFPE, &act, &old) < 0) {
perror("sigaction");
exit(1);
}
 
/* Do division. */
result = x / y;
 
/*
* Restore old hander, so that SIGFPE cannot jump out
* of a call to printf(), which might cause trouble.
*/

if (sigaction(SIGFPE, &old, NULL) < 0) {
perror("sigaction");
exit(1);
}
 
printf("%d / %d is %d\n", x, y, result);
} else {
/*
* We caught SIGFPE. Our fpe_handler() jumped to our
* sigsetjmp() and passes a nonzero code.
*
* But first, restore old handler.
*/

if (sigaction(SIGFPE, &old, NULL) < 0) {
perror("sigaction");
exit(1);
}
 
/* FPE_FLTDIV should never happen with integers. */
switch (code) {
case FPE_INTDIV: /* integer division by zero */
case FPE_FLTDIV: /* float division by zero */
printf("%d / %d: caught division by zero!\n", x, y);
break;
default:
printf("%d / %d: caught mysterious error!\n", x, y);
break;
}
}
}
 
/* Try some division. */
int
main()
{
try_division(-44, 0);
try_division(-44, 5);
try_division(0, 5);
try_division(0, 0);
try_division(INT_MIN, -1);
return 0;
}
Output:
using OpenBSD/amd64:
-44 / 0: caught division by zero!
-44 / 5 is -8
0 / 5 is 0
0 / 0: caught division by zero!
-2147483648 / -1: caught division by zero!

The last line is a mistake: the system confused an overflow (INT_MIN / -1 would be INT_MAX + 1) with division by zero and raised SIGFPE. The system normally ignores overflow.

[edit] C#

Works with: int, long, decimal

The floating point types (float, double) don't raise an exception, but return the values Infinity or NaN as appropriate.

using System;
 
namespace RosettaCode {
class Program {
static void Main(string[] args) {
int x = 1;
int y = 0;
try {
int z = x / y;
} catch (DivideByZeroException e) {
Console.WriteLine(e);
}
 
}
}
}

[edit] Clojure

After catching the ArithmeticException, print the error message, and then try and recover by returning some meaningful value. In this case, if x > 0, return +inf, if 0, NaN, otherwise -inf.

(defn safe-/ [x y]
(try (/ x y)
(catch ArithmeticException _
(println "Division by zero caught!")
(cond (> x 0) Double/POSITIVE_INFINITY
(zero? x) Double/NaN
:else Double/NEGATIVE_INFINITY) )))

[edit] COBOL

DIVIDE foo BY bar GIVING foobar
ON SIZE ERROR
DISPLAY "Division by zero detected!"
END-DIVIDE

[edit] Common Lisp

(handler-case (/ x y)
(division-by-zero () (format t "division by zero caught!~%")))

[edit] D

import std.stdio, std.string, std.math, std.traits;
 
string divCheck(T)(in T numer, in T denom)
if (isIntegral!T || isFloatingPoint!T) {
Unqual!(typeof(numer / denom)) result;
string msg;
 
static if (isIntegral!T) {
try {
result = numer / denom;
} catch(Error e) {
msg = "| " ~ e.msg ~ " (by Error)";
result = T.max;
}
} else { // Floating Point Type.
result = numer / denom;
if (numer.isNormal && result.isInfinity) {
msg = "| Division by Zero";
} else if (result != 0 && !result.isNormal) {
if (numer.isNaN)
msg = "| NaN numerator";
else if (denom.isNaN)
msg = "| NaN denominator";
else if (numer.isInfinity)
msg = "| Inf numerator";
else
msg = "| NaN (Zero Division by Zero)";
}
}
 
return format("%5s %s", format("%1.1g", real(result)), msg);
}
 
void main() {
writeln("Division with check:");
writefln("int 1/ 0:  %s", divCheck(1, 0));
writefln("ubyte 1/ 0:  %s", divCheck(ubyte(1), ubyte(0)));
writefln("real 1/ 0:  %s", divCheck(1.0L, 0.0L));
writefln("real -1/ 0:  %s", divCheck(-1.0L, 0.0L));
writefln("real 0/ 0:  %s", divCheck(0.0L, 0.0L));
writeln;
writefln("real -4/-2:  %s", divCheck(-4.0L,-2.0L));
writefln("real 2/-inf: %s", divCheck(2.0L, -real.infinity));
writeln;
writefln("real -inf/-2:  %s", divCheck(-real.infinity, -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 nan/ 0:  %s", divCheck(real.nan, 0.0L));
writefln("real inf/ inf: %s",
divCheck(real.infinity, real.infinity));
writefln("real nan/ nan: %s", divCheck(real.nan, real.nan));
}
Output:
Division with check:
int     1/ 0:   2e+09 | Integer Divide by Zero (by Error)
ubyte   1/ 0:   3e+02 | Integer Divide by Zero (by Error)
real    1/ 0:     inf | Division by Zero
real   -1/ 0:    -inf | Division by Zero
real    0/ 0:    -nan | NaN (Zero 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  nan/ 0:     nan | NaN numerator
real  inf/ inf:  -nan | Inf numerator
real  nan/ nan:   nan | NaN numerator

[edit] Déjà Vu

divcheck x y:
true
try:
drop / x y
catch value-error:
not
 
if divcheck 1 0:
 !print "Okay"
else:
 !print "Division by zero"
Output:
Division by zero

[edit] Delphi

program DivideByZero;
 
{$APPTYPE CONSOLE}
 
uses SysUtils;
 
var
a, b: Integer;
begin
a := 1;
b := 0;
try
WriteLn(a / b);
except
on e: EZeroDivide do
Writeln(e.Message);
end;
end.

[edit] E

def divide(numerator, denominator) {
def floatQuotient := numerator / denominator
if (floatQuotient.isNaN() || floatQuotient.isInfinite()) {
return ["zero denominator"]
} else {
return ["ok", floatQuotient]
}
}

[edit] ECL

Division by zero defaults to generating a zero result (0), rather than reporting a "divide by zero" error. This avoids invalid or unexpected data aborting a long job. The default behavior can be changed using #OPTION.

Evaluate to zero - default behavior

 
DBZ(REAL8 Dividend,INTEGER8 Divisor) := Quotient/Divisor;
 
#option ('divideByZero', 'zero');
DBZ(10,0); //returns 0.0
 

Stop and report a division by zero error:

 
DBZ(REAL8 Dividend,INTEGER8 Divisor) := Quotient/Divisor;
#option ('divideByZero', 'fail');
DBZ(10,0); //returns error message "Error: System error: -1: Division by zero (0, 0), -1,"
 

Returns "nan":

 
DBZ(REAL8 Dividend,INTEGER8 Divisor) := Quotient/Divisor;
#option ('divideByZero', 'nan');
DBZ(10,0); //returns 'nan'
 
/* NOTE: This is only currently supported for real numbers. Division by zero creates a quiet NaN,
which will propogate through any real expressions it is used in.
You can use NOT ISVALID(x) to test if the value is a NaN.
Integer and decimal division by zero continue to return 0.
*/
 


[edit] Eiffel

Works with: SmartEiffel
version 2.4

In a file called main.e:

class MAIN
creation main
feature main is
local
x, y: INTEGER;
retried: BOOLEAN;
do
x := 42;
y := 0;
 
if not retried then
io.put_real(x / y);
else
print("NaN%N");
end
rescue
print("Caught division by zero!%N");
retried := True;
retry
end
end

Note: The "rescue" statement catches every exception.

[edit] Ela

open core number
 
x /. y = try Some (x `div` y) with
_ = None
 
(12 /. 2, 12 /. 0)

Output:

(Some 6, None)

Of course the cleanest way to implement the safe division function is through pattern matching:

x /. 0 = None
x /. y = Some (x / y)

But it doesn't satisfy the task.

[edit] Erlang

div_check(X,Y) ->
case catch X/Y of
{'EXIT',_} -> true;
_ -> false
end.

[edit] F#

let detectDivideZero (x : int) (y : int):int option =
try
Some(x / y)
with
| :? System.ArithmeticException -> None
 
 
printfn "12 divided by 3 is %A" (detectDivideZero 12 3)
printfn "1 divided by 0 is %A" (detectDivideZero 1 0)

Output:

12 divided by 3 is Some 4
1 divided by 0 is null

[edit] Factor

USE: math.floats.env
 
: try-div ( a b -- )
'[ { +fp-zero-divide+ } [ _ _ /f . ] with-fp-traps ] try ;
( scratchpad ) 1 2 try-div
0.5
( scratchpad ) 1 0 try-div
Floating point trap

Type :help for debugging help.

[edit] Fancy

def divide: x by: y {
try {
x / y
} catch DivisionByZeroError => e {
e message println # prints error message
}
}
 

[edit] Forth

: safe-/ ( x y -- x/y )
['] / catch -55 = if cr ." divide by zero!" 2drop 0 then ;

[edit] Go

Detection on integers by recovering from a panic:

package main
 
import "fmt"
 
func divCheck(x, y int) (q int, ok bool) {
defer func() {
recover()
}()
q = x / y
return q, true
}
 
func main() {
fmt.Println(divCheck(3, 2))
fmt.Println(divCheck(3, 0))
}

Output:

1 true
0 false

[edit] Groovy

In Groovy, the float and double types follow IEEE numeric formats and rules. Here is a solution for double:

def dividesByZero = { double n, double d ->
assert ! n.infinite : 'Algorithm fails if the numerator is already infinite.'
(n/d).infinite || (n/d).naN
}

Test program:

((3d)..(0d)).each { i ->
((2d)..(0d)).each { j ->
println "${i}/${j} divides by zero? " + dividesByZero(i,j)
}
}

Output:

3.0/2.0 divides by zero? false
3.0/1.0 divides by zero? false
3.0/0.0 divides by zero? true
2.0/2.0 divides by zero? false
2.0/1.0 divides by zero? false
2.0/0.0 divides by zero? true
1.0/2.0 divides by zero? false
1.0/1.0 divides by zero? false
1.0/0.0 divides by zero? true
0.0/2.0 divides by zero? false
0.0/1.0 divides by zero? false
0.0/0.0 divides by zero? true

[edit] Haskell

import qualified Control.Exception as C
check x y = C.catch (x `div` y `seq` return False)
(\_ -> return True)

[edit] HicEst

FUNCTION zero_divide(num, denom)
XEQ( num// "/" // denom, *99) ! on error jump to label 99
zero_divide = 0 ! division OK
RETURN
 
99 zero_divide = 1
END
zero_divide(0, 1)         returns 0 (false)
zero_divide( 1, 3-2-1 ) returns 1 (true)

[edit] IDL

if not finite( <i>expression</i> ) then ...

[edit] Icon and Unicon

Setting &error to a non-zero number traps errors and converts then to failures. Division by zero generates error 201

procedure main()
&error := 1
udef := 1 / 0 | stop("Run-time error ", &errornumber, " : ", &errortext," in line #",&line," - converted to failure")
end
Sample Output:
Run-time error 201 : division by zero in line #3 - converted to failure

[edit] J

Generally, this task should be accomplished in J using 0=DEN. Here we take an approach that's more comparable with the other examples on this page.

Divide by zero is not an error in J. It results in infinity which is represented by an underscore ( _ ) or negative infinity (represented by a double underescore) or complex values which can have infinities for the real and/or imaginary part., except that 0 divided by 0 is defined to have the result zero (mathematically speaking any number is a valid result for 0 divided by 0, because 0 times any number is zero).

See also the J Dictionary page on infinity

So, anyways, the task:

funnydiv=: 0 { [: (,:'division by zero detected')"_^:(_ e. |@,) (,>:)@:(,:^:(0<#@$))@[ %"_1 _ ]

This performs division and instead of returning the result returns the string 'division by zero detected' if a denominator was zero. Note that it also provides this result if a numerator was infinite, regardless of the denominator, but since there's no reasonable use for this implementation that's probably not a problem.

Examples:

   3 funnydiv 2
1.5
3 funnydiv 0
division by zero detected
0 funnydiv 0
division by zero detected
0 funnydiv 3
0
2 3 4 funnydiv 5
0.4 0.6 0.8

[edit] 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;}
}

[edit] JavaScript

JavaScript does not give an error on division by 0, and this is more useful than it is Mathematically correct. However, 0 divided by 0 will yield NaN, which is actually correct, since 0/0 is defined as "indeterminate". It may be better to return 0 or false in these situations, though, depending on the application (in JavaScript, 0 and false are the same thing):

function divByZero(dividend,divisor)
{
var quotient=dividend/divisor;
if(isNaN(quotient)) return 0; //Can be changed to whatever is desired by the programmer to be 0, false, or Infinity
return quotient; //Will return Infinity or -Infinity in cases of, for example, 5/0 or -7/0 respectively
}
alert(divByZero(0,0));

This will output "0" instead of "NaN". In this case, when checking against for true, the condition needs to be explicit ("===" rather than "==") because if divByZero(5,5) is used, this will return 1, which is the same as true when using "==".

[edit] jq

jq, like JavaScript, does not raise an error on division by 0, but unlike JavaScript, the result of division by zero is a number: either -1.7976931348623157e+308 or 1.7976931348623157e+308.

We can however define div(x;y) so that it raises an error, "NaN", if y equals 0:

def div(x;y): if y==0 then error("NaN") else x/y end;
In versions of jq since 1.4, we can then catch the error, as illustrated by the following snippet:
 
try div(3;0) catch if "NaN" then "div by 0 error detected" else . end

[edit] LabVIEW

This example is in need of improvement:
Clarify what happens with 0/0

This image is a VI Snippet, an executable image of LabVIEW code. The LabVIEW version is shown on the top-right hand corner. You can download it, then drag-and-drop it onto the LabVIEW block diagram from a file browser, and it will appear as runnable, editable code.
LabVIEW Detect division by zero.png


[edit] Lasso

define dividehandler(a,b) => {
(
#a->isNotA(::integer) && #a->isNotA(::decimal) ||
#b->isNotA(::integer) && #b->isNotA(::decimal)
) ? return 'Error: Please supply all params as integers or decimals'
protect => {
handle_error => { return 'Error: Divide by zero' }
local(x = #a / #b)
return #x
}
}
 
dividehandler(1,0)
Output:
Error: Divide by zero

[edit] Lua

Lua, like Javascript, does not error on DIVIDE-BY-ZERO, but returns infinity. So:

function div(a,b)
quot = a/b
if quot == 1/0 then error() end
return quot
end

[edit] M4

ifelse(eval(2/0),`',`detected divide by zero or some other error of some kind')

Output, with standard output labeled "==>" and error output labeled "error==>":

error==>divideby0.m4:1: m4: Divide by zero in eval: 2/0
==>detected divide by zero or some other error of some kind

[edit] Maple

By default numeric exceptions raise errors which cannot be trapped by the usual try...catch mechanism. Instead numeric exceptions may be controlled by custom handling procedures.

1/0; # Here is the default behavior.

Output:

Error, numeric exception: division by zero

Here is a simple custom handler being installed and used.

NumericEventHandler( ':-division_by_zero'
= proc() infinity; end proc ):
 
1/0;
 
NumericStatus(':-division_by_zero'); # We may check the status flag

Output:

                                  infinity

                                    true

Alternatively, the custom handler could issue a warning or clear the status flag for that exception, as well as return some particular value.

NumericEventHandler( ':-division_by_zero'
= proc()
WARNING("division by zero");
NumericStatus(':-division_by_zero'=false):
infinity;
end proc ):
 
1/0;
 
NumericStatus(':-division_by_zero');

Output:

Warning, division by zero
                                  infinity

                                    false

[edit] Mathematica

Check[2/0, Print["division by 0"], Power::infy]

[edit] MATLAB

function [isDividedByZero] = dividebyzero(numerator, denomenator)    
isDividedByZero = isinf( numerator/denomenator );
% If isDividedByZero equals 1, divide by zero occured.

[edit] Maxima

f(a, b) := block([q: errcatch(a / b)], if emptyp(q) then 'error else q[1]);
 
f(5, 6);
5 / 6
 
f(5, 0;)
'error

[edit] MAXScript

if not bit.isFinite (<i>expression</i>) then...

[edit] MUMPS

DIV(A,B) ;Divide A by B, and watch for division by zero
 ;The ANSI error code for division by zero is "M9".
 ;$ECODE errors are surrounded by commas when set.
NEW $ETRAP
SET $ETRAP="GOTO DIVFIX^ROSETTA"
SET D=(A/B)
SET $ETRAP=""
QUIT D
DIVFIX
IF $FIND($ECODE,",M9,")>1 WRITE !,"Error: Division by zero" SET $ECODE="" QUIT ""
QUIT "" ; Fall through for other errors
Output:
USER>W $$DIV^ROSETTA(1,2)
.5
USER>W $$DIV^ROSETTA(1,4)
.25
USER>W $$DIV^ROSETTA(1,0)
 
Error: Division by zero
USER>W $$DIV^ROSETTA(1,C)
 
W $$DIV^ROSETTA(1,C)
^
<UNDEFINED> *C

[edit] mIRC Scripting Language

var %n = $rand(0,1)
if ($calc(1/ %n) == $calc((1/ %n)+1)) {
echo -ag Divides By Zero
}
else {
echo -ag Does Not Divide By Zero
}

[edit]

;; Division by zero detection using CAREFULLY
;; The CAREFULLY clause exists in NetLogo since version 2.0
;; In prior versions of NetLogo, you must examine the divisor prior to performing the division.
;; The variables result, a, and b must all be previously created global, local, or agent -own'd variables.
;; NetLogo variables are dynamically typed, so we are assuming that a and b contain numbers.
;; (All numbers in NetLogo are double-precision floating-point numbers.)
;; However, even if not numbers, the result is still the same: the carefully clause will
;; supress the run-time error and run the "commands if error" block, setting result to false.
;; this false value can be detected, to alter the rest of the course of the code
;; This behavior is consistent with other NetLogo primitives, such as POSTIION, that report
;; FALSE, rather than a number, if the operation fails.
carefully
[ ;; commands to try to run
set result a / b
]
[ ;; commands to run if an error occurs in the previous block.
set result false
]
ifelse is-number? result
[ output-print (word a " / " b " = " result)
]
[ output-print (word a " / " b " is not calculable"
]

[edit] NetRexx

/* NetRexx */
options replace format comments java crossref symbols nobinary
 
method divide(dividend, divisor) public constant returns Rexx
do
quotient = dividend / divisor
catch exu = DivideException
exu.printStackTrace()
quotient = 'undefined'
catch exr = RuntimeException
exr.printStackTrace()
quotient = 'error'
end
return quotient
 
method main(args = String[]) public static
-- process input arguments and set sensible defaults
arg = Rexx(args)
parse arg dividend .',' divisor .
if dividend.length() = 0 then dividend = 1
if divisor.length() = 0 then divisor = 0
say dividend '/' divisor '=' divide(dividend, divisor)
return
 

Output:

netrexx.lang.DivideException: Divide by 0
	at netrexx.lang.Rexx.dodivide(Rexx.nrx:1778)
	at netrexx.lang.Rexx.OpDiv(Rexx.nrx:1674)
	at zz.divide(zz.nrx:20)
	at zz.main(zz.nrx:47)

[edit] Nimrod

# In debug builds division by zero exceptions are thrown by default, in release
# builds not. We can still enable them explicitly.
{.push overflowChecks: on.}
proc divCheck(x, y): bool =
try:
discard x div y
except EDivByZero:
return true
return false
{.pop.} # Restore default check settings
 
echo divCheck(2, 0)

[edit] OCaml

Detection on integers by catching an exception:

let div_check x y =
try
ignore (x / y);
false
with Division_by_zero ->
true

Detection on floats by checking for infiniteness:

let div_check x y =
classify_float (x /. y) = FP_infinite

[edit] Octave

Dividing by zero raises a warning (a warning does not stop the execution), not an error (and the given answer is Infinity), so it's not possible to use a try-catch construct; we can however check for the lastwarn if the answer is Inf.

d = 5/0;
if ( isinf(d) )
if ( index(lastwarn(), "division by zero") > 0 )
error("division by zero")
endif
endif

[edit] ooRexx

/* REXX **************************************************************
* program demonstrates detects and handles division by zero.
* translated from REXX:
* removed fancy error reporting (ooRexx does not support linesize)
* removed label Novalue (as novalue is not enabled there)
* 28.04.2013 Walter Pachl
*********************************************************************/

Signal on Syntax /*handle all REXX syntax errors. */
x = sourceline() /*being cute, x=size of this pgm.*/
y = x-x /*setting to zero the obtuse way.*/
z = x/y /* attempt to divide by 0 */
exit /* will not be reached */
 
Syntax:
Say 'Syntax raised in line' sigl
Say sourceline(sigl)
Say 'rc='rc '('errortext(rc)')'
Exit 12

Output:

Syntax raised in line 11
z = x/y                            /* attempt to divide by 0        */
rc=42 (Arithmetic overflow/underflow)

[edit] Oz

For integer division only.

try
{Show 42 div 0}
catch error(kernel(div0 ...) ...) then
{System.showInfo "Division by zero detected."}
end

[edit] PARI/GP

trap(,"division by 0",m/n)

[edit] Pascal

See Delphi

[edit] Perl

This function returns true iff its second argument is zero.

sub div_check
{local $@;
eval {$_[0] / $_[1]};
$@ and $@ =~ /division by zero/;}

[edit] Perl 6

sub div($a, $b){
my $r;
try {
$r = $a / $b;
CATCH {
say "tried to divide by zero !" if $! ~~ "Divide by zero";
}
}
return $r // fail;
}
 
say div(10,2); # 5
say div(1,0); # Inf, 1/0 constants are substituted for Infinity
say div(1, sin(0)); # undef, and prints "tried to divide by zero"
 

[edit] Using Multi Method Dispatch

multi div($a, $b){ return $a / $b }
multi div($a, $b where { $b == 0 }){
say 'lolicheatsyou';
return 1;
}
 
say div(1, sin(0)); # prints "lolicheatsyou" newline "1"
 

[edit] PHP

This function returns true iff its second argument is zero.

function div_check($x, $y) {
@trigger_error(''); // a dummy to detect when error didn't occur
@($x / $y);
$e = error_get_last();
return $e['message'] != '';
}
function div_check($x, $y) {
return @($x / $y) === FALSE; // works at least in PHP/5.2.6-3ubuntu4.5
}

[edit] PicoLisp

(catch '("Div/0") (/ A B))

[edit] PL/I

Proc DivideDZ(a,b) Returns(Float Bin(33)); 
Dcl (a,b,c) Float Bin(33);
On ZeroDivide GoTo MyError;
c=a/b;
Return(c);
MyError:
Put Skip List('Divide by Zero Detected!');
End DivideDZ;
 
xx=DivideDZ(1,0);

[edit] PL/SQL

FUNCTION divide(n1 IN NUMBER, n2 IN NUMBER)
RETURN BOOLEAN
IS
result NUMBER;
BEGIN
result := n1/n2;
RETURN(FALSE);
EXCEPTION
WHEN ZERO_DIVIDE THEN
RETURN(TRUE);
END divide;
divide(0,1) --false 
divide(1,0) --true, division by zero

[edit] Pure

Floating point division yields inf or nan values as appropriate (if the FPU supports IEEE 754):

> 1/0, -1/0, 0/0;
inf,-inf,nan

It's possible to check for these values as follows:

> inf_or_nan x = infp x || nanp x;
> map inf_or_nan [1/0, -1/0, 0/0];
[1,1,1]

In contrast, integer division by zero raises an exception which can be caught as follows:

> divide n m = catch (\_ -> "divide by 0") (n div m);
> divide 0 1;
0
> divide 1 0;
"divide by 0"

[edit] Python

def div_check(x, y):
try:
x / y
except ZeroDivisionError:
return True
else:
return False

[edit] R

Division by zero does not raise an error nor a warning. Division of a non-zero value by zero returns infinity. Division of zero by zero returns NaN; Whether the result is not finite can be checked:

d <- 5/0
if ( !is.finite(d) ) {
# it is Inf, -Inf, or NaN
}

[edit] Racket

In Racket, the division by zero exception can be caught directly:

 
#lang racket
 
(with-handlers ([exn:fail:contract:divide-by-zero?
(λ (e) (displayln "Divided by zero"))])
(/ 1 0))
 

[edit] REBOL

rebol [
Title: "Detect Divide by Zero"
Date: 2009-12-16
Author: oofoe
URL: http://rosettacode.org/wiki/Divide_by_Zero_Detection
]

 
; The 'try' word returns an error object if the operation fails for
; whatever reason. The 'error?' word detects an error object and
; 'disarm' keeps it from triggering so I can analyze it to print the
; appropriate message. Otherwise, any reference to the error object
; will stop the program.
 
div-check: func [
"Attempt to divide two numbers, report result or errors as needed."
x y
/local result
] [
either error? result: try [x / y][
result: disarm result
print ["Caught" result/type "error:" result/id]
] [
print [x "/" y "=" result]
]
]
 
div-check 12 2 ; An ordinary calculation.
div-check 6 0 ; This will detect divide by zero.
div-check "7" 0.0001 ; Other errors can be caught as well.

Output:

12 / 2 = 6
Caught math error: zero-divide
Caught script error: cannot-use

[edit] REXX

The task's requirements are to write a function, but this example program was written to solve the spirit of the requirement.
This version isn't really a function so much as it is a method.
Also, a function and a subroutine doesn't have that much of a distinction in the REXX language.

/*REXX program demonstrates  detects and handles  division by zero.     */
 
signal on syntax /*handle all REXX syntax errors. */
x = sourceline() /*being cute, x=size of this pgm.*/
y = x-x /*setting to zero the obtuse way.*/
z = x/y /*this'll do it, furrrr shurrre. */
exit /*We're kaput. Ja vohl ! */
 
/*───────────────────────────────error handling subroutines and others.─*/
err: if rc==42 then do; say; say /*1st, check for a specific error*/
say center(' division by zero is a no-no. ',79,'═')
say; say
exit 130
end
 
say; say; say center(' error! ',max(40,linesize()%2),"*"); say
do j=1 for arg(); say arg(j); say; end; say;
exit 13
 
novalue: syntax: call err 'REXX program' condition('C') "error",,
condition('D'),'REXX source statement (line' sigl"):",,
sourceline(sigl)

This REXX program makes use of   LINESIZE   REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
The   LINESIZE.REX   REXX program is included here ──► LINESIZE.REX.

output


════════════════════════ division by zero is a no-no. ═════════════════════════

[edit] Ruby

This only checks integer division by zero.

def div_check(x, y)
begin
x / y
rescue ZeroDivisionError
true
else
false
end
end

Ruby allows division by zero if either operand is a Float.

irb(main):010:0> div_check(5, 0)
=> true
irb(main):011:0> div_check(5.0, 0)
=> false

Starting with Ruby 1.9, Numeric#div raises ZeroDivisionError, whether or not an operand is a Float.

Works with: Ruby version 1.9
def div_check(x, y)
begin
x.div y
rescue ZeroDivisionError
true
else
false
end
end
irb(main):010:0> div_check(5, 0)
=> true
irb(main):011:0> div_check(5.0, 0)
=> true

[edit] Scala

Without the "println(result)" line, the result would not get calculated as it is not needed. The method would get optimized to always return false.

object DivideByZero extends Application {
 
def check(x: Int, y: Int): Boolean = {
try {
val result = x / y
println(result)
return false
} catch {
case x: ArithmeticException => {
return true
}
}
}
 
println("divided by zero = " + check(1, 0))
 
}

[edit] Seed7

Integer division by zero raises NUMERIC_ERROR. Floating point division by zero returns Infinity or -Infinity.

$ include "seed7_05.s7i";
include "float.s7i";
 
const proc: doDivide (in integer: numer, in integer: denom) is func
begin
block
writeln(numer <& " div " <& denom <& " = " <& numer div denom);
exception
catch NUMERIC_ERROR:
writeln("Division by zero detected.");
end block;
end func;
 
const proc: doDivide (in float: numer, in float: denom) is func
local
var float: quotient is 0.0;
begin
quotient := numer / denom;
if quotient <> Infinity and quotient <> -Infinity then
writeln(numer <& " / " <& denom <& " = " <& quotient);
else
writeln("Division by zero detected.");
end if;
end func;
 
const proc: main is func
begin
doDivide(10, 8);
doDivide(1, 0);
doDivide(10.0, 8.0);
doDivide(1.0, 0.0);
end func;

Output:

10 div 8 = 1
Division by zero detected.
10.0 / 8.0 = 1.25
Division by zero detected.

[edit] Slate

[ 1 / 0 ] on: Error do: [|:err| err return: PositiveInfinity].


[edit] Smalltalk

Works with: Squeak
Works with: Smalltalk/X
zeroDivide := [:aBlock |
[aBlock value. false] on: ZeroDivide do: [true].
].
 
"Testing"
zeroDivide value: [2/1] "------> false"
zeroDivide value: [2/0] "------> true"

of course, as ZeroDivide inherits from Error, you could also write [...] on: Error do: [...], thereby catching ANY error (as done in some other code examples here).

[edit] SNOBOL4

Works with: Macro Spitbol

Using setexit( ) to trap and ignore division by zero.

        define('zdiv(x,y)') :(zdiv_end)
zdiv &errlimit = 1; setexit(.ztrap)
zdiv = x / y :(return)
ztrap zdiv = ?(&errtype ? (14 | 262)) 'Division by zero' :s(continue)f(abort)
zdiv_end
 
* # Test and display
output = '1/1 = ' zdiv(1,1)  ;* Integers non-zero
output = '1.0/1.0 = ' zdiv(1.0,1.0)  ;* Reals non-zero
output = '1/0 = ' zdiv(1,0)  ;* Integers zero
output = '1.0/0.0 = ' zdiv(1.0,0.0)  ;* Reals zero
output = 'Zero checks complete'
end

Output:

1/1     = 1
1.0/1.0 = 1.
1/0     = Division by zero
1.0/0.0 = Division by zero
Zero checks complete

[edit] Standard ML

Detection on integers by catching an exception:

fun div_check (x, y) = (
ignore (x div y);
false
) handle Div => true

Detection on floats by checking for infiniteness:

fun div_check (x, y) =
not (Real.isFinite (x / y))

[edit] Tcl

proc div_check {x y} {
if {[catch {expr {$x/$y}} result] == 0} {
puts "valid division: $x/$y=$result"
} else {
if {$result eq "divide by zero"} {
puts "caught division by zero: $x/$y -> $result"
} else {
puts "caught another error: $x/$y -> $result"
}
}
}
 
foreach denom {1 0 foo} {
div_check 42 $denom
}
Output:
valid division: 42/1=42
caught division by zero: 42/0 -> divide by zero
caught another error: 42/foo -> can't use non-numeric string as operand of "/"
Works with: Tcl version 8.6

It is easier to trap such errors in Tcl 8.6, which has an additional control structure for exception processing:

proc div_check {x y} {
try {
puts "valid division: $x/$y=[expr {$x/$y}]"
} trap {ARITH DIVZERO} msg {
puts "caught division by zero: $x/$y -> $msg"
} trap {ARITH DOMAIN} msg {
puts "caught bad division: $x/$y -> $msg"
} on error msg {
puts "caught another error: $x/$y -> $msg"
}
}
 
foreach {num denom} {42 1 42 0 42.0 0.0 0 0 0.0 0.0 0 foo} {
div_check $num $denom
}
which produces the
Output:
valid division: 42/1=42
caught division by zero: 42/0 -> divide by zero
valid division: 42.0/0.0=Inf
caught division by zero: 0/0 -> divide by zero
caught bad division: 0.0/0.0 -> domain error: argument not in valid range
caught another error: 0/foo -> can't use non-numeric string as operand of "/"

As can be seen, division-by-zero is only signaled when performing integer division. Similarly, separate detection of values that would otherwise be IEEE NaN is only performed when doing floating-point division.

[edit] TXR

@(do (defun div-check (x y)
(catch (/ x y)
(numeric_error (msg)
'div-check-failed))))
@(bind good @(div-check 32 8))
@(bind bad @(div-check 42 0))

Run:

$ txr -B division-by-zero.txr
good="4.0"
bad="div-check-failed"

[edit] XPL0

GetErr is an intrinsic function that detects runtime errors, such as division by zero, as well as more than a dozen others. Normally runtime errors terminate a program and display an error message. However the Trap intrinsic is used here to prevent error termination, which enables the program to do its own error handling. This example displays "Divide by zero".

include c:\cxpl\codes;
int A, B;
[Trap(false); \turn off error trapping
B:= 1234/(A-A); \(error not detected at compile time)
if GetErr then Text(0, "Divide by zero");
]

[edit] Yorick

func div_check(x, y) {
if(catch(0x01))
return 1;
temp = x/y;
return 0;
}

[edit] zkl

fcn f(x,y){try{x/y}catch(MathError){println(__exception)}}
Output:
zkl: f(1,0)
MathError(INF (number is infinite), Int divide by zero)
zkl: f(1.0,0)
MathError(INF (number is infinite))
Personal tools
Namespaces

Variants
Actions
Community
Explore
Misc
Toolbox