Fraction.st
changeset 21974 cf76d289d886
parent 21944 5f6d5497f82a
child 22099 cc9d508cf69c
--- a/Fraction.st	Mon Jul 03 17:02:28 2017 +0200
+++ b/Fraction.st	Mon Jul 03 17:35:53 2017 +0200
@@ -15,7 +15,7 @@
 
 Number subclass:#Fraction
 	instanceVariableNames:'numerator denominator'
-	classVariableNames:'FractionOne FractionZero PrintWholeNumbers Pi Pi_1000'
+	classVariableNames:'FractionOne FractionZero PrintWholeNumbers Pi Pi_1000 E'
 	poolDictionaries:''
 	category:'Magnitude-Numbers'
 !
@@ -247,6 +247,61 @@
 
 !Fraction class methodsFor:'constants'!
 
+e
+    "return an approximation of the constant e as Fraction.
+     The approx. returned here has an error smaller than representable by float instances
+     (roughly 26 valid digits)"
+
+    E isNil ifTrue:[
+        E := self
+                numerator:  271828182845904523536028747 
+                denominator:100000000000000000000000000
+    ].
+    ^ E
+    
+    "E := nil
+    
+     Fraction e
+     Fraction e asFloat - Float e
+     Fraction e asLongFloat - LongFloat e
+     Float e
+     FixedPoint e
+    "
+
+    "Created: / 03-07-2017 / 17:22:47 / cg"
+!
+
+e_approximation
+    "return an approximation of e as Fraction.
+     The approx. returned is good for 6 valid digits and has an error of less than -2.67-07.
+     The value might be useful to avoid floating point numbers in graphic rendering code,
+     where 6 digits of precision are usually good enough."
+
+    ^ self
+        numerator:67957
+        denominator:25000
+
+    "
+     Fraction e
+     Fraction e asFloat               
+     Fraction e_approximation asFloat 
+     
+     (Fraction e - Fraction e_approximation asFloat) abs
+     (Float e - Fraction e_approximation asFloat) abs
+
+     19/7 can be used as an approx with ~1% error:
+         Float e - (19/7) asFloat
+         
+     87/32 is another candidate:
+         Float e - (87/32) asFloat 
+
+     and 106/39:
+         Float e - (106/39) asFloat 
+    "
+
+    "Created: / 03-07-2017 / 17:21:38 / cg"
+!
+
 pi
     "return an approximation of the constant pi as Fraction.
      The approx. returned here has an error smaller than representable by float instances
@@ -311,9 +366,12 @@
      
      (Fraction pi - Fraction pi_approximation asFloat) abs
      (Float pi - Fraction pi_approximation asFloat) abs
+
+     22/7 can be used as an approx with 1% error:
+         Float pi - (22/7) asFloat
     "
 
-    "Modified (comment): / 03-07-2017 / 13:12:49 / cg"
+    "Modified (comment): / 03-07-2017 / 17:31:44 / cg"
 !
 
 unity