Nicolas Cellier uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-nice.1122.mcz ==================== Summary ==================== Name: Kernel-nice.1122 Author: nice Time: 14 November 2017, 8:40:24.173283 pm UUID: fa7a025a-ef0d-431c-80af-32d0a9f170b5 Ancestors: Kernel-eem.1121, Kernel-nice.1120 Merge Kernel-nice.1120 and correct Fraction comment typo =============== Diff against Kernel-eem.1121 =============== Item was changed: Number subclass: #Fraction instanceVariableNames: 'numerator denominator' classVariableNames: '' poolDictionaries: '' category: 'Kernel-Numbers'! + !Fraction commentStamp: 'nice 11/14/2017 20:39' prior: 0! + Fraction provides methods for dealing with fractions like 1/3 as a ratio of two integers (as apposed to a decimal representation 0.33333...). - !Fraction commentStamp: '<historical>' prior: 0! - Fraction provides methods for dealing with fractions like 1/3 as fractions (not as 0.33333...). All public arithmetic operations answer reduced fractions (see examples). + instance variables: + numerator <Integer> the number appearing before the fraction bar (above) + denominator <Integer> the number appearing after the fraction bar (below) + + A Fraction is generally created by sending the message / to an Integer, like in - instance variables: 'numerator denominator ' + 1 / 3 - Examples: (note the parentheses required to get the right answers in Smalltalk and Squeak): + Alternatively, it is possible to create a new instance of Fraction by sending #numerator:denominator: to the class. + In this later case, it is then user responsibility to ensure that it conforms to the following invariants: + + - the denominator shall allways be positive. + A negative Fraction shall have a negative numerator, never a negative denominator. + Example: 1 / -3 will return -1/3 + - the denominator shall allways be greater than 1. + A Fraction with denominator 1 shall be reduced to its numerator (an Integer). + Example 3 / 1 will answer 3 (the Integer) not 3/1 + - the numerator and denominator shall never have common multiples. + Common multiples shall allways be simplified until (numerator gcd: denominator) = 1. + Example 8 / 6 will answer 4 / 3, because both 8=2*4 and 6=2*3 are both divisible by 2. + + A Fraction non conforming to above invariants could be the cause of undefined behavior and unexpected results. + If unsure, it is advised to send the message #reduced to the freshly created instance so as to obtain a conforming Fraction, or an Integer. + + Note that Fraction and Integer represent together the set of Rational numbers: + - Integer is a subset of rational (those which are whole numbers) + - Fraction is used for representing the complementary subset of rational (those which are not whole numbers) + + There could have been a Rational superclass to both Integer and Fraction, and a message #isRational for testing if a Number is a Rational, as well as a message #asRational for converting a Number to a Rational. + But this level of indirection is not strictly necessary: instead, the notion of Rational and Fraction are collapsed in Squeak, and Integer are considered as a sort of special Fraction with unitary denominator. + Thus #isFraction is the testing message, to which every Integer will also answer true, since considered as a sort of Fraction. + And #asFraction is the conversion message, that may answer an instance of Fraction or Integer, depending if the corresponding rational number is whole or not. + + All public arithmetic operations will answer reduced fractions. + Examples: + (2/3) + (2/3) + (2/3) + (1/2) "case showing reduction to common denominator" + (2/3) + (4/3) "case where result is reduced to an Integer" + (2/3) raisedToInteger: 5 "fractions also can be exponentiated" - (2/3) + (1/2) "answers shows the reduced fraction" - (2/3) raisedToInteger: 5 "fractions also can have exponents" ! Item was changed: ----- 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)! - ^(numerator *(aFraction denominator/d) gcd: aFraction numerator*(denominator/d)) / (denominator/d*aFraction denominator)! |
Free forum by Nabble - Resume Templates | Edit this page |