Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
46
Task/Approximate-equality/Ada/approximate-equality.adb
Normal file
46
Task/Approximate-equality/Ada/approximate-equality.adb
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Ada.Numerics.Generic_Elementary_Functions;
|
||||
|
||||
procedure Main is
|
||||
type Real is digits 18;
|
||||
package Real_Funcs is new Ada.Numerics.Generic_Elementary_Functions(Real);
|
||||
use Real_Funcs;
|
||||
package Real_IO is new Ada.Text_IO.Float_IO(Real);
|
||||
use Real_IO;
|
||||
|
||||
function Approx_Equal (Left : Real; Right : Real) return Boolean is
|
||||
|
||||
-- Calculate an epsilon value based upon the magnitude of the
|
||||
-- maximum value of the two parameters
|
||||
eps : Real := Real'Max(Left, Right) * 1.0e-9;
|
||||
begin
|
||||
if left > Right then
|
||||
return Left - Right < eps;
|
||||
else
|
||||
return Right - Left < eps;
|
||||
end if;
|
||||
end Approx_Equal;
|
||||
|
||||
Type Index is (Left, Right);
|
||||
type Pairs_List is array (Index) of Real;
|
||||
type Pairs_Table is array(1..8) of Pairs_List;
|
||||
Table : Pairs_Table;
|
||||
|
||||
begin
|
||||
Table := ((100000000000000.01, 100000000000000.011),
|
||||
(100.01, 100.011),
|
||||
(10000000000000.001 / 10000.0, 1000000000.0000001000),
|
||||
(0.001, 0.0010000001),
|
||||
(0.000000000000000000000101, 0.0),
|
||||
(sqrt(2.0) * sqrt(2.0), 2.0),
|
||||
(-sqrt(2.0) * sqrt(2.0), -2.0),
|
||||
(3.14159265358979323846, 3.14159265358979324));
|
||||
|
||||
for Pair of Table loop
|
||||
Put(Item => Pair(Left), Exp => 0, Aft => 16, Fore => 6);
|
||||
Put(" ");
|
||||
Put(Item => Pair(Right), Exp => 0, Aft => 16, Fore => 6);
|
||||
Put_Line(" " & Boolean'Image(Approx_Equal(Pair(Left), Pair(Right))));
|
||||
end loop;
|
||||
|
||||
end Main;
|
||||
24
Task/Approximate-equality/Crystal/approximate-equality.cr
Normal file
24
Task/Approximate-equality/Crystal/approximate-equality.cr
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
testvalues = [{100000000000000.01, 100000000000000.011},
|
||||
{100.01, 100.011},
|
||||
{10000000000000.001 / 10000.0, 1000000000.0000001000},
|
||||
{0.001, 0.0010000001},
|
||||
{0.000000000000000000000101, 0.0},
|
||||
{(2**0.5) * (2**0.5), 2.0},
|
||||
{-(2**0.5) * (2**0.5), -2.0},
|
||||
{Float64::NAN, Float64::NAN},
|
||||
{Float64::INFINITY, Float64::INFINITY},
|
||||
]
|
||||
|
||||
struct Number
|
||||
def close_to? (num, tol = Float64::EPSILON)
|
||||
return true if self == num
|
||||
return false if (self.to_f.nan? || num.to_f.nan?) # NaN is not even close to itself
|
||||
return false if [self, num].count( Float64::INFINITY) == 1 # Infinity is only close to itself
|
||||
return false if [self, num].count(-Float64::INFINITY) == 1
|
||||
(self-num).abs <= tol * ({self.abs, num.abs}.max)
|
||||
end
|
||||
end
|
||||
|
||||
testvalues.each do |a,b|
|
||||
puts "#{a} #{a.close_to?(b) ? '≈' : '≉'} #{b}"
|
||||
end
|
||||
19
Task/Approximate-equality/ReScript/approximate-equality.res
Normal file
19
Task/Approximate-equality/ReScript/approximate-equality.res
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
let approx_eq = (v1, v2, epsilon) => {
|
||||
abs_float (v1 -. v2) < epsilon
|
||||
}
|
||||
|
||||
let test = (a, b) => {
|
||||
let epsilon = 1e-18
|
||||
Printf.printf("%g, %g => %b\n", a, b, approx_eq(a, b, epsilon))
|
||||
}
|
||||
|
||||
{
|
||||
test(100000000000000.01, 100000000000000.011)
|
||||
test(100.01, 100.011)
|
||||
test(10000000000000.001 /. 10000.0, 1000000000.0000001000)
|
||||
test(0.001, 0.0010000001)
|
||||
test(0.000000000000000000000101, 0.0)
|
||||
test(sqrt(2.0) *. sqrt(2.0), 2.0)
|
||||
test(-. sqrt(2.0) *. sqrt(2.0), (-2.0))
|
||||
test(3.14159265358979323846, 3.14159265358979324)
|
||||
}
|
||||
22
Task/Approximate-equality/V-(Vlang)/approximate-equality.v
Normal file
22
Task/Approximate-equality/V-(Vlang)/approximate-equality.v
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import math
|
||||
|
||||
fn aeq(a f64, b f64) bool {
|
||||
return math.abs(a - b) <= math.abs(a) * 1e-14
|
||||
}
|
||||
|
||||
fn test(a f64, b f64) {
|
||||
print("${a} ${b} -> ")
|
||||
if aeq(a, b) { println("true") }
|
||||
else { println("false") }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
test(100000000000000.01, 100000000000000.011)
|
||||
test(100.01, 100.011)
|
||||
test(10000000000000.001 / 10000, 1000000000.0000001)
|
||||
test(0.001, 0.0010000001)
|
||||
test(1.01e-22, 0)
|
||||
test(math.sqrt(2) * math.sqrt(2), 2)
|
||||
test(-math.sqrt(2) * math.sqrt(2), -2)
|
||||
test(3.14159265358979323846, 3.14159265358979324)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue