Ada Programming/Attributes/'Remainder
Description
editX'Remainder(Y,Z) is an Ada attribute where X is any floating-point type and Y,Z are any instances of that type. This attribute represents the fractional part of this result. Z should never be zero.
The sign of Y may or may not be the same as the sign of the result.
Example
edittype Real is digits 15; Real'Remainder (7.5, 2.3); -- returns 0.6 Real'Remainder (42.97482350828000, 6.283185307179586); -- returns -1.00747364197711
-------------------------------------------------------------------------------- -- Mod function for real numbers -- -- x = The number whose remainder is desired -- y = The divisor -- -- We return x mod y with the same sign as y. -------------------------------------------------------------------------------- function "mod" (x, y : Real) return Real is result : Real := Real'Remainder (x, y); begin if y > 0.0 then if result < 0.0 then result := result + y; end if; else if result > 0.0 then result := result + y; end if; end if; return result; end "mod";
-------------------------------------------------------------------------------------------------- -- Rem function for real numbers -- -- x = The number whose remainder is desired -- y = The divisor -- -- We return x mod y with the same sign as x. -------------------------------------------------------------------------------------------------- function "rem" (x, y : Real) return Real is result : Real := Real'Remainder (x, y); begin if x > 0.0 then if result < 0.0 then result := result + abs y; end if; else if result > 0.0 then result := result - abs y; end if; end if; return result; end "rem";
See also
editWikibook
edit- Ada Programming
- Ada Programming/Attributes
- Ada Programming/Attributes/'Rounding
- Ada Programming/Attributes/'Truncation
- Ada Programming/Attributes/'Unbiased_Rounding