Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -0,0 +1,5 @@
|
|||
@echo off
|
||||
set /a dummy=5/0 2>nul
|
||||
|
||||
if %errorlevel%==1073750993 echo I caught a division by zero operation...
|
||||
exit /b 0
|
||||
23
Task/Detect-division-by-zero/C++/detect-division-by-zero.cpp
Normal file
23
Task/Detect-division-by-zero/C++/detect-division-by-zero.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include<iostream>
|
||||
#include<csignal> /* for signal */
|
||||
#include<cstdlib>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void fpe_handler(int signal)
|
||||
{
|
||||
cerr << "Floating Point Exception: division by zero" << endl;
|
||||
exit(signal);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// Register floating-point exception handler.
|
||||
signal(SIGFPE, fpe_handler);
|
||||
|
||||
int a = 1;
|
||||
int b = 0;
|
||||
cout << a/b << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
defmodule Division do
|
||||
def by_zero?(x,y) do
|
||||
try do
|
||||
_ = x / y
|
||||
false
|
||||
rescue
|
||||
ArithmeticError -> true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
[{2, 3}, {3, 0}, {0, 5}, {0, 0}, {2.0, 3.0}, {3.0, 0.0}, {0.0, 5.0}, {0.0, 0.0}]
|
||||
|> Enum.each(fn {x,y} ->
|
||||
IO.puts "#{x} / #{y}\tdivision by zero #{Division.by_zero?(x,y)}"
|
||||
end)
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
program rosetta_divbyzero
|
||||
implicit none
|
||||
integer, parameter :: rdp = kind(1.d0)
|
||||
real(rdp) :: normal,zero
|
||||
|
||||
normal = 1.d0
|
||||
zero = 0.d0
|
||||
|
||||
call div_by_zero_check(normal,zero)
|
||||
|
||||
contains
|
||||
|
||||
subroutine div_by_zero_check(x,y)
|
||||
use, intrinsic :: ieee_exceptions
|
||||
use, intrinsic :: ieee_arithmetic
|
||||
implicit none
|
||||
real(rdp), intent(in) :: x,y
|
||||
|
||||
real(rdp) :: check
|
||||
type(ieee_status_type) :: status_value
|
||||
logical :: flag
|
||||
flag = .false.
|
||||
! Get the flags
|
||||
call ieee_get_status(status_value)
|
||||
! Set the flags quiet
|
||||
call ieee_set_flag(ieee_divide_by_zero,.false.)
|
||||
write(*,*)"Inf supported? ",ieee_support_inf(check)
|
||||
|
||||
! Calculation involving exception handling
|
||||
check = x/y
|
||||
write(*,*)"Is check finite?",ieee_is_finite(check), check
|
||||
|
||||
call ieee_get_flag(ieee_divide_by_zero, flag)
|
||||
if (flag) write(*,*)"Warning! Division by zero detected"
|
||||
|
||||
! Restore the flags
|
||||
call ieee_set_status(status_value)
|
||||
|
||||
end subroutine div_by_zero_check
|
||||
|
||||
end program rosetta_divbyzero
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
program rosetta_integer_divbyzero
|
||||
implicit none
|
||||
integer :: normal,zero,answer
|
||||
normal = 1
|
||||
zero = 0
|
||||
answer = normal/ zero
|
||||
write(*,*) answer
|
||||
end program rosetta_integer_divbyzero
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
function isdefinite{T<:Number}(n::T)
|
||||
!isequal(n, NaN) && abs(n) != Inf
|
||||
end
|
||||
|
||||
for n in {1, 1//1, 1.0, 1im, 0}
|
||||
d = n/0
|
||||
print("Divding ", n, " by 0 ")
|
||||
if isdefinite(d)
|
||||
println("results in ", d, ".")
|
||||
else
|
||||
println("yields an indefinite value (", d, ").")
|
||||
end
|
||||
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
|
||||
|
|
@ -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