The Trunk: Kernel-nice.1109.mcz

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

The Trunk: Kernel-nice.1109.mcz

commits-2
Nicolas Cellier uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-nice.1109.mcz

==================== Summary ====================

Name: Kernel-nice.1109
Author: nice
Time: 23 July 2017, 2:42:14.918543 pm
UUID: 1496608c-48fd-4bb2-b572-46e8b61421a9
Ancestors: Kernel-eem.1108

Define gcd: and lcm: for Fraction.

For example, 1/5 and 1/7 are both whole multiple of 1/35 (which is their gcd).

=============== Diff against Kernel-eem.1108 ===============

Item was added:
+ ----- Method: Fraction>>gcd: (in category 'arithmetic') -----
+ gcd: aFraction
+ | d |
+ d := denominator gcd: aFraction denominator.
+ ^(numerator *(aFraction denominator/d) gcd: aFraction numerator*(denominator/d)) / (denominator/d*aFraction denominator)!

Item was added:
+ ----- Method: Fraction>>lcm: (in category 'arithmetic') -----
+ lcm: n
+ "Answer the least common multiple of the receiver and n."
+
+ ^self // (self gcd: n) * n!

Item was changed:
  ----- Method: Integer>>gcd: (in category 'mathematical functions') -----
  gcd: anInteger
  "See Knuth, Vol 2, 4.5.2, Algorithm L"
  "Initialize"
  | higher u v k uHat vHat a b c d vPrime vPrimePrime q t |
+ anInteger denominator = 1 ifFalse: [^anInteger gcd: self].
  higher := SmallInteger maxVal highBit.
  u := self abs max: (v := anInteger abs).
  v := self abs min: v.
  [v class == SmallInteger]
  whileFalse:
  [(uHat := u bitShift: (k := higher - u highBit)) class == SmallInteger
  ifFalse:
  [k := k - 1.
  uHat := uHat bitShift: -1].
  vHat := v bitShift: k.
  a := 1.
  b := 0.
  c := 0.
  d := 1.
  "Test quotient"
  [(vPrime := vHat + d) ~= 0
  and: [(vPrimePrime := vHat + c) ~= 0 and: [(q := uHat + a // vPrimePrime) = (uHat + b // vPrime)]]]
  whileTrue:
  ["Emulate Euclid"
  c := a - (q * (a := c)).
  d := b - (q * (b := d)).
  vHat := uHat - (q * (uHat := vHat))].
  "Multiprecision step"
  b = 0
  ifTrue:
  [v := u rem: (u := v)]
  ifFalse:
  [t := u * a + (v * b).
  v := u * c + (v * d).
  u := t]].
  ^ v gcd: u!

Item was changed:
  ----- Method: SmallInteger>>gcd: (in category 'arithmetic') -----
  gcd: anInteger
  "See SmallInteger (Integer) | gcd:"
  | n m |
+ anInteger denominator = 1 ifFalse: [^anInteger gcd: self].
  n := self.
  m := anInteger.
  [n = 0]
  whileFalse:
  [n := m \\ (m := n)].
  ^ m abs!