Detect division by zero: Difference between revisions

m (syntax highlighting fixup automation)
 
(21 intermediate revisions by 9 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 753 ⟶ 757:
}
}</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func checkDivZero a b .
r = a / b
if r = number "nan" or r = number "inf" or r = number "-inf"
return 1
.
.
print checkDivZero 5 7
print checkDivZero -1 0
</syntaxhighlight>
 
=={{header|ECL}}==
Line 1,045 ⟶ 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,306 ⟶ 1,322:
 
If the division node receives zero on both nodes (0/0), the Result will be "NaN"
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .div = fn(.x, .y) {
[.x / .y, true]
catch {
if _err'msg -> re/division by 0/ {
[0, false]
} else {
# rethrow the error if not division by 0
throw
}
}
}
 
writeln .div(3, 2)
writeln .div(3, 0)
</syntaxhighlight>
 
{{out}}
<pre>[1.5, true]
[0, false]
</pre>
 
=={{header|Lasso}}==
Line 1,443 ⟶ 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 1,790 ⟶ 1,864:
<syntaxhighlight lang="pl/sql">divide(0,1) --false
divide(1,0) --true, division by zero</syntaxhighlight>
 
=={{header|Plain English}}==
{{libheader|Plain English-output}}
When dividing by zero in Plain English, it explicitly returns 2147483647. The decider below detects division by zero by checking if it returns 2147483647.
<syntaxhighlight lang="text">
To run:
Start up.
If 1 and 0 does cause division error, write "Division by zero found" to the output.
Wait for the escape key.
Shut down.
 
To decide if a number and another number does cause division error:
Put the number divided by the other number into a third number.
If the third number is the largest number, say yes.
Say no.
</syntaxhighlight>
 
=={{header|PowerShell}}==
Line 2,004 ⟶ 2,094:
*inlr = *on;
</syntaxhighlight>
 
=={{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>
4 0 <span style="color:blue">DIV</span>
{{out}}
<pre>
2: 3
1: '4/0'
</pre>
 
=={{header|Ruby}}==
Line 2,460 ⟶ 2,565:
<pre>
True
</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Go">
 
fn main() {
divide(0, 0)
divide(15, 0)
divide(15, 3)
}
 
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
}
println(result)
}
</syntaxhighlight>
 
Alternate version:
 
<syntaxhighlight lang="Go">
fn main() {
divide(0, 0)
divide(15, 0)
divide(15, 3)
}
 
pub fn divide(x f64, y f64) {
succeed := divide_error_handler(x, y) or {
println(err)
return
}
println(succeed)
}
 
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
}
</syntaxhighlight>
 
{{out}}
<pre>
Can't divide by zero!
Can't divide by zero!
5.0
</pre>
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var checkDivByZero = Fn.new { |a, b|
var c = a / b
if (c.isInfinity || c.isNan) return true
889

edits