eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'Fraction':

Home

everywhere
www.exept.de
for:
[back]

Class: Fraction


Inheritance:

   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

Description:


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


Related information:

    Number
    FixedPoint
    Float
    ShortFloat
    LongFloat
    Integer
    Complex

Class protocol:

class initialization
o  initialize

constants
o  pi
return an approximation of the constant pi as Fraction.
The approx. returned here has an error smaller than representable by float instances

o  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.

o  unity
return the neutral element for multiplication (1 / 1)

o  zero
return the neutral element for addition (0 / 1)

instance creation
o  new
create and return a new fraction with value 0

o  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.

o  readFrom: aStringOrStream onError: exceptionBlock

queries
o  isBuiltInClass
return true if this class is known by the run-time-system.
Here, true is returned for myself, false for subclasses.


Instance protocol:

accessing
o  denominator
return the denominator

o  numerator
return the numerator

arithmetic
o  * aNumber
return the product of the receiver and the argument.

o  + aNumber
return the sum of the receiver and the argument, aNumber

o  - aNumber
return the difference of the receiver and the argument, aNumber

o  / aNumber
return the quotient of the receiver and the argument, aNumber

o  // aNumber
return the integer quotient of dividing the receiver by aNumber with
truncation towards negative infinity.

o  negated
optional - could use inherited method ...

o  reciprocal
optional - could use inherited method ...

coercing & converting
o  asFixedPoint
return the receiver as fixedPoint number.
Q: what should the scale be here ?

o  asFixedPoint: scale
return the receiver as fixedPoint number, with the given number
of post-decimal-point digits.

o  asFloat
return a float with (approximately) my value.
Since floats have a limited precision, you usually loose bits when doing this.

o  asFraction
return the receiver as fraction - thats itself

o  asInteger
return an integer with my value - will usually truncate

o  asLargeFloat
return a large float with (approximately) my value

o  asLargeInteger
return an integer with my value - will usually truncate

o  asLongFloat
return a long float with (approximately) my value

o  asShortFloat
return a short float with (approximately) my value

o  coerce: aNumber
convert the argument aNumber into an instance of the receivers class and return it.

o  generality
return the generality value - see ArithmeticValue>>retry:coercing:

comparing
o  < aNumber
return true if the receiver is less
than aNumber, false otherwise.

o  = aNumber
return true, if the argument represents the same numeric value
as the receiver, false otherwise

o  > aNumber
return true if the receiver is greater
than aNumber, false otherwise.

o  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.

o  sameFractionValueAs: aNumber
return true, if the argument represents the same numeric value
as the receiver, false otherwise

double dispatching
o  differenceFromFixedPoint: aFixedPoint

o  differenceFromFloat: aFloat
sent when a float does not know how to subtract the receiver, a fraction

o  differenceFromFraction: aFraction

o  differenceFromInteger: anInteger
sent when an integer does not know how to subtract the receiver, a fraction

o  equalFromFraction: aFraction

o  equalFromInteger: anInteger
sent when an integer does not know how to compare to the receiver, a fraction

o  lessEqFromInteger: anInteger
sent when an integer does not know how to compare to the receiver, a fraction

o  lessFromFraction: aFraction
sent when a fraction does not know how to compare to the receiver

o  lessFromInteger: anInteger
sent when an integer does not know how to compare to the receiver, a fraction

o  productFromFixedPoint: aFixedPoint

o  productFromFloat: aFloat
sent when a float does not know how to multiply the receiver, a fraction

o  productFromFraction: aFraction

o  productFromInteger: anInteger
sent when an integer does not know how to multiply the receiver, a fraction

o  quotientFromFixedPoint: aFixedPoint
Return the quotient of the argument, aFixedPoint and the receiver.
Sent when aFixedPoint does not know how to divide by the receiver.

o  quotientFromFloat: aFloat
Return the quotient of the argument, aFloat and the receiver.
Sent when aFloat does not know how to divide by the receiver.

o  quotientFromFraction: aFraction
Return the quotient of the argument, aFraction and the receiver.
Sent when aFraction does not know how to divide by the receiver.

o  quotientFromInteger: anInteger
Return the quotient of the argument, anInteger and the receiver.
Sent when anInteger does not know how to divide by the receiver.

o  sumFromFixedPoint: aFixedPoint

o  sumFromFloat: aFloat
sent when a float does not know how to add the receiver, a fraction

o  sumFromFraction: aFraction

o  sumFromInteger: anInteger
sent when an integer does not know how to add the receiver, a fraction

printing & storing
o  printOn: aStream
append a printed representation of the receiver to the
argument, aStream

private
o  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).

o  setNumerator: num denominator: den
set both numerator and denominator

testing
o  isFraction
return true, if the receiver is some kind of fraction;
true is returned here - the method is redefined from Object.

o  isLiteral
return true, if the receiver can be used as a literal constant in ST syntax
(i.e. can be used in constant arrays)

o  negative
return true if the receiver is negative

truncation & rounding
o  fractionPart
extract the after-decimal fraction part,
such that (self truncated + self fractionPart) = self

o  integerPart
extract the pre-decimal integer part.

o  rounded
return the receiver rounded to the nearest integer as integer

o  truncated
return the receiver truncated towards zero as Integer

visiting
o  acceptVisitor: aVisitor with: aParameter



ST/X 6.1.1; WebServer 1.620 at exept:8081; Wed, 23 May 2012 09:15:33 GMT