new files

This commit is contained in:
Ingy döt Net 2013-04-10 12:38:42 -07:00
parent 3af7344581
commit 86c034bb8b
1364 changed files with 21352 additions and 0 deletions

View file

@ -0,0 +1 @@
Write a function to detect a divide by zero error without checking if the denominator is zero.

View file

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

View 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 then
Put("Division by zero detected (infinite value).");
else
Put(Item => Fresult, Aft => 9, Exp => 0);
end if;
New_Line;
end Divide_By_Zero;

View 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;
}

View file

@ -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) )))

View file

@ -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

View file

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

View file

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

View 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))
}

View file

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

View file

@ -0,0 +1,3 @@
public static boolean infinity(double numer, double denom){
return Double.isInfinite(numer/denom);
}

View file

@ -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;}
}

View file

@ -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));

View file

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

View file

@ -0,0 +1,6 @@
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'] != '';
}

View file

@ -0,0 +1,3 @@
function div_check($x, $y) {
return @($x / $y) === FALSE; // works at least in PHP/5.2.6-3ubuntu4.5
}

View file

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

View file

@ -0,0 +1 @@
(catch '("Div/0") (/ A B))

View file

@ -0,0 +1,7 @@
def div_check(x, y):
try:
x / y
except ZeroDivisionError:
return True
else:
return False

View file

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

View file

@ -0,0 +1,22 @@
/*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)

View file

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

View file

@ -0,0 +1,9 @@
def div_check(x, y)
begin
x / y
rescue ZeroDivisionError
true
else
false
end
end

View file

@ -0,0 +1,4 @@
irb(main):010:0> div_check(5, 0)
=> true
irb(main):011:0> div_check(5.0, 0)
=> false

View file

@ -0,0 +1,9 @@
def div_check(x, y)
begin
x.div y
rescue ZeroDivisionError
true
else
false
end
end

View file

@ -0,0 +1,4 @@
irb(main):010:0> div_check(5, 0)
=> true
irb(main):011:0> div_check(5.0, 0)
=> true

View file

@ -0,0 +1,17 @@
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))
}

View file

@ -0,0 +1,7 @@
zeroDivide := [:aBlock |
[aBlock value. false] on: ZeroDivide do: [true].
].
"Testing"
zeroDivide value: [2/1] "------> false"
zeroDivide value: [2/0] "------> true"

View file

@ -0,0 +1,15 @@
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
}

View file

@ -0,0 +1,15 @@
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
}