#DOCUMENTATION by cg
authorClaus Gittinger <cg@exept.de>
Thu, 15 Jun 2017 09:57:14 +0200
changeset 21811 0370229cfed8
parent 21810 b66021c7e3b1
child 21812 4d60b6626208
#DOCUMENTATION by cg class: LimitedPrecisionReal class comment/format in: #documentation #fromInteger:
LimitedPrecisionReal.st
--- a/LimitedPrecisionReal.st	Thu Jun 15 01:50:35 2017 +0200
+++ b/LimitedPrecisionReal.st	Thu Jun 15 09:57:14 2017 +0200
@@ -42,17 +42,76 @@
 "
     Abstract superclass for any-precision floating point numbers (i.e. IEEE floats and doubles).
 
-    Due to historic reasons, ST/X's Floats are what Doubles are in ST-80.
+    Short summary for beginners (find details in wikipedia):
+    ========================================================
+    
+    Floating point numbers are represented with a mantissa and an exponent, and the number's value is: 
+        mantissa * (2 raisedTo: exponent) 
+    with (1 > mantissa >= 0) and exponent adjusted as required for the mantissa to be in that range
+    (so called ''normalized'')
+    
+    therefore,
+        13 asFloat mantissa -> 0.8125
+        13 asFloat exponent ->  4  
+        0.8125 * (2 raisedTo:4) -> 13
+
+    and:    
+        104 asFloat mantissa -> 0.8125
+        104 asFloat exponent -> 7  
+        0.8125 * (2 raisedTo:7) -> 104
+
+    and:    
+        0.1 mantissa -> 0.8
+        0.1 exponent -> -3  
+        0.8 * (2 raisedTo:-3) -> 0.1
+
+    however:    
+        (1 / 3.0) mantissa -> 0.666666666666667
+        (1 / 3.0) exponent -> -1  
+        0.666666666666667 * (2 raisedTo:-3) -> 0.1
+
+
+    Danger in using Floats:
+    =======================
+
+    Beginners seem to forget (or never learn?) that flt. point numbers are always APPROXIMATIONs of some value.
+    You may never ever use them when exact results are neeed (i.e. when computing money !!)
+    Take a look at the FixedPoint class for that.
+    See also 'Float comparison' below.
+    
+    
+    The Float/Double confusion in ST/X:
+    ===================================
+    
+    Due to historic reasons, ST/X's Floats are what Doubles are in VisualWorks.
+    
     The reason is that in some Smalltalks, double floats are called Float, and no single float exists (VSE, V'Age),
     whereas in others, there are both Float and Double classes (VisualWorks).
-    In order to allow code from both families to be loaded into ST/X without a missing class error,
-    we decided to use IEEE doubles as the internal representation of Float and make Double an alias to it.
-    This should work for either family.
+    In order to allow code from both families to be loaded into ST/X without a missing class error, and without
+    loosing precision, we decided to use IEEE doubles as the internal representation of Float 
+    and make Double an alias to it.
+    This should work for either family (except for the unexpected additional precision in some cases).
+    
     If you really only want single precision floating point numbers, use ShortFloat instances.
     But be aware that there is usually no advantage (neither in memory usage, due to memory alignment restrictions,
     nor in speed), as these days, the CPUs are just as fast doing double precision operations.
-    (There is a difference when doing bulk operations, and you should consider using FloatArray for those).
+    (There might be a noticable difference when doing bulk operations, and you should consider using FloatArray for those).
+
+    
+    Hardware supported precisions
+    =============================
 
+    The only really portable sizes are IEEE-single and IEEE-double floats (i.e. ShortFloat and Float instances).
+    These are supported on all architectures.
+    Some do provide an extended precision floating pnt. number,
+    however, the downside is that CPU-architects did not agree on a common format and precision.
+    See the comments in the LongFloat class for more details.
+    We recommend using Float (i.e. IEEE doubles) unless absolutely required,
+    and care for machine dependencies in the code otherwise.
+    For higher precision needs, you may also try the new QDouble class, which gives you >200bits (60digits) of precision
+    on all machines (at a performance price, though).
+
+    
     Range and Precision of Storage Formats:
     =======================================
 
@@ -85,7 +144,8 @@
     Long- and LargeFloat are not supported as array containers.
     These formats are seldom used for bulk data.
 
-    QDoubles are special soft floats; slower in performance, but providing 4 times the prevision of regular doubles.
+    QDoubles are special soft floats; slower in performance, but providing 4 times the precision of regular doubles.
+
     
     Bulk Containers:
     ================
@@ -98,11 +158,12 @@
     Use them for matrices and large numeric vectors. They also provide some optimized bulk operation methods,
     such as adding, multiplying etc.
 
+    
     Comparing Floats:
     =================
     Due to rounding errors (usually on the last bit), you shalt not compare two floating point numbers
     using the #= operator. For example, the value 0.1 cannot be represented as a sum of powers of two fractions,
-    and will therefore be always an approximation with a half bit error in the last bit of the mantissa.
+    and will therefore always be an approximation with a half bit error in the last bit of the mantissa.
     Usually, the print functions take this into consideration and return a (faked) '0.1'.
     However, this half bit error may accumulate, for example, when multiplying that by 10, the error may get large
     enough to be no longer pushed under the rug by the print function, and you will get '0.9999999999999' from it.
@@ -135,6 +196,8 @@
 fromInteger:anInteger
     "return a float with anInteger's value.
      Since floats have a limited precision, you usually loose bits when doing this
+     with a large integer 
+     (i.e. when numDigits is above the flt. pnt number's precision)
      (see Float decimalPrecision, LongFloat decimalPrecision."
 
     |newFloat sign absVal completeAbsVal delta mask|
@@ -208,6 +271,8 @@
      'nearest even is lower'.
      self assert: 16r1FFFFFFFFFFFF0800 asDouble = 16r1FFFFFFFFFFFF0000 asDouble.
     "
+
+    "Modified (comment): / 15-06-2017 / 09:30:30 / cg"
 !
 
 fromLimitedPrecisionReal:anLPReal