Contents
Creation Procedures
default_create
create f.default_create
Creates zero (0/1).
Postconditions: is_zero, denominator = 1
make
create f.make (a_numerator, a_denominator: INTEGER_64)
Creates fraction from numerator and denominator. Automatically reduces to lowest terms.
Precondition: a_denominator /= 0
Postconditions: is_reduced, denominator > 0
Example:
create f.make (2, 4) -- Automatically reduces to 1/2
make_integer
create f.make_integer (a_value: INTEGER_64)
Creates fraction from integer (value/1).
Postconditions: numerator = a_value, denominator = 1
make_mixed
create f.make_mixed (a_whole, a_numerator, a_denominator: INTEGER_64)
Creates from mixed number: whole + numerator/denominator.
Preconditions: a_denominator /= 0, a_numerator >= 0, a_denominator > 0
Example:
create f.make_mixed (2, 3, 4) -- 2 3/4 = 11/4
make_from_string
create f.make_from_string (a_str: READABLE_STRING_GENERAL)
Creates from string. Accepts: "3/4", "-1/2", "5", "2 3/4", "0.5"
Precondition: a_str /= Void and then not a_str.is_empty
make_zero
create f.make_zero
Creates zero (0/1).
make_one
create f.make_one
Creates one (1/1).
Access (Attributes)
numerator
numerator: INTEGER_64
The numerator (top number). May be negative.
denominator
denominator: INTEGER_64
The denominator (bottom number). Always positive.
Invariant: denominator > 0
Status Queries
is_zero
is_zero: BOOLEAN
True if numerator = 0.
is_negative
is_negative: BOOLEAN
True if numerator < 0.
is_positive
is_positive: BOOLEAN
True if numerator > 0 (not zero, not negative).
is_integer
is_integer: BOOLEAN
True if denominator = 1 (whole number).
is_proper
is_proper: BOOLEAN
True if |numerator| < denominator. Example: 3/4 is proper.
is_improper
is_improper: BOOLEAN
True if |numerator| >= denominator. Example: 5/4 is improper.
is_reduced
is_reduced: BOOLEAN
True if fraction is in lowest terms. Always true due to automatic reduction.
is_unit_fraction
is_unit_fraction: BOOLEAN
True if numerator = 1. Examples: 1/2, 1/3, 1/4.
Comparison
is_less alias "<"
is_less alias "<" (other: like Current): BOOLEAN
True if current fraction is less than other.
is_equal
is_equal (other: like Current): BOOLEAN
True if fractions are mathematically equal.
Inherited from COMPARABLE: <=, >, >=, min, max
Conversion
to_double
to_double: DOUBLE
Converts to double (may lose precision for fractions like 1/3).
to_integer
to_integer: INTEGER_64
Converts to integer (truncates toward zero).
whole_part
whole_part: INTEGER_64
The whole number portion. Example: 11/4 returns 2.
fractional_part
fractional_part: SIMPLE_FRACTION
The fractional remainder. Example: 11/4 returns 3/4.
Postconditions: Result.is_proper or Result.is_zero, not Result.is_negative
out
out: STRING
String as "numerator/denominator" or just "integer" if denominator = 1.
to_string
to_string: STRING
Same as out.
to_mixed_string
to_mixed_string: STRING
String as mixed number "whole num/den". Examples: 11/4 returns "2 3/4", 3/4 returns "3/4".
to_decimal_string
to_decimal_string (a_places: INTEGER): STRING
Decimal representation to specified places.
Precondition: a_places >= 0
Arithmetic Operations
All operations are immutable - they return new SIMPLE_FRACTION objects.
add alias "+"
add alias "+" (other: like Current): SIMPLE_FRACTION
Sum of current and other.
subtract alias "-"
subtract alias "-" (other: like Current): SIMPLE_FRACTION
Difference of current and other.
multiply alias "*"
multiply alias "*" (other: like Current): SIMPLE_FRACTION
Product of current and other.
divide alias "/"
divide alias "/" (other: like Current): SIMPLE_FRACTION
Quotient of current divided by other.
Precondition: not other.is_zero
negate
negate: SIMPLE_FRACTION
Negated value (flips sign).
absolute
absolute: SIMPLE_FRACTION
Absolute value.
Postcondition: not Result.is_negative
reciprocal
reciprocal: SIMPLE_FRACTION
Reciprocal (1/this = denominator/numerator).
Precondition: not is_zero
Postcondition: (Current * Result).is_equal (one)
power
power (n: INTEGER): SIMPLE_FRACTION
Current raised to integer power n. Negative powers use reciprocal.
Example: (2/3).power(2) = 4/9, (2/3).power(-1) = 3/2
Scaling
scale
scale (a_factor: INTEGER_64): SIMPLE_FRACTION
Multiply by integer factor.
Example: (1/4).scale(3) = 3/4
scale_down
scale_down (a_factor: INTEGER_64): SIMPLE_FRACTION
Divide by integer factor.
Precondition: a_factor /= 0
Example: (3/4).scale_down(2) = 3/8
Factory Helpers
zero
zero: SIMPLE_FRACTION
Returns new zero fraction.
one
one: SIMPLE_FRACTION
Returns new fraction equal to 1.
half
half: SIMPLE_FRACTION
Returns 1/2.
third
third: SIMPLE_FRACTION
Returns 1/3.
quarter
quarter: SIMPLE_FRACTION
Returns 1/4.
Common Fractions
halves
halves (n: INTEGER_64): SIMPLE_FRACTION
Returns n/2.
thirds
thirds (n: INTEGER_64): SIMPLE_FRACTION
Returns n/3.
quarters
quarters (n: INTEGER_64): SIMPLE_FRACTION
Returns n/4.
eighths
eighths (n: INTEGER_64): SIMPLE_FRACTION
Returns n/8.
sixteenths
sixteenths (n: INTEGER_64): SIMPLE_FRACTION
Returns n/16.
Class Invariants
invariant
denominator_positive: denominator > 0
reduced_form: is_reduced