comp.lang.ada
 help / color / mirror / Atom feed
* Epsilon
@ 2008-05-12 14:56 adaworks
  2008-05-13 11:42 ` Epsilon Stephen Leake
  0 siblings, 1 reply; 4+ messages in thread
From: adaworks @ 2008-05-12 14:56 UTC (permalink / raw)


I am looking for some short, practical examples of the Epsilon
attribute.  If you do have some source code examples to post,
please also send them to my academic email:  rdriehle@nps.edu

Richard Riehle
Naval Postgraduate School 





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Epsilon
  2008-05-12 14:56 Epsilon adaworks
@ 2008-05-13 11:42 ` Stephen Leake
  2008-05-14  1:02   ` Epsilon adaworks
  0 siblings, 1 reply; 4+ messages in thread
From: Stephen Leake @ 2008-05-13 11:42 UTC (permalink / raw)


<adaworks@sbcglobal.net> writes:

> I am looking for some short, practical examples of the Epsilon
> attribute.  If you do have some source code examples to post,
> please also send them to my academic email:  rdriehle@nps.edu

   --  These values guarantee Integer_16 (value) or Unsigned_16
   --  (value) won't raise Constraint_Error, and allow for maximum
   --  round-off error. Useful with Clip_Scale_Limit when result will
   --  be converted to Integer_16 or Unsigned_16.
   Integer_16_First_Real : constant Real_Type := Real_Type (Interfaces.Integer_16'First) - 0.5 +
     (Real_Type'Epsilon * Real_Type (Interfaces.Integer_16'Last));

   Integer_16_Last_Real  : constant Real_Type := Real_Type (Interfaces.Integer_16'Last) + 0.5 -
     (Real_Type'Epsilon * Real_Type (Interfaces.Integer_16'Last));

   Unsigned_16_First_Real : constant Real_Type := -0.5 + Real_Type'Epsilon;
   Unsigned_16_Last_Real  : constant Real_Type := Real_Type (Interfaces.Unsigned_16'Last) + 0.5 -
     (Real_Type'Epsilon * Real_Type (Interfaces.Unsigned_16'Last));


   function First_Order_Trig return Real_Type
   is begin
      return Elementary.Sqrt (Real_Type'Model_Epsilon);
   end First_Order_Trig;

   function Half_Trig (Trig : in Trig_Pair_Type) return Trig_Pair_Type
   is
      --  The result Trig.Cos is >= 0.0.
      --
      --  A linear approximation is used when Trig.Sin <
      --  First_Order_Trig. this is exact since cos x = 1 - x**2 for
      --  this range of x.
   begin
      if abs Trig.Sin < First_Order_Trig then
         --  angle near 0 or Pi.
         if Trig.Cos > 0.0 then
            --  angle near 0
            return (Trig.Sin / 2.0, 1.0);
         else    -- angle near Pi
            if Trig.Sin >= 0.0 then
               return (1.0 - Trig.Sin / 2.0, 0.0);
            else
               return (-1.0 + Trig.Sin / 2.0, 0.0);
            end if;
         end if;
      else    -- angle not near 0 or Pi
         if Trig.Sin >= 0.0 then
            return (Elementary.Sqrt ((1.0 - Trig.Cos) / 2.0), Elementary.Sqrt ((1.0 + Trig.Cos) / 2.0));
         else
            return (-Elementary.Sqrt ((1.0 - Trig.Cos) / 2.0), Elementary.Sqrt ((1.0 + Trig.Cos) / 2.0));
         end if;
      end if;
   end Half_Trig;

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Epsilon
  2008-05-13 11:42 ` Epsilon Stephen Leake
@ 2008-05-14  1:02   ` adaworks
  2008-05-14  9:58     ` Epsilon Stephen Leake
  0 siblings, 1 reply; 4+ messages in thread
From: adaworks @ 2008-05-14  1:02 UTC (permalink / raw)


Stephen,

Very clever.   Thank you.

Richard Riehle
============================================================
"Stephen Leake" <Stephe.Leake@nasa.gov> wrote in message 
news:umymuph04.fsf@nasa.gov...
> <adaworks@sbcglobal.net> writes:
>
>> I am looking for some short, practical examples of the Epsilon
>> attribute.  If you do have some source code examples to post,
>> please also send them to my academic email:  rdriehle@nps.edu
>
>   --  These values guarantee Integer_16 (value) or Unsigned_16
>   --  (value) won't raise Constraint_Error, and allow for maximum
>   --  round-off error. Useful with Clip_Scale_Limit when result will
>   --  be converted to Integer_16 or Unsigned_16.
>   Integer_16_First_Real : constant Real_Type := Real_Type 
> (Interfaces.Integer_16'First) - 0.5 +
>     (Real_Type'Epsilon * Real_Type (Interfaces.Integer_16'Last));
>
>   Integer_16_Last_Real  : constant Real_Type := Real_Type 
> (Interfaces.Integer_16'Last) + 0.5 -
>     (Real_Type'Epsilon * Real_Type (Interfaces.Integer_16'Last));
>
>   Unsigned_16_First_Real : constant Real_Type := -0.5 + Real_Type'Epsilon;
>   Unsigned_16_Last_Real  : constant Real_Type := Real_Type 
> (Interfaces.Unsigned_16'Last) + 0.5 -
>     (Real_Type'Epsilon * Real_Type (Interfaces.Unsigned_16'Last));
>
>
>   function First_Order_Trig return Real_Type
>   is begin
>      return Elementary.Sqrt (Real_Type'Model_Epsilon);
>   end First_Order_Trig;
>
>   function Half_Trig (Trig : in Trig_Pair_Type) return Trig_Pair_Type
>   is
>      --  The result Trig.Cos is >= 0.0.
>      --
>      --  A linear approximation is used when Trig.Sin <
>      --  First_Order_Trig. this is exact since cos x = 1 - x**2 for
>      --  this range of x.
>   begin
>      if abs Trig.Sin < First_Order_Trig then
>         --  angle near 0 or Pi.
>         if Trig.Cos > 0.0 then
>            --  angle near 0
>            return (Trig.Sin / 2.0, 1.0);
>         else    -- angle near Pi
>            if Trig.Sin >= 0.0 then
>               return (1.0 - Trig.Sin / 2.0, 0.0);
>            else
>               return (-1.0 + Trig.Sin / 2.0, 0.0);
>            end if;
>         end if;
>      else    -- angle not near 0 or Pi
>         if Trig.Sin >= 0.0 then
>            return (Elementary.Sqrt ((1.0 - Trig.Cos) / 2.0), Elementary.Sqrt 
> ((1.0 + Trig.Cos) / 2.0));
>         else
>            return (-Elementary.Sqrt ((1.0 - Trig.Cos) / 2.0), Elementary.Sqrt 
> ((1.0 + Trig.Cos) / 2.0));
>         end if;
>      end if;
>   end Half_Trig;
>
> -- 
> -- Stephe 





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Epsilon
  2008-05-14  1:02   ` Epsilon adaworks
@ 2008-05-14  9:58     ` Stephen Leake
  0 siblings, 0 replies; 4+ messages in thread
From: Stephen Leake @ 2008-05-14  9:58 UTC (permalink / raw)


<adaworks@sbcglobal.net> writes:

> Very clever.   Thank you.

Thanks.

You could argue that 'machine_epsilon is better in both of these uses.
I didn't really study the issue.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-05-14  9:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-12 14:56 Epsilon adaworks
2008-05-13 11:42 ` Epsilon Stephen Leake
2008-05-14  1:02   ` Epsilon adaworks
2008-05-14  9:58     ` Epsilon Stephen Leake

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox