Detect division by zero: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(→‎{{header|Lua}}: For 0/0 Lua returns -nan)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 12:
</lang>
{{out}}<pre>true</pre>
 
=={{header|ABAP}}==
<lang ABAP>report zdiv_zero
Line 325 ⟶ 326:
if %errorlevel%==1073750993 echo I caught a division by zero operation...
exit /b 0</lang>
 
=={{header|C}}==
Technically, under the C standard, division by zero (regardless of type) is undefined behavior, so there is no standard way to run the division and then try to "detect" it later.
Line 444 ⟶ 446:
 
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.
 
=={{header|C sharp|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.
 
<lang csharp>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);
}
}
}
}</lang>
 
=={{header|C++}}==
Line 471 ⟶ 494:
}
</lang>
 
=={{header|C sharp|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.
 
<lang csharp>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);
}
}
}
}</lang>
 
=={{header|Ceylon}}==
Line 603 ⟶ 605:
real inf/ inf: -nan | Inf numerator
real nan/ nan: nan | NaN numerator</pre>
 
=={{header|Déjà Vu}}==
<lang dejavu>divcheck x y:
true
try:
drop / x y
catch value-error:
not
 
if divcheck 1 0:
!print "Okay"
else:
!print "Division by zero"</lang>
{{out}}
<pre>Division by zero</pre>
 
=={{header|Delphi}}==
Line 638 ⟶ 625:
end;
end.</lang>
 
=={{header|Déjà Vu}}==
<lang dejavu>divcheck x y:
true
try:
drop / x y
catch value-error:
not
 
if divcheck 1 0:
!print "Okay"
else:
!print "Division by zero"</lang>
{{out}}
<pre>Division by zero</pre>
 
=={{header|E}}==
Line 1,062 ⟶ 1,064:
IsDivisionByZero(0, 0)
}</lang>
 
=={{header|IDL}}==
 
<lang idl>if not finite( <i>expression</i> ) then ...</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 1,075 ⟶ 1,073:
 
Sample Output:<pre>Run-time error 201 : division by zero in line #3 - converted to failure</pre>
 
=={{header|IDL}}==
 
<lang idl>if not finite( <i>expression</i> ) then ...</lang>
 
=={{header|J}}==
Line 1,319 ⟶ 1,321:
=={{header|MAXScript}}==
<lang maxscript>if not bit.isFinite (<i>expression</i>) then...</lang>
 
=={{header|min}}==
{{works with|min|0.19.3}}
The following operator will detect division by zero since the result will be infinity.
<lang min>(/ inf ==) :div-zero?</lang>
Integer divison (that is, <code>div</code> and not <code>/</code>) by zero will cause min to exit with an uncatchable arithmetic error.
 
=={{header|mIRC Scripting Language}}==
<lang mirc>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
}</lang>
 
=={{header|MUMPS}}==
Line 1,346 ⟶ 1,363:
<UNDEFINED> *C
</pre>
 
=={{header|min}}==
{{works with|min|0.19.3}}
The following operator will detect division by zero since the result will be infinity.
<lang min>(/ inf ==) :div-zero?</lang>
Integer divison (that is, <code>div</code> and not <code>/</code>) by zero will cause min to exit with an uncatchable arithmetic error.
 
=={{header|mIRC Scripting Language}}==
<lang mirc>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
}</lang>
 
=={{header|Nanoquery}}==
Line 1,487 ⟶ 1,489:
<lang ocaml>let div_check x y =
classify_float (x /. y) = FP_infinite</lang>
 
=={{header|Oforth}}==
 
<lang Oforth>: divideCheck(n)
| e |
try: e [ 128 n / ] when: [ "Zero detected..." . ]
"Leaving" println ;</lang>
 
=={{header|Octave}}==
Line 1,505 ⟶ 1,500:
endif
endif</lang>
 
=={{header|Oforth}}==
 
<lang Oforth>: divideCheck(n)
| e |
try: e [ 128 n / ] when: [ "Zero detected..." . ]
"Leaving" println ;</lang>
 
=={{header|Ol}}==
Line 1,582 ⟶ 1,584:
eval {$_[0] / $_[1]};
$@ and $@ =~ /division by zero/;}</lang>
 
=={{header|Perl 6}}==
====Try/Catch====
<lang perl6>sub div($a, $b) {
my $r;
try {
$r = $a / $b;
CATCH {
default { note "Unexpected exception, $_" }
}
}
return $r // Nil;
}
say div(10,2);
say div(1, sin(0));</lang>
{{out}}
<pre>5
Unexpected exception, Attempt to divide 1 by zero using /
Nil</pre>
 
====Multi Method Dispatch====
<lang perl6>multi div($a, $b) { return $a / $b }
multi div($a, $b where { $b == 0 }) { note 'Attempt to divide by zero.'; return Nil }
 
say div(10, 2);
say div(1, sin(0));</lang>
{{out}}
<pre>5
Attempt to divide by zero.
Nil</pre>
 
=={{header|Phix}}==
Line 1,753 ⟶ 1,725:
(/ 1 0))
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
====Try/Catch====
<lang perl6>sub div($a, $b) {
my $r;
try {
$r = $a / $b;
CATCH {
default { note "Unexpected exception, $_" }
}
}
return $r // Nil;
}
say div(10,2);
say div(1, sin(0));</lang>
{{out}}
<pre>5
Unexpected exception, Attempt to divide 1 by zero using /
Nil</pre>
 
====Multi Method Dispatch====
<lang perl6>multi div($a, $b) { return $a / $b }
multi div($a, $b where { $b == 0 }) { note 'Attempt to divide by zero.'; return Nil }
 
say div(10, 2);
say div(1, sin(0));</lang>
{{out}}
<pre>5
Attempt to divide by zero.
Nil</pre>
 
=={{header|REBOL}}==
Line 1,852 ⟶ 1,855:
*inlr = *on;
</lang>
 
 
 
=={{header|Ruby}}==
Line 2,003 ⟶ 2,004:
<lang slate>[ 1 / 0 ] on: Error do: [|:err| err return: PositiveInfinity].</lang>
 
 
=={{header|Smalltalk}}==
Line 2,193:
end try
end</lang>
 
=={{header|VAX Assembly}}==
<lang VAX Assembly>65 64 69 76 69 64 00000008'010E0000' 0000 1 desc: .ascid "divide by zero"
Line 2,229 ⟶ 2,230:
{{Out}}
<pre>Error
15,2</pre>
 
=={{header|VBScript}}==
Line 2,277 ⟶ 2,278:
True
</pre>
 
 
=={{header|XPL0}}==
10,327

edits