IEEEFloat.st
changeset 5357 84fd4db36651
parent 5356 c5036483d628
child 5358 339af06b05c9
equal deleted inserted replaced
5356:c5036483d628 5357:84fd4db36651
       
     1 "{ Encoding: utf8 }"
       
     2 
     1 "{ Package: 'stx:libbasic2' }"
     3 "{ Package: 'stx:libbasic2' }"
     2 
     4 
     3 "{ NameSpace: Smalltalk }"
     5 "{ NameSpace: Smalltalk }"
     4 
     6 
     5 LimitedPrecisionReal variableByteSubclass:#IEEEFloat
     7 LimitedPrecisionReal variableByteSubclass:#IEEEFloat
   403     anIEEEFloat basicSize == nBytes ifTrue:[
   405     anIEEEFloat basicSize == nBytes ifTrue:[
   404         anIEEEFloat exponentSize == exponentSize ifTrue:[
   406         anIEEEFloat exponentSize == exponentSize ifTrue:[
   405             1 to:nBytes-1 do:[:i |
   407             1 to:nBytes-1 do:[:i |
   406                 (self basicAt:i) = (anIEEEFloat basicAt:i) ifFalse:[^ false].
   408                 (self basicAt:i) = (anIEEEFloat basicAt:i) ifFalse:[^ false].
   407             ].
   409             ].
       
   410             "/ care for negative zero
   408             (self basicAt:nBytes) = (anIEEEFloat basicAt:nBytes) ifFalse:[
   411             (self basicAt:nBytes) = (anIEEEFloat basicAt:nBytes) ifFalse:[
   409                 ((self basicAt:nBytes) bitAnd:16r7F) == 0 ifTrue:[
   412                 ((self basicAt:nBytes) bitAnd:16r7F) == 0 ifTrue:[
   410                     ((anIEEEFloat basicAt:nBytes) bitAnd:16r7F) == 0 ifTrue:[
   413                     ((anIEEEFloat basicAt:nBytes) bitAnd:16r7F) == 0 ifTrue:[
   411                         ^ true.
   414                         ^ true.
   412                     ].
   415                     ].
   445 
   448 
   446     ^ true.
   449     ^ true.
   447 !
   450 !
   448 
   451 
   449 lessFromIEEEFloat:anIEEEFloat
   452 lessFromIEEEFloat:anIEEEFloat
   450     |m1 m2 nM1 nM2 e1 e2|
   453     |m1 m2 nM1 nM2 e1 e2 meNegative mySize myByte otherByte isLess|
   451 
   454 
   452     self negative = anIEEEFloat negative ifFalse:[
   455     meNegative := self negative.
       
   456     meNegative = anIEEEFloat negative ifFalse:[
   453         ^ anIEEEFloat negative
   457         ^ anIEEEFloat negative
   454     ].
   458     ].
   455 
   459 
   456     anIEEEFloat basicSize == self basicSize ifTrue:[
   460     "/ same sized floats can be compared easily
       
   461     "/ (but care for the negative 0)
       
   462     mySize := self basicSize.
       
   463     mySize == anIEEEFloat basicSize ifTrue:[
   457         anIEEEFloat exponentSize == exponentSize ifTrue:[
   464         anIEEEFloat exponentSize == exponentSize ifTrue:[
   458             self basicSize to:1 by:-1 do:[:i |
   465             "/ ignore the sign bit
   459                 (anIEEEFloat basicAt:i) < (self basicAt:i) ifTrue:[^ true].
   466             myByte := (self basicAt:mySize) bitAnd:16r7F.
   460                 (anIEEEFloat basicAt:i) > (self basicAt:i) ifTrue:[^ false].
   467             otherByte := (anIEEEFloat basicAt:mySize) bitAnd:16r7F.    
       
   468             otherByte < myByte ifTrue:[
       
   469                 ^ meNegative not
   461             ].
   470             ].
       
   471             otherByte > myByte ifTrue:[
       
   472                 ^ meNegative
       
   473             ].
       
   474             mySize-1 to:1 by:-1 do:[:i |
       
   475                 myByte := self basicAt:i.
       
   476                 otherByte := anIEEEFloat basicAt:i.
       
   477                 otherByte < myByte ifTrue:[
       
   478                     ^ meNegative not
       
   479                 ].
       
   480                 otherByte > myByte ifTrue:[
       
   481                     ^ meNegative
       
   482                 ].
       
   483             ].
       
   484             "/ same
   462             ^ false.
   485             ^ false.
   463         ].
   486         ].
   464     ].
   487     ].
   465 
   488 
   466     "/ more complicated compare
   489     "/ more complicated compare
   480         ].
   503         ].
   481     ].
   504     ].
   482 
   505 
   483     e1 := anIEEEFloat exponent.
   506     e1 := anIEEEFloat exponent.
   484     e2 := self exponent.
   507     e2 := self exponent.
   485     e1 = e2 ifTrue:[^ m1 < m2].
   508     isLess := e1 = e2 ifTrue:[m1 < m2] ifFalse:[e1 < e2].
   486     ^ e1 < e2.
   509     meNegative ifTrue:[^ isLess not].
       
   510     ^ isLess.
       
   511 
       
   512     "
       
   513      self assert:(1.0 asIEEEFloat < 2.0 asIEEEFloat).
       
   514      self assert:(1.0 asIEEEFloat < 1.0 asIEEEFloat) not.
       
   515      self assert:(1.0 asIEEEFloat < 0.0 asIEEEFloat) not.
       
   516      self assert:(1.0 asIEEEFloat < -0.0 asIEEEFloat) not.
       
   517 
       
   518      self assert:(-1.0 asIEEEFloat < 2.0 asIEEEFloat).
       
   519      self assert:(-1.0 asIEEEFloat < 1.0 asIEEEFloat).
       
   520      self assert:(-1.0 asIEEEFloat < 0.0 asIEEEFloat).
       
   521      self assert:(-1.0 asIEEEFloat < -0.0 asIEEEFloat).
       
   522 
       
   523      self assert:(1.0 asIEEEFloat < -2.0 asIEEEFloat) not.
       
   524      self assert:(1.0 asIEEEFloat < -1.0 asIEEEFloat) not.
       
   525      self assert:(1.0 asIEEEFloat < 0.0 asIEEEFloat) not.
       
   526      self assert:(1.0 asIEEEFloat < -0.0 asIEEEFloat) not.
       
   527 
       
   528      self assert:(-1.0 asIEEEFloat < -2.0 asIEEEFloat) not.
       
   529      self assert:(-1.0 asIEEEFloat < -1.0 asIEEEFloat) not.
       
   530      self assert:(-1.0 asIEEEFloat < 0.0 asIEEEFloat).
       
   531      self assert:(-1.0 asIEEEFloat < -0.0 asIEEEFloat).
       
   532     "
   487 !
   533 !
   488 
   534 
   489 productFromIEEEFloat:anIEEEFloat
   535 productFromIEEEFloat:anIEEEFloat
   490     "/ anIEEEFloat * self
   536     "/ anIEEEFloat * self
   491 
   537