Fraction.st
branchjv
changeset 18062 014678b4657a
parent 18011 deb0c3355881
parent 15278 ddf05cd996da
child 18120 e3a375d5f6a8
equal deleted inserted replaced
18061:2643e3012a13 18062:014678b4657a
   524 
   524 
   525     ^ self asInteger asLargeInteger
   525     ^ self asInteger asLargeInteger
   526 !
   526 !
   527 
   527 
   528 asLongFloat
   528 asLongFloat
   529     "return a long float with (approximately) my value"
   529     "return a long float with (approximately) my value.
       
   530      Since floats have a limited precision, you usually loose bits when doing this."
   530 
   531 
   531     |num den numShift denShift numBits rslt|
   532     |num den numShift denShift numBits rslt|
   532 
   533 
   533     (numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[
   534     (numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[
   534         ^ (numerator asLongFloat) / (denominator asLongFloat)
   535         ^ (numerator asLongFloat) / (denominator asLongFloat)
   537     "Do it the hard way: reduce magnitude and undo reduction on the quotient"
   538     "Do it the hard way: reduce magnitude and undo reduction on the quotient"
   538 
   539 
   539     numBits := LongFloat precision * 2.    "number of bits to preserve (conservative)"
   540     numBits := LongFloat precision * 2.    "number of bits to preserve (conservative)"
   540     num := numerator abs.
   541     num := numerator abs.
   541     numShift := numBits - num highBit. "(num highBit - bits) negated"
   542     numShift := numBits - num highBit. "(num highBit - bits) negated"
   542     numShift < 0 ifTrue:[
   543     numShift < 0 ifTrue:[num := num bitShift:numShift] ifFalse:[ numShift := 0].
   543         num := num bitShift:numShift
       
   544     ] ifFalse:[
       
   545         numShift := 0
       
   546     ].
       
   547 
   544 
   548     den :=  denominator.
   545     den :=  denominator.
   549     denShift := numBits - den highBit. "(den highBit - bits) negated"
   546     denShift := numBits - den highBit. "(den highBit - bits) negated"
   550     denShift < 0 ifTrue:[
   547     denShift < 0 ifTrue:[den := den bitShift:denShift] ifFalse:[denShift := 0].
   551         den := den bitShift:denShift
       
   552     ] ifFalse:[
       
   553         denShift := 0
       
   554     ].
       
   555 
   548 
   556     rslt := (num asLongFloat / den asLongFloat) * (2 raisedToInteger:denShift-numShift).
   549     rslt := (num asLongFloat / den asLongFloat) * (2 raisedToInteger:denShift-numShift).
   557     numerator negative ifTrue:[ ^ rslt negated ].
   550     numerator negative ifTrue:[ ^ rslt negated ].
   558     ^ rslt.
   551     ^ rslt.
   559 
   552 
  1156 ! !
  1149 ! !
  1157 
  1150 
  1158 !Fraction class methodsFor:'documentation'!
  1151 !Fraction class methodsFor:'documentation'!
  1159 
  1152 
  1160 version
  1153 version
  1161     ^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.81 2011-05-03 09:08:52 cg Exp $'
  1154     ^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.82 2013-05-27 08:14:32 cg Exp $'
  1162 !
  1155 !
  1163 
  1156 
  1164 version_CVS
  1157 version_CVS
  1165     ^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.81 2011-05-03 09:08:52 cg Exp $'
  1158     ^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.82 2013-05-27 08:14:32 cg Exp $'
  1166 ! !
  1159 ! !
       
  1160 
  1167 
  1161 
  1168 Fraction initialize!
  1162 Fraction initialize!