comp.lang.ada
 help / color / mirror / Atom feed
* Floating point accuracy
@ 2001-02-15 11:27 Martin Dowie
  2001-02-15 14:40 ` Ted Dennison
  2001-02-15 15:22 ` M. Kotiaho
  0 siblings, 2 replies; 7+ messages in thread
From: Martin Dowie @ 2001-02-15 11:27 UTC (permalink / raw)


I'm trying to mimic 64-bit unsigned integers (that will represent
nanoseconds) on a
PowerPC (Green Hills target compiler for the PowerPC doesn't support 64 bit
unsigned directly - unless you know differently!).

I'm doing this by having a most sigificant and least significant 32bit
unsigned in what will
eventually be a private record and providing my own operators. However my
routine to
convert from microseconds to nanoseconds is puzzling me. I had always
thought that a
float declared a 'digits 15' would give me 15 significant digits of
accuracy - I'm sure I'm
only using up to 13 in these test, yet the last two results return the same
values.

clearly, I'm missing something...
(on both WinNT/Gnat 3.13p & PPC/GreenHills/VxWorks5.4)


Program =
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces;

procedure ns_test is

   type ns_type is record
      MS, LS : Interfaces.Unsigned_32;
   end record;

   type Real_15_Type is digits 15;

   function us_to_ns (Microseconds : Interfaces.Unsigned_32) return ns_Type
is
      -- 16#FFFF_FFFF# = (2^32)-1 = 10#4_294_967_295#, which has 10
significant digits
      -- multiply by 1,000 (to convert to nanoseconds) =>
      -- 10#4_294_967_295_000#, which has 13 significant digits =>
      -- Real_15_Type has enough significant digits to represent the value
in ns.
      --
      Two_To_The_Thirty_Two : constant Real_15_Type := 2.0 ** 32;
      Float_ns : constant Real_15_Type := Real_15_Type (Microseconds) *
1_000.0;
      Float_MS : constant Real_15_Type := Real_15_Type'Floor (Float_ns /
Two_To_The_Thirty_Two);
      MS : constant Real_15_Type := (Float_MS * Two_To_The_Thirty_Two);
      Float_LS : constant Real_15_Type := Float_ns - MS;
   begin
      Put_line ("ns = " & Real_15_Type'Image (Float_ns));
      Put_line ("ms = " & Real_15_Type'Image (Float_MS));
      Put_line ("MS = " & Real_15_Type'Image (MS));
      Put_line ("ls = " & Real_15_Type'Image (Float_LS));
      New_Line;
      return (MS => Interfaces.Unsigned_32 (Float_MS), LS =>
Interfaces.Unsigned_32 (Float_LS));
   end us_to_ns;

   procedure display (value : ns_type) is
   begin
     put_line ("ms =>" & Interfaces.Unsigned_32'Image (Value.MS) & " ls =>"
& Interfaces.Unsigned_32'Image (Value.LS));
   end display;

begin
   new_line;
   put_line ("test 0");
   display (value => us_to_ns (microseconds => 0));
   put_line ("test 1");
   display (value => us_to_ns (microseconds => 1));
   put_line ("test 1,000");
   display (value => us_to_ns (microseconds => 1_000));
   put_line ("test 16#0FFF_FFFF#");
   display (value => us_to_ns (microseconds => 16#0FFF_FFFF#));  -- random
mid-value
   put_line ("test 'Last - 1");
   display (value => us_to_ns (microseconds => Interfaces.Unsigned_32'Pred
(Interfaces.Unsigned_32'Last)));
   put_line ("test 'Last");
   display (value => us_to_ns (microseconds =>
Interfaces.Unsigned_32'Last));
end ns_test;


Results =
->
test 0
ns =  0.00000000000000E+00
ms =  0.00000000000000E+00
MS =  0.00000000000000E+00
ls =  0.00000000000000E+00

ms => 0 ls => 0
test 1
ns =  1.00000000000000E+03
ms =  0.00000000000000E+00
MS =  0.00000000000000E+00
ls =  1.00000000000000E+03

ms => 0 ls => 1000
test 1,000
ns =  1.00000000000000E+06
ms =  0.00000000000000E+00
MS =  0.00000000000000E+00
ls =  1.00000000000000E+06

ms => 0 ls => 1000000
test 16#0FFF_FFFF#
ns =  2.68435455000000E+11
ms =  6.20000000000000E+01
MS =  2.66287972352000E+11
ls =  2.14748264800000E+09

ms => 62 ls => 2147482648
test 'Last - 1
ns =  4.29496729400000E+12
ms =  9.99000000000000E+02
MS =  4.29067232870400E+12
ls =  4.29496529600000E+09

ms => 999 ls => 4294965296
test 'Last
ns =  4.29496729500000E+12
ms =  9.99000000000000E+02
MS =  4.29067232870400E+12
ls =  4.29496629600000E+09

ms => 999 ls => 4294966296






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

* Re: Floating point accuracy
  2001-02-15 11:27 Floating point accuracy Martin Dowie
@ 2001-02-15 14:40 ` Ted Dennison
  2001-02-15 15:19   ` Martin Dowie
  2001-02-15 15:22 ` M. Kotiaho
  1 sibling, 1 reply; 7+ messages in thread
From: Ted Dennison @ 2001-02-15 14:40 UTC (permalink / raw)


In article <3a8bbb84$1@pull.gecm.com>, Martin Dowie says...
>
>I'm trying to
mimic 64-bit unsigned integers (that will represent
>nanoseconds) on a
>PowerPC
(Green Hills target compiler for the PowerPC doesn't support 64 bit
>unsigned
directly - unless you know differently!).

Out of curiositiy, what are you doing
with your own time unit that
Ada.Real_Time.Time can't do for you?



---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com




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

* Re: Floating point accuracy
  2001-02-15 14:40 ` Ted Dennison
@ 2001-02-15 15:19   ` Martin Dowie
  0 siblings, 0 replies; 7+ messages in thread
From: Martin Dowie @ 2001-02-15 15:19 UTC (permalink / raw)


I didn't say anything about using it for timing... :-)

Ted Dennison <dennison@telepath.com> wrote in message
news:hTRi6.211$a4.1407@www.newsranger.com...
> [snip]
>
> Out of curiositiy, what are you doing
> with your own time unit that
> Ada.Real_Time.Time can't do for you?
>
>
>
> ---
> T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
>           home email - mailto:dennison@telepath.com
>





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

* Re: Floating point accuracy
  2001-02-15 11:27 Floating point accuracy Martin Dowie
  2001-02-15 14:40 ` Ted Dennison
@ 2001-02-15 15:22 ` M. Kotiaho
  2001-02-15 16:22   ` Martin Dowie
  1 sibling, 1 reply; 7+ messages in thread
From: M. Kotiaho @ 2001-02-15 15:22 UTC (permalink / raw)


Martin Dowie wrote:

> I'm trying to mimic 64-bit unsigned integers (that will represent
> nanoseconds) on a
> PowerPC (Green Hills target compiler for the PowerPC doesn't support 64 bit
> unsigned directly - unless you know differently!).
>
> I'm doing this by having a most sigificant and least significant 32bit
> unsigned in what will
> eventually be a private record and providing my own operators. However my
> routine to
> convert from microseconds to nanoseconds is puzzling me. I had always
> thought that a
> float declared a 'digits 15' would give me 15 significant digits of
> accuracy - I'm sure I'm
> only using up to 13 in these test, yet the last two results return the same
> values.
>
> clearly, I'm missing something...

Yes ... look more closely at your results ... the values you print for "ns"
differ in one digit (last non-zero digit).  The values for "ls" are also
different
(the last value has ...66..., the previous one has ...65...).  It is only for
"ms"
and "MS" that the values are the same.

You should not expect otherwise, since you are losing information when
you take the 'floor operation in computing Float_MS.  It has nothing to do
with the number of significant digits (since you have enough).

Here is the math:

'Last - 1:
Float_MS = Floor( (2**32 - 2)*1000 / 2**32 ) = Floor( 1000 - 2000/2**32 ) = 999

'Last:
Float_MS = Floor( (2**32 - 1)*1000 / 2**32 ) = Floor( 1000 - 1000/2**32 ) = 999

...<snip>...

>
> test 'Last - 1
> ns =  4.29496729400000E+12
> ms =  9.99000000000000E+02
> MS =  4.29067232870400E+12
> ls =  4.29496529600000E+09
>
> ms => 999 ls => 4294965296
> test 'Last
> ns =  4.29496729500000E+12
> ms =  9.99000000000000E+02
> MS =  4.29067232870400E+12
> ls =  4.29496629600000E+09
>
> ms => 999 ls => 4294966296

Regards,
Markku Kotiaho






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

* Re: Floating point accuracy
  2001-02-15 15:22 ` M. Kotiaho
@ 2001-02-15 16:22   ` Martin Dowie
  2001-02-15 17:23     ` M. Kotiaho
  0 siblings, 1 reply; 7+ messages in thread
From: Martin Dowie @ 2001-02-15 16:22 UTC (permalink / raw)


doh!
It's my eyes that don't have the necessary significant digits!

As Markku points out, the values for "ls" _are_ different - just
not where I had expected! In fact they are exactly 1000ns different,
as I should have expected (the last 3 digits are the same, and
confusing me!)

But, Markku, the 'Floor is doing exactly what I want it to do... :-)


"M. Kotiaho" <kotiaho@m_a_i_l.com> wrote in message
news:3A8BF422.69723366@m_a_i_l.com...
> Martin Dowie wrote:
>
> Yes ... look more closely at your results ... the values you print for
"ns"
> differ in one digit (last non-zero digit).  The values for "ls" are also
> different
> (the last value has ...66..., the previous one has ...65...).  It is only
for
> "ms"
> and "MS" that the values are the same.
>
> You should not expect otherwise, since you are losing information when
> you take the 'floor operation in computing Float_MS.  It has nothing to do
> with the number of significant digits (since you have enough).
>
> Here is the math:
>
> 'Last - 1:
> Float_MS = Floor( (2**32 - 2)*1000 / 2**32 ) = Floor( 1000 - 2000/2**32 )
= 999
>
> 'Last:
> Float_MS = Floor( (2**32 - 1)*1000 / 2**32 ) = Floor( 1000 - 1000/2**32 )
= 999
>
> ...<snip>...
>
> >
> > test 'Last - 1
> > ns =  4.29496729400000E+12
> > ms =  9.99000000000000E+02
> > MS =  4.29067232870400E+12
> > ls =  4.29496529600000E+09
> >
> > ms => 999 ls => 4294965296
> > test 'Last
> > ns =  4.29496729500000E+12
> > ms =  9.99000000000000E+02
> > MS =  4.29067232870400E+12
> > ls =  4.29496629600000E+09
> >
> > ms => 999 ls => 4294966296
>
> Regards,
> Markku Kotiaho
>
>
>





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

* Re: Floating point accuracy
  2001-02-15 16:22   ` Martin Dowie
@ 2001-02-15 17:23     ` M. Kotiaho
  2001-02-15 17:47       ` Martin Dowie
  0 siblings, 1 reply; 7+ messages in thread
From: M. Kotiaho @ 2001-02-15 17:23 UTC (permalink / raw)


Martin Dowie wrote:

> doh!
> It's my eyes that don't have the necessary significant digits!
>
> As Markku points out, the values for "ls" _are_ different - just
> not where I had expected! In fact they are exactly 1000ns different,
> as I should have expected (the last 3 digits are the same, and
> confusing me!)
>
> But, Markku, the 'Floor is doing exactly what I want it to do... :-)
>

Fair enough ... I figured I would try to explain why the values that
actually *are* the same, er, are the same.  That was more fun than
just telling you to take another look at your data.

FWIW, I think that GNAT for PPC/VxWorks does support 64 bit
unsigned integers.

MK







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

* Re: Floating point accuracy
  2001-02-15 17:23     ` M. Kotiaho
@ 2001-02-15 17:47       ` Martin Dowie
  0 siblings, 0 replies; 7+ messages in thread
From: Martin Dowie @ 2001-02-15 17:47 UTC (permalink / raw)


Yes, but we must be portable and Green Hills (and others) do not...

> FWIW, I think that GNAT for PPC/VxWorks does support 64 bit
> unsigned integers.






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

end of thread, other threads:[~2001-02-15 17:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-02-15 11:27 Floating point accuracy Martin Dowie
2001-02-15 14:40 ` Ted Dennison
2001-02-15 15:19   ` Martin Dowie
2001-02-15 15:22 ` M. Kotiaho
2001-02-15 16:22   ` Martin Dowie
2001-02-15 17:23     ` M. Kotiaho
2001-02-15 17:47       ` Martin Dowie

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