|
|
Class: Fraction
Object
|
+--Magnitude
|
+--ArithmeticValue
|
+--Number
|
+--Fraction
|
+--FixedPoint
- Package:
- stx:libbasic
- Category:
- Magnitude-Numbers
- Version:
- rev:
1.80
date: 2009/09/08 11:57:25
- user: cg
- file: Fraction.st directory: libbasic
- module: stx stc-classLibrary: libbasic
- Author:
- Claus Gittinger
Instances of Fraction represent fractional numbers consisting of
a numerator and denominator. Both are themselfes arbitrary precision
integers.
Fractions are usually created by dividing Integers using / (for exact division).
Notice, that all operations on fractions reduce their result; this means, that
the result of a fraction-operation may return an integer.
Aka:
(1 / 7) * 7 -> 1 (not 0.99999999...)
Mixed mode arithmetic:
fraction op fraction -> fraction/integer
fraction op fix -> fix; scale is fix's scale
fraction op integer -> fraction/integer
fraction op float -> float
[classVariables:]
PrintWholeNumbers Booolean experimental:
controls how fractions which are greater than 1 are printed.
if true, print them as a sum of an integral and the fractional part.
(Large ones are easier to read this way)
(17/3) printString -> '(5+(2/3))'
for now, the default is false, for backward compatibility
Number
FixedPoint
Float
ShortFloat
LongFloat
Integer
Complex
class initialization
-
initialize
-
constants
-
pi
-
return an approximation of the constant pi as Fraction.
The approx. returned here has an error smaller than representable by float instances
-
pi_approximation
-
return an approximation of the constant pi as Fraction.
The approx. returned is good for 6 valid digits and has an error of less than -2.67-07.
The value might be useful to avoid floating point numbers in graphic rendering code,
where 6 digits of precision are usually good enough.
-
unity
-
return the neutral element for multiplication (1 / 1)
-
zero
-
return the neutral element for addition (0 / 1)
instance creation
-
new
-
create and return a new fraction with value 0
-
numerator: num denominator: den
-
create and return a new fraction with numerator num and denominator den.
Notice: stc inlines this message if sent to the global named Fraction.
-
readFrom: aStringOrStream onError: exceptionBlock
-
queries
-
isBuiltInClass
-
return true if this class is known by the run-time-system.
Here, true is returned for myself, false for subclasses.
accessing
-
denominator
-
return the denominator
-
numerator
-
return the numerator
arithmetic
-
* aNumber
-
return the product of the receiver and the argument.
-
+ aNumber
-
return the sum of the receiver and the argument, aNumber
-
- aNumber
-
return the difference of the receiver and the argument, aNumber
-
/ aNumber
-
return the quotient of the receiver and the argument, aNumber
-
// aNumber
-
return the integer quotient of dividing the receiver by aNumber with
truncation towards negative infinity.
-
negated
-
optional - could use inherited method ...
-
reciprocal
-
optional - could use inherited method ...
coercing & converting
-
asFixedPoint
-
return the receiver as fixedPoint number.
Q: what should the scale be here ?
-
asFixedPoint: scale
-
return the receiver as fixedPoint number, with the given number
of post-decimal-point digits.
-
asFloat
-
return a float with (approximately) my value.
Since floats have a limited precision, you usually loose bits when doing this.
-
asFraction
-
return the receiver as fraction - thats itself
-
asInteger
-
return an integer with my value - will usually truncate
-
asLargeFloat
-
return a large float with (approximately) my value
-
asLargeInteger
-
return an integer with my value - will usually truncate
-
asLongFloat
-
return a long float with (approximately) my value
-
asShortFloat
-
return a short float with (approximately) my value
-
coerce: aNumber
-
convert the argument aNumber into an instance of the receivers class and return it.
-
generality
-
return the generality value - see ArithmeticValue>>retry:coercing:
comparing
-
< aNumber
-
return true if the receiver is less
than aNumber, false otherwise.
-
= aNumber
-
return true, if the argument represents the same numeric value
as the receiver, false otherwise
-
> aNumber
-
return true if the receiver is greater
than aNumber, false otherwise.
-
hash
-
return a number for hashing; redefined, since fractions compare
by numeric value (i.e. (9/3) = 3), therefore (9/3) hash must be the same
as 3 hash.
-
sameFractionValueAs: aNumber
-
return true, if the argument represents the same numeric value
as the receiver, false otherwise
double dispatching
-
differenceFromFixedPoint: aFixedPoint
-
-
differenceFromFloat: aFloat
-
sent when a float does not know how to subtract the receiver, a fraction
-
differenceFromFraction: aFraction
-
-
differenceFromInteger: anInteger
-
sent when an integer does not know how to subtract the receiver, a fraction
-
equalFromFraction: aFraction
-
-
equalFromInteger: anInteger
-
sent when an integer does not know how to compare to the receiver, a fraction
-
lessEqFromInteger: anInteger
-
sent when an integer does not know how to compare to the receiver, a fraction
-
lessFromFraction: aFraction
-
sent when a fraction does not know how to compare to the receiver
-
lessFromInteger: anInteger
-
sent when an integer does not know how to compare to the receiver, a fraction
-
productFromFixedPoint: aFixedPoint
-
-
productFromFloat: aFloat
-
sent when a float does not know how to multiply the receiver, a fraction
-
productFromFraction: aFraction
-
-
productFromInteger: anInteger
-
sent when an integer does not know how to multiply the receiver, a fraction
-
quotientFromFixedPoint: aFixedPoint
-
Return the quotient of the argument, aFixedPoint and the receiver.
Sent when aFixedPoint does not know how to divide by the receiver.
-
quotientFromFloat: aFloat
-
Return the quotient of the argument, aFloat and the receiver.
Sent when aFloat does not know how to divide by the receiver.
-
quotientFromFraction: aFraction
-
Return the quotient of the argument, aFraction and the receiver.
Sent when aFraction does not know how to divide by the receiver.
-
quotientFromInteger: anInteger
-
Return the quotient of the argument, anInteger and the receiver.
Sent when anInteger does not know how to divide by the receiver.
-
sumFromFixedPoint: aFixedPoint
-
-
sumFromFloat: aFloat
-
sent when a float does not know how to add the receiver, a fraction
-
sumFromFraction: aFraction
-
-
sumFromInteger: anInteger
-
sent when an integer does not know how to add the receiver, a fraction
printing & storing
-
printOn: aStream
-
append a printed representation of the receiver to the
argument, aStream
private
-
reduced
-
reduce the receiver; divide the numerator and denominator by their
greatest common divisor; if the result is integral, return an Integer.
Otherwise, return the normalized receiver.
CAVEAT: bad name; should be called reduce, as it has a side effect
(i.e. this is destructive wrt. the instance values).
-
setNumerator: num denominator: den
-
set both numerator and denominator
testing
-
isFraction
-
return true, if the receiver is some kind of fraction;
true is returned here - the method is redefined from Object.
-
isLiteral
-
return true, if the receiver can be used as a literal constant in ST syntax
(i.e. can be used in constant arrays)
-
negative
-
return true if the receiver is negative
truncation & rounding
-
fractionPart
-
extract the after-decimal fraction part,
such that (self truncated + self fractionPart) = self
-
integerPart
-
extract the pre-decimal integer part.
-
rounded
-
return the receiver rounded to the nearest integer as integer
-
truncated
-
return the receiver truncated towards zero as Integer
visiting
-
acceptVisitor: aVisitor with: aParameter
-
|