Detect division by zero: Difference between revisions

 
(10 intermediate revisions by 7 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 249:
ENDIF
ENDPROC</syntaxhighlight>
 
==={{header|GW-BASIC}}===
The [[#Locomotive_BASIC|Locomotive BASIC]] solution works without any changes.
 
==={{header|IS-BASIC}}===
Line 287 ⟶ 290:
Division by zero in line 40 </pre>
 
==={{header|PureBasicMSX Basic}}===
The [[#Locomotive_BASIC|Locomotive BASIC]] solution works without any changes.
 
==={{header|PureBasic}}===
PureBasic can be compiled with the [http://www.purebasic.com/documentation/onerror/index.html OnError] library included which gives a way to track program errors without losing speed, doing so gives support for the following functions;
*ErrorAddress()
Line 338 ⟶ 343:
PRINT 3 / 5
PRINT 4 / 0
Sleep
END
 
Line 756 ⟶ 760:
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func checkDivZero a b . .
result$r = a / b
if result$r = number "-nan" or result$r = number "inf" or result$r = number "-inf"
return 1
print "Found division by zero (" & a & " / " & b & ")"
.
.
callprint checkDivZero 5 7
callprint checkDivZero -1 0
</syntaxhighlight>
{{out}}
<pre>Found division by zero (1 / 0)</pre>
 
=={{header|ECL}}==
Line 1,059 ⟶ 1,061:
Stop on error. Error type reported in log console.
<syntaxhighlight lang="futurebasic">
include "ConsoleWindow"
 
on error stop
dim as long a
print a / 0
 
HandleEvents
</syntaxhighlight>
 
Line 1,322 ⟶ 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,479 ⟶ 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,059 ⟶ 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,540 ⟶ 2,578:
fn divide(x f64, y f64) {
result := x/y
if result.str().contains_any_substr(['"inf'",'"nan'"]) == true {
println("Can\'t divide by zero!'")
return
}
Line 2,567 ⟶ 2,605:
fn divide_error_handler(x f64, y f64) !f64 {
result := x/y
if result.str().contains_any_substr(['"inf'",'"nan'"]) == true {
return error('"Can\'t divide by zero!'")
}
return result
Line 2,582 ⟶ 2,620:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var checkDivByZero = Fn.new { |a, b|
var c = a / b
if (c.isInfinity || c.isNan) return true
889

edits