diff -r e3b499db0368 -r 1fa66e549cee IEEEFloat.st --- a/IEEEFloat.st Sat Dec 07 18:46:20 2019 +0100 +++ b/IEEEFloat.st Sun Dec 08 04:10:28 2019 +0100 @@ -87,13 +87,55 @@ ! fromInteger:anInteger - ^ self fromFloat:anInteger asFloat + "convert to one of 64, 128 or 256 bit IEEEFloat" + + |nM nE h l fraction e bias absInt nBytes| + + absInt := anInteger abs. + h := absInt highBit. + l := absInt lowBit. + nM := Float numBitsInMantissa. + nE := Float numBitsInExponent. + bias := Float eBias. + nBytes := 64 / 8. + (h - l) > nM ifTrue:[ + nM := QuadFloat numBitsInMantissa. + nE := QuadFloat numBitsInExponent. + bias := QuadFloat eBias. + nBytes := 128 / 8. + (h - l) > nM ifTrue:[ + nM := OctaFloat numBitsInMantissa. + nE := OctaFloat numBitsInExponent. + bias := OctaFloat eBias. + nBytes := 256 / 8. + ] + ]. + + "/ cut off precision by shifting right (if h > nM) + "/ or add zeros by shifting to the left + h == 0 ifTrue:[ + fraction := e := 0. + ] ifFalse:[ + fraction := absInt bitShift:(nM - h + 1). + fraction := fraction bitAnd:((1 bitShift:nM)-1). + e := bias + h - 1. + ]. + ^ (self basicNew:nBytes) exponentSize:nE; + setFraction:fraction exponent:e + signBit:(anInteger negative ifTrue:[1] ifFalse:[0]); + yourself " self fromInteger:1 + self fromInteger:-1 self fromInteger:2 + self fromInteger:1024 * 1024 * 1024 * 1024 * 1024 * 1024 self fromInteger:1e20 asInteger self fromInteger:1e100 asInteger + self fromInteger:2r1010101010101010101010101010101 + self fromInteger:2r1010101010101010101010101010101010101010101010101010101010101010 + self fromInteger:1e-100 asInteger + 1 asIEEEFloat " ! @@ -359,6 +401,10 @@ !IEEEFloat methodsFor:'coercing & converting'! +asIEEEFloat + ^ self +! + coerce:aNumber "convert the argument aNumber into an instance of the receiver's class and return it. Redefined to preserve the size and exponentSize" @@ -974,12 +1020,6 @@ 0.0 asIEEEFloat negative -0.0 asIEEEFloat negative " -! - -positive - "return true if the receiver is greater or equal to zero." - - ^ ((self basicAt:self basicSize) bitTest:16r80) not ! ! !IEEEFloat methodsFor:'truncation & rounding'!