Safe addition: Difference between revisions

From Rosetta Code
Content added Content deleted
(simplify)
m (→‎{{header|Ada}}: formatting adjustment)
Line 11: Line 11:
=={{header|Ada}}==
=={{header|Ada}}==
An interval type based on Float:
An interval type based on Float:
<lang Ada>
<lang Ada>type Interval is record
type Interval is record
Lower : Float;
Lower : Float;
Upper : Float;
Upper : Float;
end record;
end record;</lang>
</lang>
Implementation of safe addition:
Implementation of safe addition:
<lang Ada>function "+" (A, B : Float) return Interval is
<lang Ada>
function "+" (A, B : Float) return Interval is
Result : constant Float := A + B;
Result : constant Float := A + B;
begin
begin
Line 37: Line 34:
return (Float'Adjacent (0.0, Float'First), Float'Adjacent (0.0, Float'Last));
return (Float'Adjacent (0.0, Float'First), Float'Adjacent (0.0, Float'Last));
end if;
end if;
end "+";
end "+";</lang>
</lang>
The implementation uses the attribute T'Machine_Rounds to determine if rounding is performed on inexact results. If the machine rounds a symmetric interval around the result is used. When the machine does not round, it truncates. Truncating is rounding towards zero. In this case the implementation is twice better (in terms of the result interval width), because depending on its sign, the outcome of addition can be taken for either of the bounds. Unfortunately most of modern processors are rounding.
The implementation uses the attribute T'Machine_Rounds to determine if rounding is performed on inexact results. If the machine rounds a symmetric interval around the result is used. When the machine does not round, it truncates. Truncating is rounding towards zero. In this case the implementation is twice better (in terms of the result interval width), because depending on its sign, the outcome of addition can be taken for either of the bounds. Unfortunately most of modern processors are rounding.


Test program:
Test program:
<lang Ada>
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Interval_Addition is
procedure Test_Interval_Addition is
-- Definitions from above
-- Definitions from above
Line 52: Line 47:
begin
begin
Put (1.14 + 2000.0);
Put (1.14 + 2000.0);
end Test_Interval_Addition;
end Test_Interval_Addition;</lang>
</lang>
Sample output:
Sample output:
<pre> 2.00113989257813E+03, 2.00114013671875E+03</pre>
<pre>
2.00113989257813E+03, 2.00114013671875E+03
</pre>


=={{header|E}}==
=={{header|E}}==

Revision as of 11:48, 17 August 2009

Task
Safe addition
You are encouraged to solve this task according to the task description, using any language you may know.

Implementation of interval arithmetic and more generally fuzzy number arithmetic require operations that yield safe upper and lower bounds of the exact result. For example, for an addition, it is the operations *↑ and *↓ defined as: a +↓ ba + ba +↑ b. Additionally it is desired that the width of the interval (a +↑ b) - (a +↓ b) would be about the machine epsilon after removing the exponent part.

Differently to the standard floating-point arithmetic, safe interval arithmetic is accurate (but still imprecise). I.e. the result of each defined operation contains (though does not identify) the exact mathematical outcome.

Usually a FPU's have machine +,-,*,/ operations accurate within the machine precision. To illustrate it, let us consider a machine with decimal floating-point arithmetic that has the precision is 3 decimal points. If the result of the machine addition is 1.23, then the exact mathematical result is within the interval ]1.22, 1.24[. When the machine rounds towards zero, then the exact result is within [1.23,1.24[. This is the basis for an implementation of safe addition.

Task

Show how +↓ and +↑ can be implemented in your language using the standard floating-point type. Define an interval type based on the standard floating-point one, and implement an interval-valued addition of two floating-point numbers considering them exact, in short an operation that yields the interval [a +↓ b, a +↑ b].

Ada

An interval type based on Float: <lang Ada>type Interval is record

  Lower : Float;
  Upper : Float;

end record;</lang> Implementation of safe addition: <lang Ada>function "+" (A, B : Float) return Interval is

  Result : constant Float := A + B;

begin

  if Result < 0.0 then
     if Float'Machine_Rounds then
        return (Float'Adjacent (Result, Float'First), Float'Adjacent (Result, 0.0));
     else
        return (Float'Adjacent (Result, Float'First), Result);
     end if;         
  elsif Result > 0.0 then   
     if Float'Machine_Rounds then
        return (Float'Adjacent (Result, 0.0), Float'Adjacent (Result, Float'Last));
     else
        return (Result, Float'Adjacent (Result, Float'Last));
     end if;         
  else -- Underflow
     return (Float'Adjacent (0.0, Float'First), Float'Adjacent (0.0, Float'Last));
  end if;

end "+";</lang> The implementation uses the attribute T'Machine_Rounds to determine if rounding is performed on inexact results. If the machine rounds a symmetric interval around the result is used. When the machine does not round, it truncates. Truncating is rounding towards zero. In this case the implementation is twice better (in terms of the result interval width), because depending on its sign, the outcome of addition can be taken for either of the bounds. Unfortunately most of modern processors are rounding.

Test program: <lang Ada>with Ada.Text_IO; use Ada.Text_IO; procedure Test_Interval_Addition is

  -- Definitions from above
  procedure Put (I : Interval) is
  begin
     Put (Long_Float'Image (Long_Float (I.Lower)) & "," & Long_Float'Image (Long_Float (I.Upper)));
  end Put;

begin

  Put (1.14 + 2000.0);

end Test_Interval_Addition;</lang> Sample output:

 2.00113989257813E+03, 2.00114013671875E+03

E

[Note: this task has not yet had attention from a floating-point expert.]

In E, operators are defined on the left object involved (they have to be somewhere, as global definitions violate capability principles); this implies that I can't define + such that aFloat + anotherFloat yields an Interval. Instead, I shall define an Interval with its +, but restrict + (for the sake of this example) to intervals containing only one number.

(E has a built-in numeric interval type as well, but it is always closed-open rather than closed-closed and so would be particularly confusing for this task.)

E currently inherits Java's choices of IEEE floating point behavior, including round to nearest. Therefore, as in the Ada example, given ignorance of the actual direction of rounding, we must take one step away in both directions to get a correct interval.

<lang e>def makeInterval(a :float64, b :float64) {

   require(a <= b)
   def interval {
       to least() { return a }
       to greatest() { return b }
       to __printOn(out) {
           out.print("[", a, ", ", b, "]")
       }
       to add(other) {
           require(a <=> b)
           require(other.least() <=> other.greatest())
           def result := a + other.least()
           return makeInterval(result.previous(), result.next())
       }
   }
   return interval

}</lang>

<lang e>? makeInterval(1.14, 1.14) + makeInterval(2000.0, 2000.0)

  1. value: [2001.1399999999999, 2001.1400000000003]</lang>

E provides only 64-bit "double precision" floats, and always prints them with sufficient decimal places to reproduce the original floating point number exactly.