extensions.st
changeset 5293 db346a540bf9
parent 5292 975a4c7373cb
child 5296 825d2a1ce06a
equal deleted inserted replaced
5292:975a4c7373cb 5293:db346a540bf9
    14      123 asLargeFloat asQDouble
    14      123 asLargeFloat asQDouble
    15     "
    15     "
    16 
    16 
    17     "Created: / 13-06-2017 / 16:48:24 / cg"
    17     "Created: / 13-06-2017 / 16:48:24 / cg"
    18     "Modified (comment): / 25-07-2017 / 16:06:19 / cg"
    18     "Modified (comment): / 25-07-2017 / 16:06:19 / cg"
       
    19 ! !
       
    20 
       
    21 !ArithmeticValue methodsFor:'converting'!
       
    22 
       
    23 asQuadFloat
       
    24     "return a quadFloat with same value"
       
    25 
       
    26     "WARNING: could loose precision here, if not redefined in concrete classes which
       
    27      have more than float precision (i.e. LargeIntegers and Fractions)"
       
    28 
       
    29     ^ self asFloat asQuadFloat "/ subclassResponsibility
       
    30 
       
    31     "Created: / 07-06-2019 / 02:28:54 / Claus Gittinger"
    19 ! !
    32 ! !
    20 
    33 
    21 !ArithmeticValue methodsFor:'double dispatching'!
    34 !ArithmeticValue methodsFor:'double dispatching'!
    22 
    35 
    23 differenceFromQDouble:aQDouble
    36 differenceFromQDouble:aQDouble
   867     "Modified (format): / 20-06-2017 / 11:49:07 / cg"
   880     "Modified (format): / 20-06-2017 / 11:49:07 / cg"
   868 ! !
   881 ! !
   869 
   882 
   870 !Float methodsFor:'coercing & converting'!
   883 !Float methodsFor:'coercing & converting'!
   871 
   884 
       
   885 asOctaFloat
       
   886     "return an octaFloat with same value as receiver"
       
   887 
       
   888     ^ OctaFloat fromFloat:self
       
   889 
       
   890     "
       
   891      123 asFloat asOctaFloat
       
   892      0 asFloat asOctaFloat
       
   893      0.0 asOctaFloat
       
   894     "
       
   895 ! !
       
   896 
       
   897 !Float methodsFor:'coercing & converting'!
       
   898 
   872 asQDouble
   899 asQDouble
   873     "return a QDouble with my value"
   900     "return a QDouble with my value"
   874 
   901 
   875     ^ QDouble fromFloat:self
   902     ^ QDouble fromFloat:self
   876 
   903 
   877     "
   904     "
   878      1.0 asQDouble
   905      1.0 asQDouble
   879     "
   906     "
   880 
   907 
   881     "Created: / 13-06-2017 / 16:48:57 / cg"
   908     "Created: / 13-06-2017 / 16:48:57 / cg"
       
   909 ! !
       
   910 
       
   911 !Float methodsFor:'coercing & converting'!
       
   912 
       
   913 asQuadFloat
       
   914     "return a quadFloat with same value as receiver"
       
   915 
       
   916     ^ QuadFloat fromFloat:self
       
   917 
       
   918     "
       
   919      123 asFloat asQuadFloat
       
   920      0 asFloat asQuadFloat
       
   921      0.0 asQuadFloat
       
   922     "
       
   923 
       
   924     "Created: / 07-06-2019 / 02:28:26 / Claus Gittinger"
       
   925     "Modified (comment): / 10-06-2019 / 21:51:04 / Claus Gittinger"
       
   926 ! !
       
   927 
       
   928 !Fraction methodsFor:'coercing & converting'!
       
   929 
       
   930 asQuadFloat
       
   931     "return a QuadFloat with (approximately) my value.
       
   932      Since floats have a limited precision, you usually loose bits when doing this."
       
   933 
       
   934     |num den numShift denShift numBits rslt|
       
   935 
       
   936     (numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[
       
   937         ^ (numerator asQuadFloat) / (denominator asQuadFloat)
       
   938     ].
       
   939 
       
   940     "Do it the hard way: reduce magnitude and undo reduction on the quotient"
       
   941 
       
   942     numBits := QuadFloat precision * 2.    "number of bits to preserve (conservative)"
       
   943     num := numerator abs.
       
   944     numShift := numBits - num highBit. "(num highBit - bits) negated"
       
   945     numShift < 0 ifTrue:[num := num bitShift:numShift] ifFalse:[ numShift := 0].
       
   946 
       
   947     den :=  denominator.
       
   948     denShift := numBits - den highBit. "(den highBit - bits) negated"
       
   949     denShift < 0 ifTrue:[den := den bitShift:denShift] ifFalse:[denShift := 0].
       
   950 
       
   951     rslt := (num asQuadFloat / den asQuadFloat) * (2 raisedToInteger:denShift-numShift).
       
   952     numerator negative ifTrue:[ ^ rslt negated ].
       
   953     ^ rslt.
       
   954 
       
   955     "
       
   956       (5/9) asQuadFloat
       
   957       (-5/9) asQuadFloat
       
   958       (Fraction basicNew setNumerator:500000000000 denominator:900000000000) asQuadFloat = (5/9) asQuadFloat
       
   959       (Fraction basicNew setNumerator:500000000001 denominator:900000000000) asQuadFloat = (5/9) asQuadFloat
       
   960       (500000000001/900000000000) asQuadFloat
       
   961       (-500000000001/900000000000) asQuadFloat
       
   962       (500000000001/900000000000) asQuadFloat = (5/9) asQuadFloat
       
   963 
       
   964       (500000000000/9) asQuadFloat
       
   965       (5/900000000000) asQuadFloat
       
   966       89012345678901234567 asFloat / 123456789123456789 asQuadFloat
       
   967       (89012345678901234567 / 123456789123456789) asQuadFloat
       
   968       (-89012345678901234567 / 123456789123456789) asQuadFloat
       
   969 
       
   970       (
       
   971        180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
       
   972         /
       
   973        180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
       
   974       ) asQuadFloat
       
   975 
       
   976       180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
       
   977          asQuadFloat /
       
   978       180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
       
   979          asQuadFloat
       
   980     "
       
   981 
       
   982     "Created: / 07-06-2019 / 02:32:19 / Claus Gittinger"
   882 ! !
   983 ! !
   883 
   984 
   884 !Integer methodsFor:'coercing & converting'!
   985 !Integer methodsFor:'coercing & converting'!
   885 
   986 
   886 asOctaFloat
   987 asOctaFloat
   946 
  1047 
   947     "
  1048     "
   948      100 atRandom
  1049      100 atRandom
   949      1000 atRandom
  1050      1000 atRandom
   950     "
  1051     "
       
  1052 ! !
       
  1053 
       
  1054 !LimitedPrecisionReal methodsFor:'coercing & converting'!
       
  1055 
       
  1056 asOctaFloat
       
  1057     ^ OctaFloat fromLimitedPrecisionReal:self
       
  1058 
       
  1059     "Created: / 07-06-2019 / 02:30:07 / Claus Gittinger"
       
  1060 ! !
       
  1061 
       
  1062 !LimitedPrecisionReal methodsFor:'coercing & converting'!
       
  1063 
       
  1064 asQuadFloat
       
  1065     ^ QuadFloat fromLimitedPrecisionReal:self
       
  1066 
       
  1067     "Created: / 07-06-2019 / 02:30:07 / Claus Gittinger"
       
  1068 ! !
       
  1069 
       
  1070 !LongFloat methodsFor:'coercing & converting'!
       
  1071 
       
  1072 asQuadFloat
       
  1073     "return a QuadFloat with same value as the receiver"
       
  1074 
       
  1075     ^ QuadFloat fromLongFloat:self
       
  1076 
       
  1077     "Created: / 07-06-2019 / 02:46:30 / Claus Gittinger"
   951 ! !
  1078 ! !
   952 
  1079 
   953 !Object methodsFor:'dependents-interests'!
  1080 !Object methodsFor:'dependents-interests'!
   954 
  1081 
   955 addInterest:anInterest
  1082 addInterest:anInterest
  1610 
  1737 
  1611 when:anAspect sendTo:anObject
  1738 when:anAspect sendTo:anObject
  1612     self expressInterestIn:anAspect for:anObject sendBack:anAspect
  1739     self expressInterestIn:anAspect for:anObject sendBack:anAspect
  1613 ! !
  1740 ! !
  1614 
  1741 
       
  1742 !ShortFloat methodsFor:'coercing & converting'!
       
  1743 
       
  1744 asQuadFloat
       
  1745     "return a QuadFloat with same value as the receiver"
       
  1746 
       
  1747     ^ QuadFloat fromShortFloat:self
       
  1748 
       
  1749     "Created: / 07-06-2019 / 02:29:14 / Claus Gittinger"
       
  1750 ! !
       
  1751 
  1615 !Stream methodsFor:'stacked computing streams'!
  1752 !Stream methodsFor:'stacked computing streams'!
  1616 
  1753 
  1617 collecting:aBlock
  1754 collecting:aBlock
  1618     "return a stacked computing stream, which reads elements from the receiver,
  1755     "return a stacked computing stream, which reads elements from the receiver,
  1619      applies aBlock to each read element, and provides the results as elements to its reader."
  1756      applies aBlock to each read element, and provides the results as elements to its reader."