June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
16
Task/Safe-addition/D/safe-addition.d
Normal file
16
Task/Safe-addition/D/safe-addition.d
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import std.traits;
|
||||
auto safeAdd(T)(T a, T b)
|
||||
if (isFloatingPoint!T) {
|
||||
import std.math; // nexDown, nextUp
|
||||
import std.typecons; // tuple
|
||||
return tuple!("d", "u")(nextDown(a+b), nextUp(a+b));
|
||||
}
|
||||
|
||||
import std.stdio;
|
||||
void main() {
|
||||
auto a = 1.2;
|
||||
auto b = 0.03;
|
||||
|
||||
auto r = safeAdd(a, b);
|
||||
writefln("(%s + %s) is in the range %0.16f .. %0.16f", a, b, r.d, r.u);
|
||||
}
|
||||
20
Task/Safe-addition/Java/safe-addition.java
Normal file
20
Task/Safe-addition/Java/safe-addition.java
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
public class SafeAddition {
|
||||
private static double stepDown(double d) {
|
||||
return Math.nextAfter(d, Double.NEGATIVE_INFINITY);
|
||||
}
|
||||
|
||||
private static double stepUp(double d) {
|
||||
return Math.nextUp(d);
|
||||
}
|
||||
|
||||
private static double[] safeAdd(double a, double b) {
|
||||
return new double[]{stepDown(a + b), stepUp(a + b)};
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
double a = 1.2;
|
||||
double b = 0.03;
|
||||
double[] result = safeAdd(a, b);
|
||||
System.out.printf("(%.2f + %.2f) is in the range %.16f..%.16f", a, b, result[0], result[1]);
|
||||
}
|
||||
}
|
||||
19
Task/Safe-addition/Julia/safe-addition.julia
Normal file
19
Task/Safe-addition/Julia/safe-addition.julia
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
julia> using IntervalArithmetic
|
||||
|
||||
julia> n = 2.0
|
||||
2.0
|
||||
|
||||
julia> @interval 2n/3 + 1
|
||||
[2.33333, 2.33334]
|
||||
|
||||
julia> showall(ans)
|
||||
Interval(2.333333333333333, 2.3333333333333335)
|
||||
|
||||
julia> a = @interval(0.1, 0.3)
|
||||
[0.0999999, 0.300001]
|
||||
|
||||
julia> b = @interval(0.3, 0.6)
|
||||
[0.299999, 0.600001]
|
||||
|
||||
julia> a + b
|
||||
[0.399999, 0.900001]
|
||||
13
Task/Safe-addition/Scala/safe-addition.scala
Normal file
13
Task/Safe-addition/Scala/safe-addition.scala
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
object SafeAddition extends App {
|
||||
val (a, b) = (1.2, 0.03)
|
||||
val result = safeAdd(a, b)
|
||||
|
||||
private def safeAdd(a: Double, b: Double) = Seq(stepDown(a + b), stepUp(a + b))
|
||||
|
||||
private def stepDown(d: Double) = Math.nextAfter(d, Double.NegativeInfinity)
|
||||
|
||||
private def stepUp(d: Double) = Math.nextUp(d)
|
||||
|
||||
println(f"($a%.2f + $b%.2f) is in the range ${result.head}%.16f .. ${result.last}%.16f")
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue