Jump to content

Ada Programming/Attributes/'Remainder

From Wikibooks, open books for an open world

Description

[edit | edit source]

X'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

[edit | edit source]
type 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

[edit | edit source]

Wikibook

[edit | edit source]

Ada Reference Manual

[edit | edit source]