2

Reading this http://en.wikibooks.org/wiki/Ada_Programming/Types/delta has got me wondering what the limit value of delta is.

For example

delta 127 range 0..1_000_000;

needs one byte to hold the delta value.

But

delta 0.0000000001 range 0..1;

would need more bytes, right?

So is there a limit to delta? Surely we can't go on indefinitely to smaller increments?

In the above link it says

If the compiler accepts your fixed point type definition, it guarantees that values represented by that type will have at least the degree of accuracy specified (or better). If the compiler cannot support the type definition (e.g. due to limited hardware) then a compile-time error will result.

1 Answers1

3

The compiled code doesn't hold the Delta value anywhere; stored values of the type are scaled, so the required size corresponds to the range divided by the Small (remembering that if Delta isn't a power of two you will need to specify Small to be the same as Delta).

For GNAT, it turns out that there is a minimum supported value of Delta, and that there's a maximum Size for objects of the type: on Mac OS X (Intel),

Small_Delta : constant := 2.0 ** (-127);
Small_Bound : constant := 2.0 ** (-64);
type T1 is delta Small_Delta range -Small_Bound .. Small_Bound;

fails with

"T1'Small" too small, minimum allowed is 2.0**(-80)

and

Larger_Delta : constant := 2.0 ** (-64);
Larger_Bound : constant := 2.0;
type T2 is delta Larger_Delta range -Larger_Bound .. Larger_Bound;

fails with

size required (67) for type "T2" too large, maximum allowed is 64

(I'm not sure why 67, not 65!)

Simon Wright
  • 25,108
  • 2
  • 35
  • 62
  • So does this mean that with GNAT the smallest possible increment is 2**(-80)? –  Mar 12 '13 at 07:48
  • 1
    Yes (if `Delta` is a power of 2, `Small` is equal to it; if not, it's the next power of 2 lower). See [the LRM](http://www.ada-auth.org/standards/12rm/html/RM-3-5-9.html#p8). – Simon Wright Mar 12 '13 at 09:02
  • 1
    Why 67? 64 bits would cover the range 0 .. 1, 65 bits the range 0 to 2, 66 bits the range -2 to 2 ... now zoom in at the upper end, and you see it stops at 2.0 - delta, the same way integer'last = 2^31 - 1... The 67th bit is necessary to give you that last delta! –  Mar 12 '13 at 16:36
  • @Brian, d'oh. In a previous version I had set (the equivalent of) Larger_Delta to 2.0 ** (-63). If I also set Larger_Bound to 1.0, GNAT is clever enough to work out that I really mean the upper bound to be Larger_Bound - Larger_Delta. I think that 3.5.9(13..15) give permission for this. – Simon Wright Mar 12 '13 at 21:17
  • Actually 3.5.9(22..24). I guess that if this approach doesn't succeed, GNAT reports the full bit requirement. – Simon Wright Mar 12 '13 at 21:26