class: LimitedPrecisionReal
authorStefan Vogel <sv@exept.de>
Tue, 16 Dec 2014 13:20:28 +0100
changeset 17221 1ee235ddab7f
parent 17220 614c3a1d3c73
child 17222 9ff268844b9c
class: LimitedPrecisionReal added: #digitBytesMSB: #isIEEEFormat #digitBytes
LimitedPrecisionReal.st
--- a/LimitedPrecisionReal.st	Tue Dec 16 13:19:17 2014 +0100
+++ b/LimitedPrecisionReal.st	Tue Dec 16 13:20:28 2014 +0100
@@ -497,6 +497,24 @@
     "
 !
 
+isIEEEFormat
+    "return true, if this machine represents floats in IEEE format.
+     Currently, no support is provided for non-ieee machines
+     to convert their floats into this (which is only relevant,
+     if such a machine wants to send floats as binary to some other
+     machine).
+     Machines with non-IEEE format are VAXed and IBM370-type systems
+     (among others). Today, most systems use IEEE format floats."
+
+%{
+#ifdef __s390__
+    RETURN(false);
+#endif
+%}.
+
+    ^ true "/ this may be a lie
+!
+
 numBitsInExponent
     "return the number of bits in the exponent"
 
@@ -666,6 +684,60 @@
     "
 ! !
 
+!LimitedPrecisionReal methodsFor:'bytes access'!
+
+digitBytes
+    "answer the float's digit bytes im IEEE format.
+     Use the native machine byte ordering."
+
+    |sz "{ Class:SmallInteger }"
+     bytes|
+
+    sz := self basicSize.
+    bytes := ByteArray new:sz.
+    1 to:sz do:[:i|
+        bytes at:i put:(self basicAt:i).
+    ].
+    ^ bytes
+
+    "
+        Float pi digitBytes
+        ShortFloat pi digitBytes
+    "
+!
+
+digitBytesMSB:msb
+    "answer the float's digit bytes im IEEE format.
+     If msb == true, use MSB byte order, otherwise LSB byte order."
+
+    |sz "{ Class:SmallInteger }"
+     bytes|
+
+    self class isIEEEFormat ifFalse:[self error:'unsupported operation'].
+
+    sz := self basicSize.
+    bytes := ByteArray new:sz.
+
+    (UninterpretedBytes isBigEndian == msb) ifTrue:[
+        1 to:sz do:[:i|
+            bytes at:i put:(self basicAt:i).
+        ].
+    ] ifFalse:[
+        "reverse the bytes"
+        1 to:sz do:[:i|
+            bytes at:sz-i+1 put:(self basicAt:i).
+        ].
+    ].
+    ^ bytes
+
+    "
+        Float pi digitBytesMSB:false
+        Float pi digitBytesMSB:true
+        ShortFloat pi digitBytesMSB:false
+        ShortFloat pi digitBytesMSB:true
+    "
+! !
+
 !LimitedPrecisionReal methodsFor:'coercing & converting'!
 
 asFloat
@@ -1280,11 +1352,11 @@
 !LimitedPrecisionReal class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/LimitedPrecisionReal.st,v 1.80 2014-12-11 14:10:25 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/LimitedPrecisionReal.st,v 1.81 2014-12-16 12:20:28 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/LimitedPrecisionReal.st,v 1.80 2014-12-11 14:10:25 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/LimitedPrecisionReal.st,v 1.81 2014-12-16 12:20:28 stefan Exp $'
 ! !