class interface FRACTION_WITH_BIG_INTEGER_NUMBER
--
-- To implement NUMBER (do not use this class, see NUMBER).
--
creation
make (n, d: INTEGER_GENERAL_NUMBER; s: BOOLEAN)
-- Create a simplified large_fraction
require
n.is_positive;
d.is_positive;
not ((n \\ d) @= 0)
make_simply (n, d: INTEGER_GENERAL_NUMBER; s: BOOLEAN)
-- create a large_fraction without simplify it
require
n.is_positive;
d.is_positive;
not ((n \\ d) @= 0)
feature(s) from HASHABLE
hash_code: INTEGER
-- The hash-code value of Current.
ensure
good_hash_value: Result >= 0
feature(s) from COMPARABLE
is_equal (other: NUMBER): BOOLEAN
require
other /= Void
ensure
trichotomy: Result = (not (Current < other) and not (other < Current));
generating_type = other.generating_type implies Result = other.is_equal(Current)
infix "<" (other: NUMBER): BOOLEAN
-- Is Current strictly less than other?
require
other_exists: other /= Void
ensure
asymmetric: Result implies not (other < Current)
infix "<=" (other: NUMBER): BOOLEAN
-- Is Current less or equal than other?
require
other_exists: other /= Void
ensure
definition: Result = (Current < other or is_equal(other))
infix ">" (other: NUMBER): BOOLEAN
-- Is Current strictly greater than other?
require
other_exists: other /= Void
ensure
definition: Result = (other < Current)
infix ">=" (other: NUMBER): BOOLEAN
-- Is Current greater or equal than other?
require
other_exists: other /= Void
ensure
definition: Result = (other <= Current)
in_range (lower, upper: NUMBER): BOOLEAN
-- Return True if Current is in range [lower..upper]
ensure
Result = (Current >= lower and Current <= upper)
compare (other: NUMBER): INTEGER
-- Compare Current with other.
-- < <==> Result < 0
-- > <==> Result > 0
-- Otherwise Result = 0.
require
other_exists: other /= Void
ensure
equal_zero: Result = 0 = is_equal(other);
smaller_negative: Result = -1 = (Current < other);
greater_positive: Result = 1 = (Current > other)
three_way_comparison (other: NUMBER): INTEGER
-- Compare Current with other.
-- < <==> Result < 0
-- > <==> Result > 0
-- Otherwise Result = 0.
require
other_exists: other /= Void
ensure
equal_zero: Result = 0 = is_equal(other);
smaller_negative: Result = -1 = (Current < other);
greater_positive: Result = 1 = (Current > other)
min (other: NUMBER): NUMBER
-- Minimum of Current and other.
require
other /= Void
ensure
Result <= Current and then Result <= other;
compare(Result) = 0 or else other.compare(Result) = 0
max (other: NUMBER): NUMBER
-- Maximum of Current and other.
require
other /= Void
ensure
Result >= Current and then Result >= other;
compare(Result) = 0 or else other.compare(Result) = 0
feature(s) from NUMBER
-- Binary operators for two NUMBERs:
infix "+" (other: NUMBER): NUMBER
-- Sum of Current and other.
require
other /= Void
ensure
(Result - other).is_equal(Current)
infix "-" (other: NUMBER): NUMBER
-- Difference of Current and other.
require
other /= Void
ensure
(Result + other).is_equal(Current)
infix "*" (other: NUMBER): NUMBER
-- Product of Current and other.
require
other /= Void
ensure
Result /= Void
infix "/" (other: NUMBER): NUMBER
-- Quotient of Current and other.
require
other /= Void;
divisible(other)
ensure
Result /= Void
infix "//" (other: NUMBER): NUMBER
-- Divide Current by other (Integer division).
require
is_integer_general_number;
other.is_integer_general_number;
divisible(other)
ensure
Result.is_integer_general_number;
Current.is_equal(Result * other + Current \\ other)
infix "\\" (other: NUMBER): NUMBER
-- Remainder of division of Current by other.
require
is_integer_general_number;
other.is_integer_general_number;
divisible(other)
ensure
Result.is_integer_general_number;
Result.is_positive and Result < other.abs
infix "^" (exp: NUMBER): NUMBER
-- Current raised to exp-th power.
require
exp.is_integer_general_number;
is_zero implies exp @> 0
ensure
Result /= Void;
exp.is_zero implies Result.is_one
gcd (other: NUMBER): INTEGER_GENERAL_NUMBER
-- Great Common Divisor of Current and other.
require
other.is_integer_general_number;
is_integer_general_number
ensure
Result.is_positive;
Result.is_zero implies Current.is_zero and other.is_zero;
not Result.is_zero implies (Current / Result).gcd(other / Result).is_one
feature(s) from NUMBER
-- Unary operators for two NUMBERs:
prefix "+": NUMBER
-- Unary plus of Current.
ensure
Result = Current
prefix "-": NUMBER
-- Opposite of Current.
ensure
Result /= Void
feature(s) from NUMBER
-- To know more about a NUMBER:
is_integer_8: BOOLEAN
-- Does Current value fit on an INTEGER_8?
ensure
Result implies is_integer_general_number
is_integer_16: BOOLEAN
-- Does Current value fit on an INTEGER_16?
ensure
Result implies is_integer_general_number
is_integer: BOOLEAN
-- Does Current value fit on an INTEGER?
ensure
Result implies is_integer_general_number
is_integer_32: BOOLEAN
-- Does Current value fit on an INTEGER?
ensure
Result implies is_integer_general_number
is_integer_64: BOOLEAN
-- Does Current value fit on an INTEGER_64?
ensure
Result implies is_integer_general_number
is_zero: BOOLEAN
-- Is it 0 ?
ensure
Result = Current @= 0
is_one: BOOLEAN
-- Is it 1 ?
ensure
Result = Current @= 1
is_positive: BOOLEAN
-- Is Current greater or equal zero ?
ensure
Result = Current @>= 0
is_negative: BOOLEAN
-- Is Current < 0 ?
ensure
Result = Current @< 0
is_odd: BOOLEAN
-- Is odd ?
require
is_integer_general_number
odd: BOOLEAN
-- Is odd ?
require
is_integer_general_number
is_even: BOOLEAN
-- Is even ?
require
is_integer_general_number
even: BOOLEAN
-- Is even ?
require
is_integer_general_number
is_integer_general_number: BOOLEAN
is_fraction_general_number: BOOLEAN
is_double: BOOLEAN
feature(s) from NUMBER
-- Conversions and printing:
to_integer_8: INTEGER_8
-- Conversion of Current in an INTEGER_8.
require
is_integer_8
to_integer_16: INTEGER_16
-- Conversion of Current in an INTEGER_16.
require
is_integer_16
to_integer: INTEGER
-- Conversion of Current in an INTEGER.
require
is_integer
to_integer_32: INTEGER
-- Conversion of Current in an INTEGER.
require
is_integer
to_integer_64: INTEGER_64
-- Conversion of Current in an INTEGER.
require
is_integer_64
to_double: DOUBLE
-- *** This is not very good, bad precision on big numbers
-- (Vincent Croizier, 05/07/04) ***
require
is_double
to_string: STRING
-- Convert the NUMBER into a new allocated STRING.
-- Note: see also append_in to save memory.
append_in (string: STRING)
-- Append the equivalent of to_string at the end of buffer.
-- Thus you can save memory because no other STRING is allocated
-- for the job.
require
string /= Void
to_decimal (digits: INTEGER; all_digits: BOOLEAN): STRING
-- Convert Current into its decimal view. A maximum of decimal
-- digits places will be used for the decimal part. If the
-- all_digits flag is True insignificant digits will be included
-- as well. (See also decimal_in to save memory.)
require
non_negative_digits: digits >= 0
ensure
not Result.is_empty
append_decimal_in (buffer: STRING; digits: INTEGER; all_digits: BOOLEAN)
-- Append the equivalent of to_decimal at the end of buffer. Thus
-- you can save memory because no other STRING is allocated.
require
non_negative_digits: digits >= 0
digit: CHARACTER
-- Gives the corresponding CHARACTER for range 0..9.
require
to_integer.in_range(0,9)
ensure
(once "0123456789").has(Result);
Current @= Result.value
feature(s) from NUMBER
-- To mimic NUMERIC:
divisible (other: NUMBER): BOOLEAN
-- Is other a valid divisor for Current ?
require
other /= Void
one: NUMBER
-- The neutral element of multiplication.
ensure
neutral_element: -- Result is the neutral element of
-- multiplication.
zero: NUMBER
-- The neutral element of addition.
ensure
neutral_element: -- Result is the neutral element of
-- addition.
sign: INTEGER
sqrt: DOUBLE
-- Compute the square routine.
require
is_double
log: DOUBLE
require
is_double
abs: NUMBER
ensure
Result.is_positive
feature(s) from NUMBER
-- To mix NUMBER and INTEGER_64:
infix "@+" (other: INTEGER_64): NUMBER
-- Sum of 'Current' and 'other'.
-- *** Can be very faster !! (Vincent Croizier, 04/07/04) ***
ensure
Result /= Void
infix "@-" (other: INTEGER_64): NUMBER
-- Difference of Current and other.
ensure
Result /= Void
infix "@*" (other: INTEGER_64): NUMBER
-- *** Can be very faster !! (Vincent Croizier, 04/07/04) ***
ensure
Result /= Void
infix "@/" (other: INTEGER_64): NUMBER
-- *** Can be very faster !! (Vincent Croizier, 04/07/04) ***
require
other /= 0
ensure
Result /= Void
infix "@//" (other: INTEGER_64): NUMBER
-- Divide Current by other (Integer division).
require
is_integer_general_number;
other /= 0
ensure
Result.is_integer_general_number
infix "@\\" (other: INTEGER_64): NUMBER
-- Remainder of division of Current by other.
require
is_integer_general_number;
other /= 0
ensure
Result.is_integer_general_number
infix "@^" (exp: INTEGER_64): NUMBER
require
is_zero implies exp > 0
ensure
Result /= Void
infix "@=" (other: INTEGER_64): BOOLEAN
-- Is Current equal other ?
infix "@<" (other: INTEGER_64): BOOLEAN
-- Is Current strictly less than other?
ensure
Result = not (Current @>= other)
infix "@<=" (other: INTEGER_64): BOOLEAN
-- Is Current less or equal other?
ensure
Result = not (Current @> other)
infix "@>" (other: INTEGER_64): BOOLEAN
-- Is Current strictly greater than other?
ensure
Result = not (Current @<= other)
infix "@>=" (other: INTEGER_64): BOOLEAN
-- Is Current greater or equal than other?
ensure
Result = not (Current @< other)
feature(s) from NUMBER
-- To mix NUMBER and DOUBLE:
infix "#=" (other: DOUBLE): BOOLEAN
-- Is Current equal other?
infix "#<" (other: DOUBLE): BOOLEAN
-- Is Current strictly less than other?
ensure
Result implies not (Current #>= other)
infix "#<=" (other: DOUBLE): BOOLEAN
-- Is Current less or equal other?
ensure
Result = not (Current #> other)
infix "#>" (other: DOUBLE): BOOLEAN
-- Is Current strictly greater than other?
ensure
Result = not (Current #<= other)
infix "#>=" (other: DOUBLE): BOOLEAN
-- Is Current greater or equal than other?
ensure
Result = not (Current #< other)
feature(s) from NUMBER
-- Misc:
out_in_tagged_out_memory
-- Append terse printable represention of current object
-- in tagged_out_memory.
ensure
not_cleared: tagged_out_memory.count >= old tagged_out_memory.count;
append_only: (old tagged_out_memory.twin).is_equal(tagged_out_memory.substring(1,old tagged_out_memory.count))
fill_tagged_out_memory
-- Append a viewable information in tagged_out_memory in
-- order to affect the behavior of out, tagged_out, etc.
inverse: NUMBER
require
divisible(Current)
ensure
Result /= Void
factorial: NUMBER
require
is_integer_general_number;
is_positive
ensure
Result.is_integer_general_number;
Result.is_positive
numerator: INTEGER_GENERAL_NUMBER
denominator: INTEGER_GENERAL_NUMBER
feature(s) from NUMBER
-- Implementation:
add_with_integer_64_number (other: INTEGER_64_NUMBER): NUMBER
require
other /= Void
ensure
Result /= Void
add_with_big_integer_number (other: BIG_INTEGER_NUMBER): NUMBER
require
other /= Void
ensure
Result /= Void
add_with_fraction_with_big_integer_number (other: FRACTION_WITH_BIG_INTEGER_NUMBER): NUMBER
require
other /= Void
ensure
Result /= Void
multiply_with_integer_64_number (other: INTEGER_64_NUMBER): NUMBER
require
other /= Void
ensure
Result /= Void
multiply_with_big_integer_number (other: BIG_INTEGER_NUMBER): NUMBER
require
other /= Void
ensure
Result /= Void
multiply_with_fraction_with_big_integer_number (other: FRACTION_WITH_BIG_INTEGER_NUMBER): NUMBER
require
other /= Void
ensure
Result /= Void
greater_with_integer_64_number (other: INTEGER_64_NUMBER): BOOLEAN
require
other /= Void
greater_with_big_integer_number (other: BIG_INTEGER_NUMBER): BOOLEAN
require
other /= Void
greater_with_fraction_with_big_integer_number (other: FRACTION_WITH_BIG_INTEGER_NUMBER): BOOLEAN
require
other /= Void
gcd_with_integer_64_number (other: INTEGER_64_NUMBER): INTEGER_GENERAL_NUMBER
require
other /= Void
gcd_with_big_integer_number (other: BIG_INTEGER_NUMBER): INTEGER_GENERAL_NUMBER
require
other /= Void
feature(s) from NUMBER
max_double: NUMBER
min_double: NUMBER
feature(s) from NUMBER
-- To implement efficient calculus
mutable_register1: MUTABLE_BIG_INTEGER
mutable_register2: MUTABLE_BIG_INTEGER
mutable_register3: MUTABLE_BIG_INTEGER
mutable_register4: MUTABLE_BIG_INTEGER
feature(s) from FRACTION_GENERAL_NUMBER
from_two_integer (n, d: INTEGER_64): NUMBER
require
n /= 0;
d /= 0;
n \\ d /= 0
from_two_integer_general_number (n, d: INTEGER_GENERAL_NUMBER): NUMBER
require
n /= Void;
d /= Void;
not d.is_zero
from_integer_and_integer_general_number (n: INTEGER_64; d: INTEGER_GENERAL_NUMBER): NUMBER
require
n /= 0;
d /= Void;
not d.is_zero;
not ((n.to_number \\ d) @= 0)
from_abstract_integer_and_integer (n: INTEGER_GENERAL_NUMBER; d: INTEGER_64): NUMBER
require
d /= 0;
n /= Void;
not n.is_zero;
not ((n \\ d.to_number) @= 0)
feature(s) from FRACTION_GENERAL_NUMBER
decimal_in (buffer: STRING; num, denom: NUMBER; negative: BOOLEAN; digits: INTEGER; all_digits: BOOLEAN)
feature(s) from FRACTION_WITH_BIG_INTEGER_NUMBER
make (n, d: INTEGER_GENERAL_NUMBER; s: BOOLEAN)
-- Create a simplified large_fraction
require
n.is_positive;
d.is_positive;
not ((n \\ d) @= 0)
make_simply (n, d: INTEGER_GENERAL_NUMBER; s: BOOLEAN)
-- create a large_fraction without simplify it
require
n.is_positive;
d.is_positive;
not ((n \\ d) @= 0)
invariant
denominator @>= 2;
numerator @> 0;
numerator.gcd(denominator) @= 1;
end of FRACTION_WITH_BIG_INTEGER_NUMBER