Detect division by zero: Difference between revisions

 
(5 intermediate revisions by 3 users not shown)
Line 203:
==={{header|Applesoft BASIC}}===
The error code for division by zero is 133. There is a good overview of Applesoft ONERR GOTO handling here:
https://web.archive.org/web/20190202133738/http://newsgroups.derkeiler.com/Archive/Comp/comp.sys.apple2.programmer/2010-04/msg00000.html
 
<syntaxhighlight lang="basic"> 100 REM TRY
Line 1,324:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .div = ffn(.x, .y) {
[.x / .y, true]
catch {
if matching(_err'msg -> re/division by 0/, _err["msg"]) {
[0, false]
} else {
Line 1,481:
<syntaxhighlight lang="min">(/ inf ==) :div-zero?</syntaxhighlight>
Integer divison (that is, <code>div</code> and not <code>/</code>) by zero will cause min to exit with an uncatchable arithmetic error.
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">number.isInfinite = function
return abs(self) == 1/0
end function
 
number.isNaN = function
return self != self
end function
 
number.toBoolStr = function
if self == 0 then return "false"
return "true"
end function
 
checkDivByZero = function(a, b)
c = a / b
if c.isInfinite or c.isNaN then return true
return false
end function
 
print "Division by zero?"
print " 0 / 0 -> " + checkDivByZero( 0, 0).toBoolStr
print " 1 / 0 -> " + checkDivByZero( 1, 0).toBoolStr
print " 1 / 1 -> " + checkDivByZero( 1, 1).toBoolStr
print " -5 / 0 -> " + checkDivByZero(-5, 0).toBoolStr
print " -5 / 2 -> " + checkDivByZero(-5, 2).toBoolStr</syntaxhighlight>
 
{{out}}
<pre>Division by zero?
0 / 0 -> true
1 / 0 -> true
1 / 1 -> false
-5 / 0 -> true
-5 / 2 -> false
</pre>
 
=={{header|mIRC Scripting Language}}==
Line 2,061 ⟶ 2,097:
=={{header|RPL}}==
RPL provides targeted error detection and handling. In case of a division by zero, rather than displaying an error message, the program delivers the attempted arithmetic operation as an expression to be further processed.
'''IFERR''' / '''THEN'''
SWAP "'" SWAP →STR + "/" + SWAP →STR + STR→
'''END'''
'<span style="color:blue">DIV</span>' STO
6 2 <span style="color:blue">DIV</span>
6 2 DIV
4 0 <span style="color:blue">DIV</span>
4 0 DIV
{{out}}
<pre>
Line 2,584 ⟶ 2,620:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var checkDivByZero = Fn.new { |a, b|
var c = a / b
if (c.isInfinity || c.isNan) return true
890

edits