Float.st
changeset 25008 89196028f729
parent 25002 34d9021dd872
child 25021 33a9aa9a5efe
--- a/Float.st	Sun Dec 01 05:17:46 2019 +0100
+++ b/Float.st	Sun Dec 01 05:18:11 2019 +0100
@@ -917,15 +917,41 @@
 !
 
 emax
-    "Answer the maximum exponent for this representation."
-
-    ^1023
+    "The largest exponent value allowed by instances of this class."
+
+%{  /* NOCONTEXT */
+#include <float.h>
+#if defined(DBL_MAX_EXP)
+    RETURN(__MKSMALLINT(DBL_MAX_EXP));
+#endif
+%}.
+    ^ super emax
+
+    "
+     1.0 fmax  -> 1.189731495357231765E+4932
+     1.0 fmin  -> 3.362103143112093506E-4932
+     1.0 emin  -> -16381
+     1.0 emax  -> 1024
+    "
 !
 
 emin
-    "Answer the minimum exponent for this representation."
-
-    ^ -1022
+    "The smallest exponent value allowed by instances of this class."
+
+%{  /* NOCONTEXT */
+#include <float.h>
+#if defined(DBL_MIN_EXP)
+    RETURN(__MKSMALLINT(DBL_MIN_EXP));
+#endif
+%}.
+    ^ super emin
+
+    "
+     1.0 fmax  -> 1.189731495357231765E+4932
+     1.0 fmin  -> 3.362103143112093506E-4932
+     1.0 emin  -> -1021
+     1.0 emax  -> 1024
+    "
 !
 
 epsilon
@@ -957,6 +983,16 @@
     "Modified (comment): / 10-06-2019 / 21:27:46 / Claus Gittinger"
 !
 
+fmax
+%{
+    RETURN(__MKFLOAT(DBL_MAX));
+%}.
+
+    "
+     Float fmax
+    "
+!
+
 fmin
 %{
     RETURN(__MKFLOAT(DBL_MIN));
@@ -967,16 +1003,6 @@
     "
 !
 
-fmax
-%{
-    RETURN(__MKFLOAT(DBL_MAX));
-%}.
-
-    "
-     Float fmax
-    "
-!
-
 isBuiltInClass
     "return true if this class is known by the run-time-system.
      Here, true is returned for myself, false for subclasses."
@@ -1004,10 +1030,23 @@
 numBitsInMantissa
     "answer the number of bits in the mantissa.
      This is an IEEE double, where 52 bits (the hidden one is not counted here) are available:
-	seeeeeee eeeemmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm
+        seeeeeee eeeemmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm
     "
 
      ^ 52
+
+    "
+     1.0 numBitsInExponent 11
+     1.0 numBitsInMantissa 52
+     1.0 precision         53
+     1.0 decimalPrecision  16
+     1.0 eBias             1023
+     1.0 emin              -1022
+     1.0 emax              1023
+     1.0 fmin              2.2250738585072E-308
+     1.0 fmax              1.79769313486232E+308
+    "
+
 !
 
 precision
@@ -2372,31 +2411,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), __floatVal(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), __floatVal(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__ */
@@ -2727,8 +2766,8 @@
     if (! (isnan(myVal) || isinf(myVal)))
 #endif
     {
-	frac = frexp(myVal, &exp);
-	RETURN (__MKFLOAT(frac));
+        frac = frexp(myVal, &exp);
+        RETURN (__MKFLOAT(frac));
     }
 %}.
     ^ super mantissa
@@ -2744,6 +2783,11 @@
      0.00000011111 mantissa
 
      1e1000 mantissa
+
+     self assert:(1.0 mantissa * (2 raisedTo:1.0 exponent)) = 1.0.
+     self assert:(100.0 mantissa * (2 raisedTo:100.0 exponent)) = 100.0.
+     self assert:(10e15 mantissa * (2 raisedTo:10e15 exponent)) = 10e15.
+     self assert:(10e-15 mantissa * (2 raisedTo:10e-15 exponent)) = 10e-15.
     "
 
     "Modified: / 20-06-2017 / 11:37:13 / cg"