eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'Fraction':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: Fraction


Inheritance:

   Object
   |
   +--Magnitude
      |
      +--ArithmeticValue
         |
         +--Number
            |
            +--Fraction
               |
               +--FixedPoint

Package:
stx:libbasic
Category:
Magnitude-Numbers
Version:
rev: 1.150 date: 2023/12/11 12:05:16
user: stefan
file: Fraction.st directory: libbasic
module: stx stc-classLibrary: libbasic

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       Boolean        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

copyright

COPYRIGHT (c) 1989 by Claus Gittinger All Rights Reserved This software is furnished under a license and may be used only in accordance with the terms of that license and with the inclusion of the above copyright notice. This software may not be provided or otherwise made available to, or used by, any other person. No title to or ownership of the software is hereby transferred.

Class protocol:

class initialization
o  initialize
(comment from inherited method)
setup the signals

coercing & converting
o  coerce: aNumber
convert the argument aNumber into an instance of the receiver (class) and return it.

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

constants
o  e
return an approximation of the constant e as Fraction.
The approx. returned here has an error smaller than representable by float instances
(roughly 26 valid digits)

Usage example(s):

E := nil
    
     Fraction e
     Fraction e asFloat - Float e
     Fraction e asLongFloat - LongFloat e
     Float e
     FixedPoint e

Usage example(s):

Modified (comment): / 06-06-2019 / 17:10:56 / Claus Gittinger

o  e_approximation
return an approximation of e 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.

Usage example(s):

     Fraction e
     Fraction e asFloat               
     Fraction e_approximation asFloat 
     
     (Fraction e - Fraction e_approximation asFloat) abs
     (Float e - Fraction e_approximation asFloat) abs

     19/7 can be used as an approx with ~1% error:
         Float e - (19/7) asFloat
         
     87/32 is another candidate:
         Float e - (87/32) asFloat 

     and 106/39:
         Float e - (106/39) asFloat 

o  phi
return an approximation of the constant phi as Fraction.
The approx. returned here has an error smaller than representable by float instances
(roughly 26 valid digits)

Usage example(s):

     Fraction phi
     Fraction phi asFloat - Float phi
     Fraction phi asLongFloat - LongFloat phi
     Float phi

o  pi
return an approximation of the constant pi as Fraction.
The approx. returned here has an error smaller than representable by float instances
(roughly 26 valid digits)

Usage example(s):

     Fraction pi
     Fraction pi asFloat - Float pi
     Fraction pi asLongFloat - LongFloat pi
     Float pi

o  pi1000
return an approximation of the constant pi as Fraction (>= 1000 valid decimal digits).

Usage example(s):

     Pi_1000 := nil.
     Fraction pi1000
     Fraction pi1000 asLongFloat - LongFloat pi
     LongFloat pi

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.

Usage example(s):

     Fraction pi
     Fraction pi asFloat
     
     (Fraction pi - Fraction pi_approximation asFloat) abs
     (Float pi - Fraction pi_approximation asFloat) abs

     22/7 can be used as an approx with 1% error:
         Float pi - (22/7) asFloat

o  sqrt2
return an approximation of sqrt(2) as Fraction.
The approx. returned here has an error smaller than representable by float instances
(roughly 26 valid digits)

Usage example(s):

     Fraction sqrt2
     Fraction sqrt2 asFloat - Float sqrt2
     Fraction sqrt2 asLongFloat - LongFloat sqrt2
     Fraction sqrt2 asQDouble - QDouble sqrt2
     Float sqrt2

o  unity
return the neutral element for multiplication (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.

Usage example(s):

     Fraction numerator:1 denominator:3    -> (1/3)
     Fraction numerator:-1 denominator:3   -> (-1/3)
     Fraction numerator:1 denominator:-3   -> (-1/3)
     Fraction numerator:-1 denominator:-3  -> (1/3)

     Fraction numerator:2 denominator:3   
     Fraction numerator:2 denominator:6   

     Fraction numerator:1 denominator:0  -> error
     Fraction numerator:2 denominator:0  -> error

     Fraction numerator:5 denominator:10  
     Fraction numerator:50 denominator:100
     Fraction numerator:8 denominator:16  

o  random
( an extension from the stx:libbasic2 package )
Answers a random fraction with abs between 0 and 1.

Usage example(s):

     Fraction random asFloat 

o  readDecimalFractionFrom: aStringOrStream onError: exceptionBlock
Read an arbitrary number (>0) of digits representing a decimal fraction.

Usage example(s):

     Fraction readDecimalFractionFrom:'1'   onError:[nil]     -> 0.1
     Fraction readDecimalFractionFrom:'123' onError:[nil]     -> 0.123
     Fraction readDecimalFractionFrom:'5'   onError:[nil]     -> 0.5
     Fraction readDecimalFractionFrom:'005' onError:[nil]     -> 0.005
     Fraction readDecimalFractionFrom:''    onError:[nil]     -> nil
     Fraction readDecimalFractionFrom:'aa'  onError:[nil]     -> nil

o  readFrom: aStringOrStream onError: exceptionBlock
sigh - care for subclasses...

queries
o  epsilon
return any arbitrary value as a fallback

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.

Usage example(s):

^ (numerator * aNumber) / denominator

Usage example(s):

        2/3 * 3 asLongFloat

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

Usage example(s):

        2/3 + 10 asLongFloat

o  +% aNumber
EXPERIMENTAL; may be removed without warning
return the sum of the receiver and the argument, aNumber.
If the result is a fraction, return it unreduced.
Use this only if the result is followed by another fraction arithmetic operation
which eventually reduces the result.

Usage example(s):

        2/3 +% 10 
        1/3 +% (2/3) 

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

Usage example(s):

     (1/3) - (1/9)
     (1/9) - (1/3)
     (999/1000) - (1/1000)
     (999/1000) - (1/1000000)
     (999000/1000000) - (1/1000000)

o  -% aNumber
EXPERIMENTAL; may be removed without warning
return the difference of the receiver and the argument, aNumber.
If the result is a fraction, return it unreduced.
Use this only if the result is followed by another fraction arithmetic operation
which eventually reduces the result.

Usage example(s):

     (1/3) -% (1/9)
     (1/9) -% (1/3)
     (999/1000) -% (1/1000)
     (999/1000) -% (1/1000000)
     (999000/1000000) -% (1/1000000)

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.

Usage example(s):

     0.5 // 1
     -0.5 // 1
     (1/2) // 1  = 0 ifFalse:[self halt].
     (-1/2) // 1 = -1 ifFalse:[self halt].

o  negated
optional - could use inherited method ...

o  reciprocal
optional - could use inherited method ...

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

Usage example(s):

For large numerators or denominators, we cannot simply return
        numerator asFloat / denumerator asFloat
     because those might individually overflow the float range,
     although the quotient may well be within.

Usage example(s):

      (5/9) asFloat
      (-5/9) asFloat
      (500000000000/900000000000) asFloat
      (-500000000000/900000000000) asFloat
      (500000000000/9) asFloat
      (5/900000000000) asFloat
      89012345678901234567 asFloat / 123456789123456789 asFloat
      (89012345678901234567 / 123456789123456789) asFloat

      (
       180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
        /
       180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
      ) asFloat 

      180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
         asFloat /
      180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
         asFloat

o  asFraction
return the receiver as fraction - that's the receiver itself

o  asIEEEFloat
( an extension from the stx:libbasic2 package )
return an IEEE float with (approximately) my value

Usage example(s):

      (5/9) asIEEEFloat       
      (500000000000/900000000000) asIEEEFloat
      (500000000000/9) asIEEEFloat

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

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

Usage example(s):

      (5/9) asLargeFloat                       0.555555555556
      (500000000000/900000000000) asLargeFloat 0.555555555556
      (500000000000/9) asLargeFloat            55555555555.555555555556

o  asLargeFloatPrecision: n
( an extension from the stx:libbasic2 package )
Answer a Floating point with arbitrary precision
close to the receiver.

Usage example(s):

      (5/9) asFloat                   -> 0.555555555555556
      (5/9) asLargeFloat              -> 0.5555555555555555555555555555555555555555555555555555555555554172663272
      
      (500000000000/900000000000) asFloat * 900000000000 - 500000000000
      ((500000000000/900000000000) asLargeFloatPrecision:200) * 900000000000 - 500000000000
      (500000000000/900000000000) asLargeFloat * 900000000000 - 500000000000

      (500000000000/9) asFloat                   -> 55555555555.5556
      (500000000000/9) asLongFloat               -> 55555555555.55555556
      (500000000000/9) asQuadFloat               -> 5.5555555555555429236847004268323273e+10
      (500000000000/9) asOctaFloat               -> 5.55555555555555555555555555555555555555555555555555555555555555555555553e+10
      (500000000000/9) asQDouble                 -> 5.5555555555555555555555555555555555555555555555555555555555556e+10
      (500000000000/9) asLargeFloat              -> 5.5555555555555555555555555555555555555555555555555555555556e+10
      (500000000000/9) asLargeFloatPrecision:100 -> 5.5555555555555555555555555556e+10
      (500000000000/9) asLargeFloatPrecision:300 -> 5.555555555555555555555555555555555555555555555555555555555555555555555555555556e+10

o  asLargeInteger
return an integer with my value - will usually truncate.
Not for general use:
Notice, that this may return an unnormalized large int (i.e. a large with a smallint value),
which are normally not present in the system, and not handled correctly by many functions.
This exists only as a helper for some algorithms and converters.
(i.e. use asInteger)

o  asLimitedPrecisionWithClass: floatClass
return a float with (approximately) my value.
Since floats have a limited precision, you often loose bits when doing this

Usage example(s):

      (5/9) asLimitedPrecisionWithClass:Float
      (-5/9) asLimitedPrecisionWithClass:Float
      (500000000000/900000000000) asLimitedPrecisionWithClass:Float
      (-500000000000/900000000000) asLimitedPrecisionWithClass:Float
      (500000000000/9) asLimitedPrecisionWithClass:Float
      (5/900000000000) asLimitedPrecisionWithClass:Float
      89012345678901234567 asFloat / 123456789123456789 asFloat
      (89012345678901234567 / 123456789123456789) asLimitedPrecisionWithClass:Float

      -- huge values
      (
       180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
        /
       180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
      ) asLimitedPrecisionWithClass:Float 

      180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
         asFloat /
      180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
         asFloat  

      (
       180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
        /
       180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
      ) asLimitedPrecisionWithClass:ShortFloat 

      180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
         asShortFloat /
      180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
         asShortFloat        

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

Usage example(s):

rslt := (num asLongFloat / den asLongFloat) * (2 raisedToInteger:denShift-numShift).

Usage example(s):

      (5/9) asLongFloat
      (-5/9) asLongFloat
      (Fraction basicNew setNumerator:500000000000 denominator:900000000000) asLongFloat = (5/9) asLongFloat
      (Fraction basicNew setNumerator:500000000001 denominator:900000000000) asLongFloat = (5/9) asLongFloat
      (500000000001/900000000000) asLongFloat
      (-500000000001/900000000000) asLongFloat
      (500000000001/900000000000) asLongFloat = (5/9) asLongFloat

      (500000000000/9) asLongFloat
      (5/900000000000) asLongFloat
      89012345678901234567 asFloat / 123456789123456789 asLongFloat
      (89012345678901234567 / 123456789123456789) asLongFloat
      (-89012345678901234567 / 123456789123456789) asLongFloat

      (
       180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
        /
       180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
      ) asLongFloat

      180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
         asLongFloat /
      180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
         asLongFloat

o  asOctaFloat
( an extension from the stx:libbasic2 package )
return an OctaFloat with (approximately) my value.
Since floats have a limited precision, you usually loose bits when doing this.

Usage example(s):

rslt := (num asOctaFloat / den asOctaFloat) * (2 raisedToInteger:denShift-numShift).

Usage example(s):

      (5/9) asOctaFloat
      (-5/9) asOctaFloat
      (Fraction basicNew setNumerator:500000000000 denominator:900000000000) asOctaFloat = (5/9) asOctaFloat
      (Fraction basicNew setNumerator:500000000001 denominator:900000000000) asOctaFloat = (5/9) asOctaFloat
      (500000000001/900000000000) asOctaFloat
      (-500000000001/900000000000) asOctaFloat
      (500000000001/900000000000) asOctaFloat = (5/9) asOctaFloat

      (500000000000/9) asOctaFloat
      (5/900000000000) asOctaFloat
      89012345678901234567 asFloat / 123456789123456789 asOctaFloat
      (89012345678901234567 / 123456789123456789) asOctaFloat
      (-89012345678901234567 / 123456789123456789) asOctaFloat

      (
       180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
        /
       180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
      ) asOctaFloat

      180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
         asOctaFloat /
      180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
         asOctaFloat

o  asQDouble
return a QDouble float with (approximately) my value

Usage example(s):

      (5/9) asQDouble                       0.55555555555555555555555555555555555555555555555555555555555556
      (500000000000/900000000000) asQDouble 0.55555555555555555555555555555555555555555555555555555555555556
      (500000000000/9) asQDouble            5.5555555555555555555555555555555555555555555555555555555555556e+10

o  asQuadFloat
( an extension from the stx:libbasic2 package )
return a QuadFloat with (approximately) my value.
Since floats have a limited precision, you usually loose bits when doing this.

Usage example(s):

      (5/9) asQuadFloat
      (-5/9) asQuadFloat
      (Fraction basicNew setNumerator:500000000000 denominator:900000000000) asQuadFloat = (5/9) asQuadFloat
      (Fraction basicNew setNumerator:500000000001 denominator:900000000000) asQuadFloat = (5/9) asQuadFloat
      (500000000001/900000000000) asQuadFloat
      (-500000000001/900000000000) asQuadFloat
      (500000000001/900000000000) asQuadFloat = (5/9) asQuadFloat

      (500000000000/9) asQuadFloat
      (5/900000000000) asQuadFloat
      89012345678901234567 asFloat / 123456789123456789 asQuadFloat
      (89012345678901234567 / 123456789123456789) asQuadFloat
      (-89012345678901234567 / 123456789123456789) asQuadFloat

      (
       180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
        /
       180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
      ) asQuadFloat

      180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
         asQuadFloat /
      180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
         asQuadFloat

o  asScaledDecimal
return the receiver as fixedPoint number.
Q: what should the scale be here ?

Usage example(s):

     (1/2) asScaledDecimal 0.50
     (1/3) asScaledDecimal 0.33
     (5/9) asScaledDecimal 0.56
     (5/9) asScaledDecimal:10  0.5555555556
     (5/9) asScaledDecimal:20  0.55555555555555555556

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

Usage example(s):

     (1/2) asScaledDecimal:2
     (1/3) asScaledDecimal:2
     (1/3) asScaledDecimal:5
     (2/3) asScaledDecimal:2
     (2/3) asScaledDecimal:5

     (5/9) asScaledDecimal     0.56
     (5/9) asScaledDecimal:5   0.55556
     (5/9) asScaledDecimal:10  0.5555555556
     (5/9) asScaledDecimal:20  0.55555555555555555556

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

Usage example(s):

^ self asFloat asShortFloat

Usage example(s):

      (5/9) asShortFloat                             -> 0.5555556
      (500000000000/900000000000) asShortFloat       -> 0.5555556
      (5e300/9e300) asShortFloat                     -> 0.5555556
      (5q3000/9q3000) asShortFloat                   -> 0.5555556
      ((5*(10**10000))/(9*(10**10000))) asShortFloat -> 0.5555556
      (500000000000/9) asShortFloat                  -> 5.555556e+10

o  asTrueFraction
Answer a fraction or integer that EXACTLY represents the receiver

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

Usage example(s):

        self assert:(1.00s = 1).
        self assert:(1 = 1.00s).
        self assert:(1.00s = 1.0).
        self assert:(1.0 = 1.00s).
        self assert:(1.20s = 1.2s).
        self assert:((12/10) = (120/100)).
        self assert:((12/10) = 1.2s).
        self assert:(1.2s = (12/10)).
        self assert:((120/100) = 1.2s).
        self assert:(1.20s = (120/100)).

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

o  epsilonForCloseTo
return the epsilon used in the closeTo: comparison.
Here, we return the receiver's magnitude divided by 10000

Usage example(s):

     1.0 epsilonForCloseTo                           
     (1/10) epsilonForCloseTo                           
     10 asShortFloat epsilonForCloseTo      
     10000 asShortFloat epsilonForCloseTo      

o  hash
return a number for hashing; redefined, since fractions compare
by numeric value (i.e. (1/2) = 0.5), hash values must be the same

Usage example(s):

     3 hash
     (9/3) hash
     3.0 hash
     (1/2) hash
     (1/4) hash
     0.0 hash
     0.5 hash
     0.25 hash
     0.4 hash

     0.25 hash
     -0.25 hash
     (1/4) hash
     (-1/4) hash

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

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

o  differenceFromFraction: aFraction
save a multiplication if possible

o  differenceFromFractionUnreduced: aFraction
EXPERIMENTAL; may be removed without warning

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

o  differenceFromScaledDecimal: aScaledDecimal
sent when a scaled decimal does not know how to subtract the receiver, a fraction

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

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.
Return true if aFraction < self.

o  lessFromInteger: anInteger
sent when an integer does not know how to compare to the receiver, a fraction.
Return true if anInteger < self.

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

o  productFromFraction: aFraction
sent when a fraction does not know how to multiply the receiver, a fraction

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

o  productFromScaledDecimal: aScaledDecimal
sent when a scaled decimal does not know how to multiply the receiver, a fraction

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.
Return aFloat / self

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  quotientFromScaledDecimal: aScaledDecimal
Return the quotient of the argument, aScaledDecimal and the receiver.
Sent when aScaledDecimal does not know how to divide by the receiver.

o  raisedFromFloat: aFloat
aFloat does not know how to be raised to the receiver

o  raisedFromNumber: aNumber
aNumber does not know how to be raised to the receiver

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

o  sumFromFraction: aFraction
sent when a fraction does not know how to add the receiver, a fraction

o  sumFromFractionUnreduced: aFraction
EXPERIMENTAL; may be removed without warning

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

o  sumFromScaledDecimal: aScaledDecimal
sent when a scaled decimal does not know how to add the receiver, a fraction

inspecting
o  inspectorExtraAttributes
( an extension from the stx:libtool package )
extra (pseudo instvar) entries to be shown in an inspector.

o  inspectorValueListIconFor: anInspector
( an extension from the stx:libtool package )
returns the icon to be shown alongside the value list of an inspector

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

o  printOn: aStream base: base showRadix: showRadix
(16/500) printOn:Transcript base:16 showRadix:true

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  isExact
Answer whether the receiver performs exact arithmetic.

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 less than zero

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

Usage example(s):

     (3/2) fractionPart + (3/2) truncated
     (-3/2) fractionPart + (-3/2) truncated        

     (3/2) fractionPart
     (-3/2) fractionPart
     (3/2) asFloat fractionPart
     (-3/2) asFloat fractionPart
     (2/3) fractionPart
     ((3/2)*(15/4)) fractionPart
     ((2/3)*(4/15)) fractionPart

     (1/4) fractionPart       
     (5/4) fractionPart       
     (6/4) fractionPart       
     (8/9) fractionPart       

     (16/10) fractionPart     
     (16/10) truncated     
     (16/10) fractionPart + (16/10) truncated     

     (11/9) fractionPart                     
     (11/9) fractionPart + (11/9) truncated   

     (12/10) asFixedPoint fractionPart   
     1.2345s asFixedPoint fractionPart   

o  integerPart
extract the pre-decimal integer part.

Usage example(s):

^ super integerPart

Usage example(s):

     (3/2) integerPart  
     (-3/2) integerPart   
     (2/3) integerPart
     ((3/2)*(15/4)) integerPart
     ((2/3)*(4/15)) integerPart

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

Usage example(s):

     (1/3) rounded
     (1/3) negated rounded
     (1/2) rounded
     (1/2) negated rounded
     (-1/2) rounded   
     0.5 rounded
     -0.5 rounded   
     (2/3) rounded   
     (2/3) negated rounded 

o  truncated
return the receiver truncated towards zero as Integer

Usage example(s):

     (3/2) truncated
     (3/2) negated truncated

o  truncatedAsFloat
For protocol completeness;
integer values are never represented as a fraction

Usage example(s):

     (3/2) truncated           1
     (3/2) truncatedAsFloat    1   not (1/1)

visiting
o  acceptVisitor: aVisitor with: aParameter
dispatch for visitor pattern; send #visitFraction:with: to aVisitor



ST/X 7.7.0.0; WebServer 1.702 at 20f6060372b9.unknown:8081; Mon, 18 Nov 2024 00:43:59 GMT