langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
|
|
@ -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,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
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
try
|
||||
{Show 42 div 0}
|
||||
catch error(kernel(div0 ...) ...) then
|
||||
{System.showInfo "Division by zero detected."}
|
||||
end
|
||||
|
|
@ -0,0 +1 @@
|
|||
trap(,"division by 0",m/n)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
/* A function is not required to detect division by zero. */
|
||||
/* The following statement is executed anywhere prior to */
|
||||
/* executing the statement containing the division by zero. */
|
||||
|
||||
on zerodivide snap go to trap_zerodivide;
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
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;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
divide(0,1) --false
|
||||
divide(1,0) --true, division by zero
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
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"
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
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"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
> 1/0, -1/0, 0/0;
|
||||
inf,-inf,nan
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
> inf_or_nan x = infp x || nanp x;
|
||||
> map inf_or_nan [1/0, -1/0, 0/0];
|
||||
[1,1,1]
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
> divide n m = catch (\_ -> "divide by 0") (n div m);
|
||||
> divide 0 1;
|
||||
0
|
||||
> divide 1 0;
|
||||
"divide by 0"
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
;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
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Define.d a, b
|
||||
Debug a/b
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
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.
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
$ 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;
|
||||
|
|
@ -0,0 +1 @@
|
|||
[ 1 / 0 ] on: Error do: [|:err| err return: PositiveInfinity].
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
fun div_check (x, y) = (
|
||||
ignore (x div y);
|
||||
false
|
||||
) handle Div => true
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fun div_check (x, y) =
|
||||
not (Real.isFinite (x / y))
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
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");
|
||||
]
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
func div_check(x, y) {
|
||||
if(catch(0x01))
|
||||
return 1;
|
||||
temp = x/y;
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue