Ada Programming/Attributes/'Unbiased Rounding

From Wikibooks, open books for an open world
Jump to: navigation, search

Ada Lovelace 1838.jpg

Description[edit]

X'Unbiased_Rounding(Y) is an Ada attribute where X is any floating-point type and Y is any instance of that type. This attribute represents the closest integer value to Y. If Y is exactly between two integer values (e.g. 1.5), then the result is the even value (e.g. 2.0).

Example[edit]

X : Float := 1.5;
Y : Float := 1.0;
Z : Float := 1.999;
... 
pragma Assert(Float'Unbiased_Rounding(X) = 2.0);  -- OK
pragma Assert(Float'Unbiased_Rounding(Y) = 1.0);  -- OK
pragma Assert(Float'Unbiased_Rounding(Z) = 2.0);  -- OK
pragma Assert(Float'Unbiased_Rounding(X) = 1.0);  -- Wrong
...
X := 2.5;
pragma Assert(Float'Unbiased_Rounding(X) = 2.0 );  -- OK
pragma Assert(Float'Unbiased_Rounding(X) = 3.0 );  -- Wrong

See also[edit]

Wikibook[edit]

Ada Reference Manual[edit]