Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
4
Task/Detect-division-by-zero/00-META.yaml
Normal file
4
Task/Detect-division-by-zero/00-META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Simple
|
||||
from: http://rosettacode.org/wiki/Detect_division_by_zero
|
||||
3
Task/Detect-division-by-zero/00-TASK.txt
Normal file
3
Task/Detect-division-by-zero/00-TASK.txt
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
;Task:
|
||||
Write a function to detect a ''divide by zero error'' without checking if the denominator is zero.
|
||||
<br><br>
|
||||
|
|
@ -0,0 +1 @@
|
|||
1 0 n:/ Inf? . cr
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
report zdiv_zero
|
||||
data x type i.
|
||||
try.
|
||||
x = 1 / 0.
|
||||
catch CX_SY_ZERODIVIDE.
|
||||
write 'Divide by zero.'.
|
||||
endtry.
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
begin
|
||||
% integer division procedure %
|
||||
% sets c to a divided by b, returns true if the division was OK, %
|
||||
% false if there was division by zero %
|
||||
logical procedure divideI ( integer value a, b; integer result c ) ;
|
||||
begin
|
||||
% set exception handling to allow integer division by zero to occur once %
|
||||
INTDIVZERO := EXCEPTION( false, 1, 0, false, "INTDIVZERO" );
|
||||
c := a div b;
|
||||
not XCPNOTED(INTDIVZERO)
|
||||
end divideI ;
|
||||
% real division procedure %
|
||||
% sets c to a divided by b, returns true if the division was OK, %
|
||||
% false if there was division by zero %
|
||||
logical procedure divideR ( long real value a, b; long real result c ) ;
|
||||
begin
|
||||
% set exception handling to allow realdivision by zero to occur once %
|
||||
DIVZERO := EXCEPTION( false, 1, 0, false, "DIVZERO" );
|
||||
c := a / b;
|
||||
not XCPNOTED(DIVZERO)
|
||||
end divideR ;
|
||||
integer c;
|
||||
real d;
|
||||
write( divideI( 4, 2, c ) ); % prints false as no exception %
|
||||
write( divideI( 5, 0, c ) ); % prints true as division by zero was detected %
|
||||
write( divideR( 4, 2, d ) ); % prints false as no exception %
|
||||
write( divideR( 5, 0, d ) ) % prints true as division by zero was detected %
|
||||
end.
|
||||
32
Task/Detect-division-by-zero/Ada/detect-division-by-zero.ada
Normal file
32
Task/Detect-division-by-zero/Ada/detect-division-by-zero.ada
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
-- 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 or Fresult < Float'First then
|
||||
Put("Division by zero detected (infinite value).");
|
||||
else
|
||||
Put(Item => Fresult, Aft => 9, Exp => 0);
|
||||
end if;
|
||||
New_Line;
|
||||
end Divide_By_Zero;
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
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"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
try? -> 3/0
|
||||
else -> print "division by zero"
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
onerror TratoError
|
||||
|
||||
print 2 / 3
|
||||
print 3 / 5
|
||||
print 4 / 0
|
||||
end
|
||||
|
||||
TratoError:
|
||||
print "Error in the line " + lasterrorline + " – Error number: " + lasterror + " – " + lasterrormessage + " (" + lasterrorextra + ")"
|
||||
return
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
Div ← {∨´"∞"‿"NaN"≡¨<•Fmt𝕩}◶⊢‿"Division by 0"÷
|
||||
|
||||
•Show 5 Div 0
|
||||
•Show 5 Div 5
|
||||
•Show 0 Div 0
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
"Division by 0"
|
||||
1
|
||||
"Division by 0"
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
@echo off
|
||||
set /a dummy=5/0 2>nul
|
||||
|
||||
if %errorlevel%==1073750993 echo I caught a division by zero operation...
|
||||
exit /b 0
|
||||
23
Task/Detect-division-by-zero/C++/detect-division-by-zero.cpp
Normal file
23
Task/Detect-division-by-zero/C++/detect-division-by-zero.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include<iostream>
|
||||
#include<csignal> /* for signal */
|
||||
#include<cstdlib>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void fpe_handler(int signal)
|
||||
{
|
||||
cerr << "Floating Point Exception: division by zero" << endl;
|
||||
exit(signal);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// Register floating-point exception handler.
|
||||
signal(SIGFPE, fpe_handler);
|
||||
|
||||
int a = 1;
|
||||
int b = 0;
|
||||
cout << a/b << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
100
Task/Detect-division-by-zero/C/detect-division-by-zero.c
Normal file
100
Task/Detect-division-by-zero/C/detect-division-by-zero.c
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
#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;
|
||||
}
|
||||
33
Task/Detect-division-by-zero/CLU/detect-division-by-zero.clu
Normal file
33
Task/Detect-division-by-zero/CLU/detect-division-by-zero.clu
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
% This will catch a divide-by-zero exception and
|
||||
% return a oneof instead, with either the result or div_by_zero.
|
||||
% Overflow and underflow are resignaled.
|
||||
check_div = proc [T: type] (a, b: T) returns (otype)
|
||||
signals (overflow, underflow)
|
||||
where T has div: proctype (T,T) returns (T)
|
||||
signals (zero_divide, overflow, underflow)
|
||||
otype = oneof[div_by_zero: null, result: T]
|
||||
|
||||
return(otype$make_result(a/b))
|
||||
except when zero_divide:
|
||||
return(otype$make_div_by_zero(nil))
|
||||
end resignal overflow, underflow
|
||||
end check_div
|
||||
|
||||
% Try it
|
||||
start_up = proc ()
|
||||
pair = struct[n, d: int]
|
||||
pairs: sequence[pair] := sequence[pair]$[
|
||||
pair${n: 10, d: 2}, % OK
|
||||
pair${n: 10, d: 0}, % divide by zero
|
||||
pair${n: 20, d: 2} % another OK one to show the program doesn't stop
|
||||
]
|
||||
|
||||
po: stream := stream$primary_output()
|
||||
for p: pair in sequence[pair]$elements(pairs) do
|
||||
stream$puts(po, int$unparse(p.n) || "/" || int$unparse(p.d) || " = ")
|
||||
tagcase check_div[int](p.n, p.d)
|
||||
tag div_by_zero: stream$putl(po, "divide by zero")
|
||||
tag result (r: int): stream$putl(po, int$unparse(r))
|
||||
end
|
||||
end
|
||||
end start_up
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
DIVIDE foo BY bar GIVING foobar
|
||||
ON SIZE ERROR
|
||||
DISPLAY "Division by zero detected!"
|
||||
END-DIVIDE
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
shared void run() {
|
||||
|
||||
//integers divided by zero throw an exception
|
||||
try {
|
||||
value a = 1 / 0;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
//floats divided by zero produce infinity
|
||||
print(1.0 / 0 == infinity then "division by zero!" else "not division by zero!");
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(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) )))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(handler-case (/ x y)
|
||||
(division-by-zero () (format t "division by zero caught!~%")))
|
||||
53
Task/Detect-division-by-zero/D/detect-division-by-zero.d
Normal file
53
Task/Detect-division-by-zero/D/detect-division-by-zero.d
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
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));
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
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.
|
||||
8
Task/Detect-division-by-zero/E/detect-division-by-zero.e
Normal file
8
Task/Detect-division-by-zero/E/detect-division-by-zero.e
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
def divide(numerator, denominator) {
|
||||
def floatQuotient := numerator / denominator
|
||||
if (floatQuotient.isNaN() || floatQuotient.isInfinite()) {
|
||||
return ["zero denominator"]
|
||||
} else {
|
||||
return ["ok", floatQuotient]
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
DBZ(REAL8 Dividend,INTEGER8 Divisor) := Quotient/Divisor;
|
||||
|
||||
#option ('divideByZero', 'zero');
|
||||
DBZ(10,0); //returns 0.0
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
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,"
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
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.
|
||||
*/
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
PROGRAM DIV_BY_ZERO
|
||||
|
||||
EXCEPTION
|
||||
IF ERR=11 THEN PRINT("Division by Zero") END IF
|
||||
END EXCEPTION
|
||||
|
||||
BEGIN
|
||||
PRINT(0/3)
|
||||
PRINT(3/0)
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
proc checkDivZero a b . .
|
||||
result$ = a / b
|
||||
if result$ = "-nan" or result$ = "inf" or result$ = "-inf"
|
||||
print "Found division by zero (" & a & " / " & b & ")"
|
||||
.
|
||||
.
|
||||
call checkDivZero 5 7
|
||||
call checkDivZero 1 0
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
open core number
|
||||
|
||||
x /. y = try Some (x `div` y) with
|
||||
_ = None
|
||||
|
||||
(12 /. 2, 12 /. 0)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
x /. 0 = None
|
||||
x /. y = Some (x / y)
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
defmodule Division do
|
||||
def by_zero?(x,y) do
|
||||
try do
|
||||
_ = x / y
|
||||
false
|
||||
rescue
|
||||
ArithmeticError -> true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
[{2, 3}, {3, 0}, {0, 5}, {0, 0}, {2.0, 3.0}, {3.0, 0.0}, {0.0, 5.0}, {0.0, 0.0}]
|
||||
|> Enum.each(fn {x,y} ->
|
||||
IO.puts "#{x} / #{y}\tdivision by zero #{Division.by_zero?(x,y)}"
|
||||
end)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(condition-case nil
|
||||
(/ 1 0)
|
||||
(arith-error
|
||||
(message "Divide by zero (either integer or float)")))
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
div_check(X,Y) ->
|
||||
case catch X/Y of
|
||||
{'EXIT',_} -> true;
|
||||
_ -> false
|
||||
end.
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
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)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
USE: math.floats.env
|
||||
|
||||
: try-div ( a b -- )
|
||||
'[ { +fp-zero-divide+ } [ _ _ /f . ] with-fp-traps ] try ;
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
def divide: x by: y {
|
||||
try {
|
||||
x / y
|
||||
} catch DivisionByZeroError => e {
|
||||
e message println # prints error message
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
: safe-/ ( x y -- x/y )
|
||||
['] / catch -55 = if cr ." divide by zero!" 2drop 0 then ;
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
program rosetta_divbyzero
|
||||
implicit none
|
||||
integer, parameter :: rdp = kind(1.d0)
|
||||
real(rdp) :: normal,zero
|
||||
|
||||
normal = 1.d0
|
||||
zero = 0.d0
|
||||
|
||||
call div_by_zero_check(normal,zero)
|
||||
|
||||
contains
|
||||
|
||||
subroutine div_by_zero_check(x,y)
|
||||
use, intrinsic :: ieee_exceptions
|
||||
use, intrinsic :: ieee_arithmetic
|
||||
implicit none
|
||||
real(rdp), intent(in) :: x,y
|
||||
|
||||
real(rdp) :: check
|
||||
type(ieee_status_type) :: status_value
|
||||
logical :: flag
|
||||
flag = .false.
|
||||
! Get the flags
|
||||
call ieee_get_status(status_value)
|
||||
! Set the flags quiet
|
||||
call ieee_set_flag(ieee_divide_by_zero,.false.)
|
||||
write(*,*)"Inf supported? ",ieee_support_inf(check)
|
||||
|
||||
! Calculation involving exception handling
|
||||
check = x/y
|
||||
write(*,*)"Is check finite?",ieee_is_finite(check), check
|
||||
|
||||
call ieee_get_flag(ieee_divide_by_zero, flag)
|
||||
if (flag) write(*,*)"Warning! Division by zero detected"
|
||||
|
||||
! Restore the flags
|
||||
call ieee_set_status(status_value)
|
||||
|
||||
end subroutine div_by_zero_check
|
||||
|
||||
end program rosetta_divbyzero
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
program rosetta_integer_divbyzero
|
||||
implicit none
|
||||
integer :: normal,zero,answer
|
||||
normal = 1
|
||||
zero = 0
|
||||
answer = normal/ zero
|
||||
write(*,*) answer
|
||||
end program rosetta_integer_divbyzero
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Const divByZeroResult As Integer = -9223372036854775808
|
||||
|
||||
Sub CheckForDivByZero(result As Integer)
|
||||
If result = divByZeroResult Then
|
||||
Print "Division by Zero"
|
||||
Else
|
||||
Print "Division by Non-Zero"
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Dim As Integer x, y
|
||||
|
||||
x = 0 : y = 0
|
||||
CheckForDivByZero(x/y) ' automatic conversion to type of parameter which is Integer
|
||||
x = 1
|
||||
CheckForDivByZero(x/y)
|
||||
x = -1
|
||||
CheckForDivByZero(x/y)
|
||||
y = 1
|
||||
CheckForDivByZero(x/y)
|
||||
Print
|
||||
Print "Press any key to exit"
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
include "ConsoleWindow"
|
||||
|
||||
on error stop
|
||||
dim as long a
|
||||
print a / 0
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
Public Sub Main()
|
||||
|
||||
Try Print 1 / 0
|
||||
If Error Then Print Error.Text
|
||||
|
||||
End
|
||||
16
Task/Detect-division-by-zero/Go/detect-division-by-zero.go
Normal file
16
Task/Detect-division-by-zero/Go/detect-division-by-zero.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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))
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
def dividesByZero = { double n, double d ->
|
||||
assert ! n.infinite : 'Algorithm fails if the numerator is already infinite.'
|
||||
(n/d).infinite || (n/d).naN
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
((3d)..(0d)).each { i ->
|
||||
((2d)..(0d)).each { j ->
|
||||
println "${i}/${j} divides by zero? " + dividesByZero(i,j)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
import qualified Control.Exception as C
|
||||
check x y = C.catch (x `div` y `seq` return False)
|
||||
(\_ -> return True)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
let a 1
|
||||
let b 0
|
||||
if tostr (a / (b + 0.)) = "inf"
|
||||
println "Divide by Zero"
|
||||
else
|
||||
println a / b
|
||||
endif
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
zero_divide(0, 1) returns 0 (false)
|
||||
zero_divide( 1, 3-2-1 ) returns 1 (true)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
try {
|
||||
Print("%d\n", 10 / 0);
|
||||
} catch {
|
||||
Print("Divide by zero");
|
||||
}
|
||||
15
Task/Detect-division-by-zero/I/detect-division-by-zero.i
Normal file
15
Task/Detect-division-by-zero/I/detect-division-by-zero.i
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
//Division by zero is defined in 'i' so the result can be checked to determine division by zero.
|
||||
concept IsDivisionByZero(a, b) {
|
||||
c = a/b
|
||||
if c = 0 and a - 0 or a = 0 and c > 0
|
||||
print( a, "/", b, " is a division by zero.")
|
||||
return
|
||||
end
|
||||
print( a, "/", b, " is not division by zero.")
|
||||
}
|
||||
|
||||
software {
|
||||
IsDivisionByZero(5, 0)
|
||||
IsDivisionByZero(5, 2)
|
||||
IsDivisionByZero(0, 0)
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
if not finite( <i>expression</i> ) then ...
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
100 WHEN EXCEPTION USE ERROR
|
||||
110 FOR I=5 TO-2 STEP-1
|
||||
120 PRINT 10/I
|
||||
130 NEXT
|
||||
140 END WHEN
|
||||
150 HANDLER ERROR
|
||||
160 IF EXTYPE=3001 THEN PRINT EXSTRING$(EXTYPE);" in line";EXLINE
|
||||
170 CONTINUE
|
||||
180 END HANDLER
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
procedure main()
|
||||
&error := 1
|
||||
udef := 1 / 0 | stop("Run-time error ", &errornumber, " : ", &errortext," in line #",&line," - converted to failure")
|
||||
end
|
||||
|
|
@ -0,0 +1 @@
|
|||
funnydiv=: 0 { [: (,:'division by zero detected')"_^:(_ e. |@,) (,>:)@:(,:^:(0<#@$))@[ %"_1 _ ]
|
||||
10
Task/Detect-division-by-zero/J/detect-division-by-zero-2.j
Normal file
10
Task/Detect-division-by-zero/J/detect-division-by-zero-2.j
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
public static boolean infinity(double numer, double denom){
|
||||
return Double.isInfinite(numer/denom);
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
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;}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
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));
|
||||
|
|
@ -0,0 +1 @@
|
|||
def div(x;y): if y==0 then error("NaN") else x/y end;
|
||||
|
|
@ -0,0 +1 @@
|
|||
try div(3;0) catch if "NaN" then "div by 0 error detected" else . end
|
||||
|
|
@ -0,0 +1 @@
|
|||
if (!isFinite(numerator/denominator)) puts("result is infinity or not a number");
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
isdefinite(n::Number) = !isnan(n) && !isinf(n)
|
||||
|
||||
for n in (1, 1//1, 1.0, 1im, 0)
|
||||
d = n / 0
|
||||
println("Dividing $n by 0 ", isdefinite(d) ? "results in $d." : "yields an indefinite value ($d).")
|
||||
end
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
// version 1.1
|
||||
|
||||
fun divideByZero(x: Int, y:Int): Boolean =
|
||||
try {
|
||||
x / y
|
||||
false
|
||||
} catch(e: ArithmeticException) {
|
||||
true
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val x = 1
|
||||
val y = 0
|
||||
if (divideByZero(x, y)) {
|
||||
println("Attempted to divide by zero")
|
||||
} else {
|
||||
@Suppress("DIVISION_BY_ZERO")
|
||||
println("$x / $y = ${x / y}")
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
{def DivByZero?
|
||||
{lambda {:w}
|
||||
{W.equal? :w Infinity}}}
|
||||
|
||||
{DivByZero? {/ 3 2}}
|
||||
-> false
|
||||
{DivByZero? {/ 3 0}}
|
||||
-> true
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
val .div = f(.x, .y) {
|
||||
[.x / .y, true]
|
||||
catch {
|
||||
if matching(re/division by 0/, _err["msg"]) {
|
||||
[0, false]
|
||||
} else {
|
||||
# rethrow the error if not division by 0
|
||||
throw
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
writeln .div(3, 2)
|
||||
writeln .div(3, 0)
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
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)
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
on div (a, b)
|
||||
-- for simplicity type check of vars omitted
|
||||
res = value("float(a)/b")
|
||||
if voidP(res) then
|
||||
_player.alert("Division by zero!")
|
||||
else
|
||||
return res
|
||||
end if
|
||||
end
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
local function div(a,b)
|
||||
if b == 0 then error() end
|
||||
return a/b
|
||||
end
|
||||
|
|
@ -0,0 +1 @@
|
|||
Print function("{Read x : =x**2}", 2)=4
|
||||
|
|
@ -0,0 +1 @@
|
|||
Print Valid(100/0)=False
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
Module Checkit {
|
||||
Function DetectDivisionByZero(&a()) {
|
||||
Try {
|
||||
a=a()
|
||||
}
|
||||
=Error$=" division by zero"
|
||||
}
|
||||
|
||||
Print DetectDivisionByZero(lazy$(10/0))=True
|
||||
Z=10
|
||||
A=4
|
||||
B=0
|
||||
Print DetectDivisionByZero(lazy$(Z/B))=True
|
||||
Print DetectDivisionByZero(lazy$(Z/A))=False
|
||||
}
|
||||
Checkit
|
||||
|
|
@ -0,0 +1 @@
|
|||
ifelse(eval(2/0),`',`detected divide by zero or some other error of some kind')
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
function [isDividedByZero] = dividebyzero(numerator, denomenator)
|
||||
isDividedByZero = isinf( numerator/denomenator );
|
||||
% If isDividedByZero equals 1, divide by zero occured.
|
||||
|
|
@ -0,0 +1 @@
|
|||
if not bit.isFinite (<i>expression</i>) then...
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
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
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
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
|
||||
|
|
@ -0,0 +1 @@
|
|||
1/0; # Here is the default behavior.
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
NumericEventHandler( ':-division_by_zero'
|
||||
= proc() infinity; end proc ):
|
||||
|
||||
1/0;
|
||||
|
||||
NumericStatus(':-division_by_zero'); # We may check the status flag
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
NumericEventHandler( ':-division_by_zero'
|
||||
= proc()
|
||||
WARNING("division by zero");
|
||||
NumericStatus(':-division_by_zero'=false):
|
||||
infinity;
|
||||
end proc ):
|
||||
|
||||
1/0;
|
||||
|
||||
NumericStatus(':-division_by_zero');
|
||||
|
|
@ -0,0 +1 @@
|
|||
Check[2/0, Print["division by 0"], Power::infy]
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
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
|
||||
|
|
@ -0,0 +1 @@
|
|||
(/ inf ==) :div-zero?
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
10 ON ERROR GOTO 40
|
||||
20 PRINT 1/0
|
||||
30 END
|
||||
40 IF ERR = 10 THEN PRINT "DIVISION BY ZERO IN LINE"ERL
|
||||
50 RESUME 30
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
def div_check(x, y)
|
||||
try
|
||||
(x / y)
|
||||
return false
|
||||
catch
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
/**
|
||||
Detect division by zero
|
||||
*/
|
||||
|
||||
var ans = 1.0 / 0.0
|
||||
if $isinfinite(ans) $print("division by zero: ", ans, "\n")
|
||||
|
||||
ans = 1 / 0
|
||||
if $isinfinite(ans) $print("division by zero: ", ans, "\n")
|
||||
|
||||
try $print($idiv(1, 0)) catch problem $print("idiv by zero: ", problem, "\n")
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
;; 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"
|
||||
]
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
/* 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
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
#! /usr/local/bin/newlisp
|
||||
|
||||
(define (check-division x y)
|
||||
(catch (/ x y) 'check-zero)
|
||||
(if (not (integer? check-zero))
|
||||
(setq check-zero "Division by zero."))
|
||||
check-zero
|
||||
)
|
||||
|
||||
(println (check-division 10 4))
|
||||
(println (check-division 4 0))
|
||||
(println (check-division 20 5))
|
||||
(println (check-division 11 0))
|
||||
|
||||
(exit)
|
||||
10
Task/Detect-division-by-zero/Nim/detect-division-by-zero.nim
Normal file
10
Task/Detect-division-by-zero/Nim/detect-division-by-zero.nim
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
{.push overflowChecks: on.}
|
||||
proc divCheck(x, y): bool =
|
||||
try:
|
||||
discard x div y
|
||||
except DivByZeroDefect:
|
||||
return true
|
||||
return false
|
||||
{.pop.} # Restore default check settings
|
||||
|
||||
echo divCheck(2, 0)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
let div_check x y =
|
||||
try
|
||||
ignore (x / y);
|
||||
false
|
||||
with Division_by_zero ->
|
||||
true
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
let div_check x y =
|
||||
classify_float (x /. y) = FP_infinite
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
d = 5/0;
|
||||
if ( isinf(d) )
|
||||
if ( index(lastwarn(), "division by zero") > 0 )
|
||||
error("division by zero")
|
||||
endif
|
||||
endif
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue