fraction Specification Sheet
Computer Algebra Kit (c) 1993,00 by Comp.Alg.Objects. All Rights Reserved.
FractionInherits from: CAObject
Maturity Index: Relatively immature
Class DescriptionFraction objects consist of a numerator object divided by a denominator object. The numerators and denominators can be arbitrary Computer Algebra Kit objects, but the current implementation has been tested only in the case of total fractions i.e., numerators and denominators are taken as elements of the same set. For example, a fraction of two integers is a rational number; a fraction of two polynomials is a rational function.
Canonical FormThe arithmetical operations on fractions assume that the fractions are in a simplified (canonical) form, and produce, as result, a fraction in canonical form. In the case of the integers, or any ordered, integral domain (see inIntegralDomain and inOrderedSet), the numerator and denominator are kept coprime (common gcd is divided out) and the denominator is kept positive. For example, the fraction minus one, is represented as (-1)/1, and never as 1/(-1). In the case of an unordered, integral domain, such as polynomials over the integers or over a field, the denominator and numerator are also coprime, and the leading unit of the denominator is equal to one (over fields, this means that the denominator is kept monic, see leadingUnit). For example, the fraction minus x is represented over Z mod 3 as (2 x)/1 and not as x/2. Finally, for rational functions with floating-point coefficients, no gcd is computed, but the denominator of the fraction is kept monic.
Multiplication by Int Value
+totalFraction:aNumeratorReturns a new fraction with numerator set to aNumerator and denominator set to the element one for aNumerator.
+numerator:aNumeratordenominator:aDenominatorReturns a new fraction with numerator set to aNumerator and denominator set to aDenominator.
-copyReturns a new copy of the fraction.
-deepCopyReturns a new copy of the fraction; sends deepCopy messages to denominator and numerator.
-numeratorReturns a for a fraction a/b.
-denominatorReturns b for a fraction a/b.
- (unsigned)hashReturns a small integer that is the same for fractions that are equal in the sense of isEqual:.
- (BOOL)isEqual:bTwo fractions are equal if they are pointer equal, or if their numerators and denominators are equal. If the numerators and denominators are coprime, but not ordered, the method returns YES if numerators and denominators are opposite to each other. If the fraction is not in canonical form, it is verified that the difference of the two fractions is zero.
- (int)compare:bCompares the fractions by comparing the products of numerators and denominators.
-asNumericalReturns the numerical value of the fraction. Attempts to divide the numerical value of the numerator and denominator.
-asModp:(unsigned short)pReturns the value of the fraction modulo p. Attempts to divide the values of the numerator and denominator. If the division fails, the method generates an error message.
-asIntegralReturns, if the denominator of the fraction is equal to one, a new reference to the numerator. Otherwise, the method generates an error message.
-zeroReturns a new fraction with numerator equal to zero.
- (BOOL)isZeroWhether the numerator of the fraction is equal to zero.
- (BOOL)isOpposite:bTwo fractions are opposite if their numerators are opposite and the denominators are equal. However, if the numerators and denominators are coprime, but not ordered, the method returns YES if the numerators are equal and the denominators are opposite to each other. If the fraction is not in canonical form, it is verified that the sum of the two fractions is zero.
-negateReturns a new fraction, the opposite of self by negating the numerator.
-doubleReturns a new fraction, the double of self by doubling the numerator.
-add:bReturns a new fraction, the sum of self and b. The method first handles some special cases and then distinguishes between the case that both fractions have a common denominator or not.
-subtract:bReturns a new fraction, the difference of self and b. The method first handles some special cases and then distinguishes between the case that both fractions have a common denominator or not.
-oneReturns a new fraction with numerator equal to one.
- (BOOL)isOneWhether the numerator is equal to the denominator.
- (BOOL)isMinusOneWhether the numerator is the opposite of the denominator.
-squareReturns a new fraction; squares numerator and denominator.
-inverseReturns the inverse of the fraction; interchanges, in the case of a total fraction, denominator and numerator.
-multiply:bReturns a new fraction; multiplies the numerators and the denominators together.
-divide:bReturns a new fraction by multiplying the fraction by the inverse of b.
-power:(int)nReturns a new fraction; raises numerator and denominator to the n-th power.
-intValue:(int)intValueCreates a fraction with numerator equal to intValue and denominator one.
- (int)intValueReturns intValue of numerator divided by intValue of denominator.
-multiplyIntValue:(int)intValueReturns a new fraction by multiplying the numerator by intValue.
-divideIntValue:(int)intValueReturns a new fraction by multiplying the denominator by intValue.
- (BOOL)printsLeadingSignWhether the fraction prints a leading minus sign.
- (BOOL)printsSumWhether the fraction prints as a sum.
- (BOOL)printsProductWhether the fraction prints as a product.
-printOn:(IOD)aFilePrints the fraction to aFile by sending printOn: messages to numerator and denominator.