with ada.text_io; use ada.text_io;
package body bignumPkg.even is
-- Element size - 1 is the least significant digit of a bignum
function is_even (b : BigNum) return Boolean is
(b (size - 1) mod 2 = 0);
function make_even (b, toward : BigNum) return Even_BigNum is
ans : BigNum;
overflow : Boolean;
begin
if toward < b then
-- Adding Last and ignoring overflow works as subtraction!
-- The value of b can't be zero and so overflow will always be true,
-- and but we ignore it and simply use the result, which will be b-1.
plus_ov (b, Last, ans, overflow); -- BigNumPkg.plus_ov is called
-- This can overflow only if b = Last,
-- but if b = Last then b < toward is false
-- and an exception is raised in the else.
elsif b < toward then
ans := b + One; -- BigNumPkg."+" is called
else -- b = toward not allowed since we wouldn't know which direction to go
raise constraint_error with "Toward must be less or greater, not equal";
end if;
-- The result returned must be an Even_BigNum.
-- We can safely Convert ans to an Even_BigNum because Odd +/- 1 gives even.
return Even_BigNum(ans);
end make_even;
end BigNumPkg.even;