1.B. Intervals (Coded) - JulTob/Mathematics GitHub Wiki
We sometimes have little trust in a value. We can then use an Interval.
This means we can do operations by the use of an interval around a number.
Here is a nice calculator for intervals
in Ada
language:
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
procedure Interval is
type Interval is record
From: Float := 0.0;
To: Float := 1.0;
end record;
function "+" (Left,Right: Interval) return Interval is
begin
return (Left.From+Right.From, Left.To+Right.To);
end;
function "-" (Right: Interval) return Interval is
begin
return (-Right.To, -Right.From);
end;
function "-" (Left,Right: Interval) return Interval is
begin
return (Left.From - Right.To, Left.To - Right.From );
end;
function "*" (Left: Float;Right: Interval) return Interval is
begin
if Left>0.0 then return (Left* Right.From, Left*Right.To );
elsif Left = 0.0 then return (0.0, 0.0);
else return (Left* Right.To, Left*Right.From );
end if;
end;
function "*" (Left,Right: Interval) return Interval is
a,b,c,d: Float;
from, to : Float;
begin
a := Left.From * Right.From;
from := a; To := a;
b := Left.From * Right.To;
if b<from then from := b; end if;
if b>to then to := b; end if;
c := Left.To * Right.From;
if c<from then from := c; end if;
if c>to then to := c; end if;
d := Left.To * Right.To;
if d<from then from := d; end if;
if d>to then to := d; end if;
return (from, to);
end;
function "and" (Left,Right: Interval) return Interval is
from, to: Float;
begin
if Left.from<Right.from then from := Left.from; else from := Right.from; end if;
if Left.To>Right.To then to := Left.to; else to := Right.to; end if;
return (Left, Right);
end;
-- Give The Value of the Intervals
A: Interval := (-2.0,2.0);
B: Interval := (-1.0,3.0);
Z: Interval;
begin
-- Change the operation here with the options given above
Z := A+B;
Put("A + B = ");
Put(Z.from);
Put(Z.To);
New_Line;
end Interval;