ShortFloat.st
changeset 25016 cbd04064b803
parent 25003 18856dfc86e4
child 25020 24168eb5923a
--- a/ShortFloat.st	Sun Dec 01 05:27:43 2019 +0100
+++ b/ShortFloat.st	Sun Dec 01 05:28:03 2019 +0100
@@ -488,7 +488,7 @@
 !ShortFloat class methodsFor:'coercing & converting'!
 
 coerce:aNumber
-    "convert the argument aNumber into an instance of the receiver's class and return it."
+    "convert the argument aNumber into an instance of the receiver (class) and return it."
 
     ^ aNumber asShortFloat.
 ! !
@@ -527,38 +527,6 @@
     ^ 127
 !
 
-emax
-    "Answer the maximum exponent for this representation."
-
-    ^127
-!
-
-emin
-    "Answer the minimum exponent for this representation."
-
-    ^-126
-!
-
-fmin
-%{
-    RETURN(__MKFLOAT(FLT_MIN));
-%}.
-
-    "
-     ShortFloat fmin
-    "
-!
-
-fmax
-%{
-    RETURN(__MKFLOAT(FLT_MAX));
-%}.
-
-    "
-     ShortFloat fmax
-    "
-!
-
 infinity
     "return a shortFloat which represents positive infinity (for my instances)"
 
@@ -665,6 +633,44 @@
     "Created: / 17-06-2017 / 02:59:31 / cg"
 !
 
+emax
+    "The largest exponent value allowed by instances of this class."
+
+%{  /* NOCONTEXT */
+#include <float.h>
+#if defined(FLT_MAX_EXP)
+    RETURN(__MKSMALLINT(FLT_MAX_EXP));
+#endif
+%}.
+    ^ super emax
+
+    "
+     1.0 asShortFloat fmax  -> 1.189731495357231765E+4932
+     1.0 asShortFloat fmin  -> 3.362103143112093506E-4932
+     1.0 asShortFloat emin  -> -16381
+     1.0 asShortFloat emax  -> 128
+    "
+!
+
+emin
+    "The largest exponent value allowed by instances of this class."
+
+%{  /* NOCONTEXT */
+#include <float.h>
+#if defined(FLT_MIN_EXP)
+    RETURN(__MKSMALLINT(FLT_MIN_EXP));
+#endif
+%}.
+    ^ super emin
+
+    "
+     1.0 asShortFloat fmax  -> 1.189731495357231765E+4932
+     1.0 asShortFloat fmin  -> 3.362103143112093506E-4932
+     1.0 asShortFloat emin  -> -125
+     1.0 asShortFloat emax  -> 128
+    "
+!
+
 epsilon
     "return the maximum relative spacing of instances of mySelf
      (i.e. the value-delta of the least significant bit)"
@@ -692,6 +698,44 @@
     "Modified (comment): / 10-06-2019 / 21:27:52 / Claus Gittinger"
 !
 
+fmax
+    "The largest exponent value allowed by instances of this class."
+
+%{  /* NOCONTEXT */
+#include <float.h>
+#if defined(FLT_MAX)
+    RETURN(__MKSFLOAT(FLT_MAX));
+#endif
+%}.
+    ^ super fmax
+
+    "
+     1.0 asShortFloat fmax  -> 3.402823e+38
+     1.0 asShortFloat fmin  -> 1.175494e-38
+     1.0 asShortFloat emin  -> -125
+     1.0 asShortFloat emax  -> 128
+    "
+!
+
+fmin
+    "The largest exponent value allowed by instances of this class."
+
+%{  /* NOCONTEXT */
+#include <float.h>
+#if defined(FLT_MIN)
+    RETURN(__MKSFLOAT(FLT_MIN));
+#endif
+%}.
+    ^ super fmin
+
+    "
+     1.0 asShortFloat fmax  -> 3.40282346638529E+38
+     1.0 asShortFloat fmin  -> 1.175494e-38
+     1.0 asShortFloat emin  -> -125
+     1.0 asShortFloat emax  -> 128
+    "
+!
+
 isBuiltInClass
     "return true if this class is known by the run-time-system.
      Here, true is returned for myself, false for subclasses."
@@ -718,11 +762,24 @@
 
 numBitsInMantissa
     "answer the number of bits in the mantissa.
-     This is an IEEE float, where 23 bits (the hidden one is not counted here) are available:
-	seeeeeee emmmmmmm mmmmmmmm mmmmmmmm
+     This is an IEEE float, where 23 bits are available 
+     (the hidden bit is not counted here):
+        seeeeeee emmmmmmm mmmmmmmm mmmmmmmm
     "
 
     ^ 23
+
+    "
+     1.0 asShortFloat numBitsInExponent 8
+     1.0 asShortFloat numBitsInMantissa 23
+     1.0 asShortFloat precision         24
+     1.0 asShortFloat decimalPrecision  7
+     1.0 asShortFloat eBias             127
+     1.0 asShortFloat emin              -126
+     1.0 asShortFloat emax              127
+     1.0 asShortFloat fmin              1.17549435082229E-38
+     1.0 asShortFloat fmax              3.40282346638529E+38
+    "
 !
 
 precision
@@ -1723,31 +1780,31 @@
      WARNING: this goes directly to the C-printf function and may therefore me inherently unsafe.
      Please use the printf: method, which is safe as it is completely implemented in Smalltalk."
 
-%{  /* STACK: 8000 */
+%{  /* STACK: 16000 */
 #ifndef __SCHTEAM__
-    char buffer[256];
+    char buffer[1024];
     OBJ s;
     int len;
 
     if (__isStringLike(formatString)) {
-	/*
-	 * actually only needed on sparc: since thisContext is
-	 * in a global register, which gets destroyed by printf,
-	 * manually save it here - very stupid ...
-	 */
-	__BEGIN_PROTECT_REGISTERS__
-
-	len = snprintf(buffer, sizeof(buffer), __stringVal(formatString), __shortFloatVal(self));
-
-	__END_PROTECT_REGISTERS__
-
-	if (len < 0) goto fail;
-	if (len >= sizeof(buffer)) goto fail;
-
-	s = __MKSTRING_L(buffer, len);
-	if (s != nil) {
-	    RETURN (s);
-	}
+        /*
+         * actually only needed on sparc: since thisContext is
+         * in a global register, which gets destroyed by printf,
+         * manually save it here - very stupid ...
+         */
+        __BEGIN_PROTECT_REGISTERS__
+
+        len = snprintf(buffer, sizeof(buffer), __stringVal(formatString), __shortFloatVal(self));
+
+        __END_PROTECT_REGISTERS__
+
+        if (len < 0) goto fail;
+        if (len >= sizeof(buffer)) goto fail;
+
+        s = __MKSTRING_L(buffer, len);
+        if (s != nil) {
+            RETURN (s);
+        }
     }
 fail: ;
 #endif /* not __SCHTEAM__ */
@@ -2030,11 +2087,11 @@
 #endif
     {
 #if defined(__i386__) && defined(__GNUC__)
-	float frac = frexpf(myVal, &exp);
-	RETURN (__MKSFLOAT(frac));
+        float frac = frexpf(myVal, &exp);
+        RETURN (__MKSFLOAT(frac));
 #else
-	double frac = frexp( (double)(myVal), &exp);
-	RETURN (__MKFLOAT(frac));
+        double frac = frexp( (double)(myVal), &exp);
+        RETURN (__MKFLOAT(frac));
 #endif
     }
 %}.
@@ -2053,6 +2110,11 @@
 
      0.00000011111 asShortFloat exponent
      0.00000011111 asShortFloat mantissa
+
+     self assert:(1.0 asShortFloat mantissa * (2 raisedTo:1.0 asShortFloat exponent)) = 1.0 asShortFloat.
+     self assert:(100.0 asShortFloat mantissa * (2 raisedTo:100.0 asShortFloat exponent)) = 100.0 asShortFloat.
+     self assert:(10e15 asShortFloat mantissa * (2 raisedTo:10e15 asShortFloat exponent)) = 10e15 asShortFloat.
+     self assert:(10e-15 asShortFloat mantissa * (2 raisedTo:10e-15 asShortFloat exponent)) = 10e-15 asShortFloat.
     "
 
     "Modified: / 20-06-2017 / 11:41:11 / cg"