Reputation: 31193
I want this type to only contain values x for which is (x > 0) && (x <= 10.0)
is true
I declared my type larger_than_zero
this way:
type a_float is digits 6 range - 1.0E38 .. 1.0E38;
type larger_than_zero is new a_float range a_float'Adjacent(0.0, 1.0) .. 10.0;
It is now impossible to assign 0.0
to a variable of type larger_than_zero
.
'Adjacent(A, B)
takes the next possible machine value starting from A
that is not A
, and lies in the direction to B
My question is whether this can cause some weird behaviour that would normally not be expected of a type.
Maybe larger_than_zero'First
isn't even part of the model of larger_than_zero
?
Any pointers to detailed information on this topic would be appeciated.
Upvotes: 1
Views: 235
Reputation: 31699
It shouldn't cause any problems. Technically, the range
declaration on the type
doesn't have any effect on the type at all; it only impacts the first subtype. Your type larger_than_zero
declaration defines both a type and a first subtype. The type includes every floating-point number representable in the floating-point format, including negative numbers, zero, and numbers greater than 10.0. The first subtype is a subrange of that type that includes only the numbers you specify in the range. Calculations are performed using the type, and intermediate results may go outside the range. The subtype is relevant only for constraint checking; when assigning a larger_than_zero
variable, or passing a larger_than_zero
parameter, the value will be checked to make sure it's in range, and Constraint_Error
will be raised if not. Since that's really the only use of the range, it shouldn't cause any weird behavior.
One thing to note is that a_float'Adjacent(0.0, 1.0)
may not return the same thing in different implementations, even if IEEE floats are used in both cases, since some implementations may not support denormalized numbers. It should be the same, as long as a_float'Denorm
returns the same value (see A.5.3(10)). Of course, if one of the implementations is on a machine that doesn't use IEEE floats, such as VAXes, the value of 'Adajcent
could be different. And, of course, z_float'Adjacent(0.0, 1.0)
, where z_float
is an IEEE 64-bit float, will be different from the a_float
version assuming a_float
is a 32-bit float.
Upvotes: 3