comp.lang.ada
 help / color / mirror / Atom feed
* Fixed Point number mul, is it a bug?
@ 2012-10-18  7:11 kylix
  2012-10-18  7:35 ` Vinzent Höfler
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: kylix @ 2012-10-18  7:11 UTC (permalink / raw)


-- GNAT GPL 2012 
with Ada.Text_IO;
procedure FixPoint is
   type FP is delta 0.01 range 0.00 .. 99.99;
   -- type FP is delta 0.01 digits 4;
   x : FP := 0.01;
begin
   for i in 1 .. 5 loop
      x := x * 2;
      Ada.Text_IO.Put_Line("x =>" & FP'Image(x)); 
   end loop;
end Fixpoint;

In my machine, it yield results:

x => 0.02
x => 0.03
x => 0.06
x => 0.13
x => 0.25

Why not: 0.02 0.04 0.08 0.16 0.32 ?

If FP declared as "type FP is delta 0.01 digits 4",
it yield expected results.





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

* Re: Fixed Point number mul, is it a bug?
  2012-10-18  7:11 Fixed Point number mul, is it a bug? kylix
@ 2012-10-18  7:35 ` Vinzent Höfler
  2012-10-18 14:49   ` Adam Beneschan
  2012-10-18  7:57 ` Dmitry A. Kazakov
  2012-10-18 14:04 ` Shark8
  2 siblings, 1 reply; 11+ messages in thread
From: Vinzent Höfler @ 2012-10-18  7:35 UTC (permalink / raw)


kylix <likai3g@gmail.com> wrote:

> -- GNAT GPL 2012 
> with Ada.Text_IO;
> procedure FixPoint is
>    type FP is delta 0.01 range 0.00 .. 99.99;
>    -- type FP is delta 0.01 digits 4;
>    x : FP := 0.01;
> begin
>    for i in 1 .. 5 loop
>       x := x * 2;
>       Ada.Text_IO.Put_Line("x =>" &
>.                            FP'Image(x)); 
>    end loop;
> end Fixpoint;

> In my machine, it yield results:

> x => 0.02
> x => 0.03
> x => 0.06
> x => 0.13
> x => 0.25

This is expected. The definition requests a fixed point type with a 
delta of at least 0.01, most compilers would choose 2**-7 then, which 
is 0.0078125 in decimal notation. Hence the results you see.

> Why not: 0.02 0.04 0.08 0.16 0.32 ?

Because 0.01 is not representable in a finite, binary number.

> If FP declared as
> "type FP is delta 0.01 digits 4",
> it yield expected results.

Yes. In that case you're requesting a decimal type which is required 
to be exact.

So, no, this is not a bug.


Vinzent.



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

* Re: Fixed Point number mul, is it a bug?
  2012-10-18  7:11 Fixed Point number mul, is it a bug? kylix
  2012-10-18  7:35 ` Vinzent Höfler
@ 2012-10-18  7:57 ` Dmitry A. Kazakov
  2012-10-18 10:34   ` Georg Bauhaus
  2012-10-18 10:42   ` Ian Clifton
  2012-10-18 14:04 ` Shark8
  2 siblings, 2 replies; 11+ messages in thread
From: Dmitry A. Kazakov @ 2012-10-18  7:57 UTC (permalink / raw)


On Thu, 18 Oct 2012 00:11:32 -0700 (PDT), kylix wrote:

> -- GNAT GPL 2012 
> with Ada.Text_IO;
> procedure FixPoint is
>    type FP is delta 0.01 range 0.00 .. 99.99;
>    -- type FP is delta 0.01 digits 4;
>    x : FP := 0.01;
> begin
>    for i in 1 .. 5 loop
>       x := x * 2;
>       Ada.Text_IO.Put_Line("x =>" & FP'Image(x)); 
>    end loop;
> end Fixpoint;
> 
> In my machine, it yield results:
> 
> x => 0.02
> x => 0.03
> x => 0.06
> x => 0.13
> x => 0.25
> 
> Why not: 0.02 0.04 0.08 0.16 0.32 ?

If you try this one:

   Ada.Text_IO.Put_Line("List:");
   loop
      Ada.Text_IO.Put_Line("x =>" & FP'Image(x));
      exit when x = FP'Last;
      x := FP'Succ (x);
   end loop;

you will see what is going on. A binary fixed point type has values which
are not necessarily exact decimal ones. In RM's language it would read as
"the small" is not a power of 10. The small is the difference between two
adjacent values.

> If FP declared as "type FP is delta 0.01 digits 4",
> it yield expected results.

Because this makes it decimal = the small is a power of 10 = values are
exactly decimal. See 3.5.9(9)

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Fixed Point number mul, is it a bug?
  2012-10-18  7:57 ` Dmitry A. Kazakov
@ 2012-10-18 10:34   ` Georg Bauhaus
  2012-10-18 10:42   ` Ian Clifton
  1 sibling, 0 replies; 11+ messages in thread
From: Georg Bauhaus @ 2012-10-18 10:34 UTC (permalink / raw)


On 18.10.12 09:57, Dmitry A. Kazakov wrote:
> A binary fixed point type has values which
> are not necessarily exact decimal ones. In RM's language it would read as
> "the small" is not a power of 10.


And if GNAT is with -gnatwa (all warnings), it will mention the issue.



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

* Re: Fixed Point number mul, is it a bug?
  2012-10-18  7:57 ` Dmitry A. Kazakov
  2012-10-18 10:34   ` Georg Bauhaus
@ 2012-10-18 10:42   ` Ian Clifton
  2012-10-18 12:37     ` Dmitry A. Kazakov
  2012-10-18 14:40     ` Adam Beneschan
  1 sibling, 2 replies; 11+ messages in thread
From: Ian Clifton @ 2012-10-18 10:42 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

> On Thu, 18 Oct 2012 00:11:32 -0700 (PDT), kylix wrote:
>
>> -- GNAT GPL 2012 
>> with Ada.Text_IO;
>> procedure FixPoint is
>>    type FP is delta 0.01 range 0.00 .. 99.99;
>>    -- type FP is delta 0.01 digits 4;
>>    x : FP := 0.01;
>> begin
>>    for i in 1 .. 5 loop
>>       x := x * 2;
>>       Ada.Text_IO.Put_Line("x =>" & FP'Image(x)); 
>>    end loop;
>> end Fixpoint;
>> 
>> In my machine, it yield results:
>> 
>> x => 0.02
>> x => 0.03
>> x => 0.06
>> x => 0.13
>> x => 0.25
>> 
>> Why not: 0.02 0.04 0.08 0.16 0.32 ?
>
> If you try this one:
>
>    Ada.Text_IO.Put_Line("List:");
>    loop
>       Ada.Text_IO.Put_Line("x =>" & FP'Image(x));
>       exit when x = FP'Last;
>       x := FP'Succ (x);
>    end loop;
>
> you will see what is going on. A binary fixed point type has values which
> are not necessarily exact decimal ones. In RM's language it would read as
> "the small" is not a power of 10. The small is the difference between two
> adjacent values.
>
>> If FP declared as "type FP is delta 0.01 digits 4",
>> it yield expected results.
>
> Because this makes it decimal = the small is a power of 10 = values are
> exactly decimal. See 3.5.9(9)

Didn’t some IBM machines have a floating‐point format where the exponent
was decimal rather than binary?
-- 
Ian ◎



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

* Re: Fixed Point number mul, is it a bug?
  2012-10-18 10:42   ` Ian Clifton
@ 2012-10-18 12:37     ` Dmitry A. Kazakov
  2012-10-19 22:58       ` Ian Clifton
  2012-10-18 14:40     ` Adam Beneschan
  1 sibling, 1 reply; 11+ messages in thread
From: Dmitry A. Kazakov @ 2012-10-18 12:37 UTC (permalink / raw)


On Thu, 18 Oct 2012 11:42:26 +0100, Ian Clifton wrote:

> Didn’t some IBM machines have a floating‐point format where the exponent
> was decimal rather than binary?

http://en.wikipedia.org/wiki/Binary-coded_decimal#IBM_and_BCD

An Ada compiler could use the machine's decimal number.

However, AFAIK, fixed point arithmetic in any radix does not need hardware
support beyond plain integers. E.g. Small could even be power of Pi, if
anybody wanted that. It should influence only conversions to other numeric
types not the arithmetic itself.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Fixed Point number mul, is it a bug?
  2012-10-18  7:11 Fixed Point number mul, is it a bug? kylix
  2012-10-18  7:35 ` Vinzent Höfler
  2012-10-18  7:57 ` Dmitry A. Kazakov
@ 2012-10-18 14:04 ` Shark8
  2 siblings, 0 replies; 11+ messages in thread
From: Shark8 @ 2012-10-18 14:04 UTC (permalink / raw)


Try:

    Package FP is
	Type Fixed is delta 0.01 range 0.00 .. 99.99;
	
    Private
	For Fixed'Small use 0.01;
    End FP;

Then:
   Declare
	use type FP.Fixed;
	X : FP.Fixed:= 0.01;
   Begin
	
	For index in 1..5 loop
	    x := x * 2;
	    Ada.Text_IO.Put_Line( "x =>" & X'Img ); 
	end loop;
   
   End;

Yields:
x => 0.02
x => 0.04
x => 0.08
x => 0.16
x => 0.32

(I'm using GNAT on a Windows/AMD.)



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

* Re: Fixed Point number mul, is it a bug?
  2012-10-18 10:42   ` Ian Clifton
  2012-10-18 12:37     ` Dmitry A. Kazakov
@ 2012-10-18 14:40     ` Adam Beneschan
  1 sibling, 0 replies; 11+ messages in thread
From: Adam Beneschan @ 2012-10-18 14:40 UTC (permalink / raw)


On Thursday, October 18, 2012 3:42:26 AM UTC-7, Ian Clifton wrote:

> Didn’t some IBM machines have a floating‐point format where the exponent
> was decimal rather than binary?

I don't know about IBM, but my first programming job was on a Burroughs mainframe (B3500, I think) that represented *all* numbers in decimal--integer, floating-point, everything.  I don't even think it had any binary arithmetic instructions (although I think it did have bitwise AND, OR, XOR operations).  Even operand addresses in instructions were in decimal (hex digits A-F in certain positions in an address signified some special indexing mode, hex digits in other places would just cause a fault).

                         -- Adam




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

* Re: Fixed Point number mul, is it a bug?
  2012-10-18  7:35 ` Vinzent Höfler
@ 2012-10-18 14:49   ` Adam Beneschan
  2012-10-19  9:09     ` AdaMagica
  0 siblings, 1 reply; 11+ messages in thread
From: Adam Beneschan @ 2012-10-18 14:49 UTC (permalink / raw)


On Thursday, October 18, 2012 12:35:38 AM UTC-7, Vinzent Höfler wrote:
> kylix wrote:
> 
> 
> 
> > -- GNAT GPL 2012 
> 
> > with Ada.Text_IO;
> 
> > procedure FixPoint is
> >    type FP is delta 0.01 range 0.00 .. 99.99;
> >    -- type FP is delta 0.01 digits 4;
> >    x : FP := 0.01;
> > begin
> >    for i in 1 .. 5 loop
> >       x := x * 2;
> >       Ada.Text_IO.Put_Line("x =>" &
> >                            FP'Image(x)); 
> >    end loop;
> > end Fixpoint;
> 
> 
> 
> > In my machine, it yield results:
> 
> > x => 0.02
> > x => 0.03
> > x => 0.06
> > x => 0.13
> > x => 0.25
> 
> This is expected. The definition requests a fixed point type with a 
> delta of at least 0.01, most compilers would choose 2**-7 then, which 
> is 0.0078125 in decimal notation. Hence the results you see.

To elaborate a bit, the RM says that the 'Small of the type *must* be a power of two if not specified (3.5.9(8)).  The 'Small has to be no greater than the specified delta, and most compilers would choose the largest power of 2 that is <= the delta.  (This applies only to ordinary fixed-point types, not those with "digits" in the declaration.)  Shark8 showed how you can specify the 'Small, although that isn't completely portable because it's allowable for compilers not to let you specify a 'Small that isn't a power of 2 (3.5.9(21)).  

                              -- Adam



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

* Re: Fixed Point number mul, is it a bug?
  2012-10-18 14:49   ` Adam Beneschan
@ 2012-10-19  9:09     ` AdaMagica
  0 siblings, 0 replies; 11+ messages in thread
From: AdaMagica @ 2012-10-19  9:09 UTC (permalink / raw)


On Thursday, October 18, 2012 4:49:47 PM UTC+2, Adam Beneschan wrote:

> To elaborate a bit, the RM says that the 'Small of the type *must* be a power of two if not specified (3.5.9(8)).  The 'Small has to be no greater than the specified delta, and most compilers would choose the largest power of 2 that is <= the delta.  (This applies only to ordinary fixed-point types, not those with "digits" in the declaration.)  Shark8 showed how you can specify the 'Small, although that isn't completely portable because it's allowable for compilers not to let you specify a 'Small that isn't a power of 2 (3.5.9(21)).  
>                               -- Adam

To elaborate on this: Adam is of course right, but I doubt that any compiler that has decimal fixed points will reject the specification of a small that is a power of ten.



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

* Re: Fixed Point number mul, is it a bug?
  2012-10-18 12:37     ` Dmitry A. Kazakov
@ 2012-10-19 22:58       ` Ian Clifton
  0 siblings, 0 replies; 11+ messages in thread
From: Ian Clifton @ 2012-10-19 22:58 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

> On Thu, 18 Oct 2012 11:42:26 +0100, Ian Clifton wrote:
>
>> Didn’t some IBM machines have a floating‐point format where the exponent
>> was decimal rather than binary?
>
> http://en.wikipedia.org/wiki/Binary-coded_decimal#IBM_and_BCD
>

[...]

Ah, in fact I’d mis‐remembered, the format I was thinking of was

http://en.wikipedia.org/wiki/IBM_Floating_Point_Architecture

which has a base‐16 exponent, not decimal.
-- 
Ian ◎



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

end of thread, other threads:[~2012-10-28  2:23 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-18  7:11 Fixed Point number mul, is it a bug? kylix
2012-10-18  7:35 ` Vinzent Höfler
2012-10-18 14:49   ` Adam Beneschan
2012-10-19  9:09     ` AdaMagica
2012-10-18  7:57 ` Dmitry A. Kazakov
2012-10-18 10:34   ` Georg Bauhaus
2012-10-18 10:42   ` Ian Clifton
2012-10-18 12:37     ` Dmitry A. Kazakov
2012-10-19 22:58       ` Ian Clifton
2012-10-18 14:40     ` Adam Beneschan
2012-10-18 14:04 ` Shark8

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