Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,32 @@
|
|||
-- Divide By Zero Detection
|
||||
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
|
||||
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
|
||||
|
||||
procedure Divide_By_Zero is
|
||||
Fnum : Float := 1.0;
|
||||
Fdenom : Float := 0.0;
|
||||
Fresult : Float;
|
||||
Inum : Integer := 1;
|
||||
Idenom : Integer := 0;
|
||||
Iresult : Integer;
|
||||
begin
|
||||
begin
|
||||
Put("Integer divide by zero: ");
|
||||
Iresult := Inum / Idenom;
|
||||
Put(Item => Iresult);
|
||||
exception
|
||||
when Constraint_Error =>
|
||||
Put("Division by zero detected.");
|
||||
end;
|
||||
New_Line;
|
||||
Put("Floating point divide by zero: ");
|
||||
Fresult := Fnum / Fdenom;
|
||||
if Fresult > Float'Last or Fresult < Float'First then
|
||||
Put("Division by zero detected (infinite value).");
|
||||
else
|
||||
Put(Item => Fresult, Aft => 9, Exp => 0);
|
||||
end if;
|
||||
New_Line;
|
||||
end Divide_By_Zero;
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Divide_By_Zero is
|
||||
begin
|
||||
declare
|
||||
N : Integer := 1;
|
||||
D : Integer := 0;
|
||||
begin
|
||||
N := N / D;
|
||||
exception
|
||||
when Constraint_Error =>
|
||||
Put_Line ("Integer zero divide");
|
||||
end;
|
||||
declare
|
||||
type Sane_Float is new Float range Float'First..Float'Last;
|
||||
N : Sane_Float := 1.0;
|
||||
D : Sane_Float := 0.0;
|
||||
begin
|
||||
N := N / D;
|
||||
exception
|
||||
when Constraint_Error =>
|
||||
Put_Line ("Floating-point zero divide");
|
||||
end;
|
||||
end Divide_By_Zero;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
DIVIDE foo BY bar GIVING foobar
|
||||
ON SIZE ERROR
|
||||
DISPLAY "Division by zero detected!"
|
||||
END-DIVIDE
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
def division_by_zero? (dividend, divisor)
|
||||
dividend % divisor
|
||||
false
|
||||
rescue DivisionByZeroError
|
||||
true
|
||||
end
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(condition-case nil
|
||||
(/ 1 0)
|
||||
(arith-error
|
||||
(message "Divide by zero (either integer or float)")))
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
import gleam/int
|
||||
import gleam/io
|
||||
|
||||
pub fn safe_div(a, b: Int) -> Result(Int, Nil) {
|
||||
case b {
|
||||
0 -> Error(Nil)
|
||||
_ -> Ok(a / b)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
// In Gleam division by 0 is not an error and returns 0.
|
||||
// But we can make a safe_div function if we desire.
|
||||
case safe_div(10, 5) {
|
||||
Error(Nil) -> io.print_error("Division by zero")
|
||||
Ok(d) -> io.println("result of division is " <> d |> int.to_string)
|
||||
}
|
||||
|
||||
case safe_div(10, 0) {
|
||||
Error(Nil) -> io.print_error("Division by zero")
|
||||
Ok(d) -> io.println("result of division is " <> d |> int.to_string)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
local function divide(a, b)
|
||||
return a // b
|
||||
end
|
||||
|
||||
local ok, res = pcall(divide, 1, 0)
|
||||
if ok then
|
||||
print(res)
|
||||
else
|
||||
print("Attempted to divide by zero.")
|
||||
end
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
function div ($a, $b) {
|
||||
try{$a/$b}
|
||||
catch{"Bad parameters: `$a = $a and `$b = $b"}
|
||||
}
|
||||
div 10 2
|
||||
div 1 0
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
-- 23 Aug 2025
|
||||
-- 21 Feb 2026
|
||||
include Setting
|
||||
|
||||
say 'DETECT DIVISION BY ZERO'
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
Rebol [
|
||||
Title: "Detect Divide by Zero"
|
||||
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,14 @@
|
|||
div-check: func [
|
||||
"Attempt to divide two numbers, report result or errors as needed."
|
||||
x y
|
||||
/local result
|
||||
] [
|
||||
print either error? result: try [x / y][
|
||||
["Caught" result/type "error:" result/id]
|
||||
][ [x "/" y "=" result]]
|
||||
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 @@
|
|||
try { 1 / 0 }
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
÷0 10
|
||||
= ∞
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
Function div(num,den)
|
||||
On Error Resume Next
|
||||
n = num/den
|
||||
If Err.Number <> 0 Then
|
||||
div = Err.Description & " is not allowed."
|
||||
Else
|
||||
div = n
|
||||
End If
|
||||
End Function
|
||||
|
||||
WScript.StdOut.WriteLine div(6,3)
|
||||
WScript.StdOut.WriteLine div(6,0)
|
||||
WScript.StdOut.WriteLine div(7,-4)
|
||||
Loading…
Add table
Add a link
Reference in a new issue