Class: Float
- Inherits:
-
Object
- Object
- Float
- Defined in:
- lib/float_ext.rb
Overview
Usage:
require 'float_ext.rb'
x = 1.37; y = 0.123
phi = x.arg(y)
r = x.hypot(y)
dx = r * phi.cos - x
dy = r * phi.sin - y
Defining all functons of module Math as methods of class Float. The meaning of functions frexp and ldexp has been changed from referring to exponent 2 to exponent 10. Notice that code that worked without a statement
require 'float_ext.rb'
will work in an identical manner with this statement added. Only if this is true, extending the functionaliy of standard types is considered acceptable. A few functions, not to be found in module Math have been added. These are methods which are defined in R and thus should be available in Float in order to make R and Float strictly replacable in all code that loads float_ext.rb and rnum.rb. These new functions are:
inv, pseudo_inv, conj, cot, coth, acot, acoth, arg,
clone, dis, integer?, real?, complex?
Notice also, that for R.prec = 0, the real-number generating functions
R.ran, R.tob, R.c, R.i, R.pi, R.e
R.c0, R.c1, ..., R.c10, R.i2, ... R.i10
that normally return R-numbers are forced to return Floats so that it is simple to write programs in such a manner that all real numbers switch their type from Float to R and vice versa. The class R is coded in a manner that it makes no use of the present extension of Float, although this would have allowed some code reduction.
Instance Method Summary collapse
- #acos ⇒ Object
- #acosh ⇒ Object
- #acot ⇒ Object
- #acoth ⇒ Object
-
#arg(y) ⇒ Object
The name ‘arg’ stands for ‘agument’, which is the name for the polar angle of a complex number preferred in the mahematical literature.
- #asin ⇒ Object
- #asinh ⇒ Object
- #atan ⇒ Object
- #atan2(x) ⇒ Object
- #atanh ⇒ Object
-
#clone ⇒ Object
One should have this!.
-
#complex? ⇒ Boolean
Not complex, since no second dimesion (filling a plane) is provided.
-
#conj ⇒ Object
(Complex) conjugation, no effect on real numbers.
- #cos ⇒ Object
- #cosh ⇒ Object
-
#cot ⇒ Object
All functions having ‘cot’ in their name deal with the trigonometric or hyperbolic cotangent.
- #coth ⇒ Object
-
#dis(x) ⇒ Object
For all Float x, y we have 0 <= x.dis(y) <= 1 and x.dis(x) = 0 It is a kind of relative distance which should be in the order of magnitude of the smalles positive representable number if x and y are known to differ only by numerical noise.
-
#erf ⇒ Object
error function.
-
#erfc ⇒ Object
complemetary error function.
- #exp ⇒ Object
-
#frexp ⇒ Object
Warning! x.frexp differs from Math.frexp(x).
-
#hypot(y) ⇒ Object
x.hypot(y) is an efficient and accurate representation of the square root of x*x + y*y.
-
#integer? ⇒ Boolean
Since Float is not Fixnum or Bignum we return ‘false’.
-
#inv ⇒ Object
It is convenient to have inversion (the multiplicative analogon of the unary - operation) as a member function.
-
#ldexp(n) ⇒ Object
Warning! x.ldexp differs from Math.ldexp(x).
-
#log ⇒ Object
method log (unlike function Math.log) is defined in a mathematically reasonable manner also for negative arguments.
-
#log10 ⇒ Object
method log10 (unlike function Math.log10) is defined in a mathematically reasonable manner also for negative arguments.
-
#prn(name) ⇒ Object
Print.
-
#pseudo_inv ⇒ Object
pseudo inverse, which always exists.
-
#real? ⇒ Boolean
Although there may be technical variants in representing real numbers, these all should answer this question with ‘yes’ since they all model mathematical real numbers.
-
#round(*arg) ⇒ Object
Returns a real number, the significand of which has not more than n digits.
- #sin ⇒ Object
- #sinh ⇒ Object
- #sqrt ⇒ Object
- #tan ⇒ Object
- #tanh ⇒ Object
-
#to_0 ⇒ Object
Providing the neutral element of addition via a method.
-
#to_1 ⇒ Object
Providing the neutral element of multiplication via a method.
Instance Method Details
#acos ⇒ Object
96 |
# File 'lib/float_ext.rb', line 96 def acos; Math.acos(self); end |
#acosh ⇒ Object
97 |
# File 'lib/float_ext.rb', line 97 def acosh; Math.acosh(self); end |
#acot ⇒ Object
122 |
# File 'lib/float_ext.rb', line 122 def acot; Math.atan(1./self); end |
#acoth ⇒ Object
123 |
# File 'lib/float_ext.rb', line 123 def acoth; 0.5 * Math.log( ((self + 1.0)/(self - 1.0)).abs ) end |
#arg(y) ⇒ Object
The name ‘arg’ stands for ‘agument’, which is the name for the polar angle of a complex number preferred in the mahematical literature. So for a complex number z = u + iv we have
argument(z) = u.arg(v)
Notice also the representation of the absolute value of z:
|z| = u.hypot(v)
The functions hypot and arg (or atan2) thus prepare the introduction of complex numbers.
112 |
# File 'lib/float_ext.rb', line 112 def arg(y); Math.atan2(y,self); end |
#asin ⇒ Object
91 |
# File 'lib/float_ext.rb', line 91 def asin; Math.asin(self); end |
#asinh ⇒ Object
92 |
# File 'lib/float_ext.rb', line 92 def asinh; Math.asinh(self); end |
#atan ⇒ Object
101 |
# File 'lib/float_ext.rb', line 101 def atan; Math.atan(self); end |
#atan2(x) ⇒ Object
102 |
# File 'lib/float_ext.rb', line 102 def atan2(x); Math.atan2(self,x); end |
#atanh ⇒ Object
114 |
# File 'lib/float_ext.rb', line 114 def atanh; Math.atanh(self); end |
#clone ⇒ Object
One should have this!
159 160 161 |
# File 'lib/float_ext.rb', line 159 def clone res = 0.0; res += self; res end |
#complex? ⇒ Boolean
Not complex, since no second dimesion (filling a plane) is provided.
190 |
# File 'lib/float_ext.rb', line 190 def complex?; false; end |
#conj ⇒ Object
(Complex) conjugation, no effect on real numbers. Supports the unified treatment of real and complex numbers.
78 |
# File 'lib/float_ext.rb', line 78 def conj; self; end |
#cos ⇒ Object
94 |
# File 'lib/float_ext.rb', line 94 def cos; Math.cos(self); end |
#cosh ⇒ Object
95 |
# File 'lib/float_ext.rb', line 95 def cosh; Math.cosh(self); end |
#cot ⇒ Object
All functions having ‘cot’ in their name deal with the trigonometric or hyperbolic cotangent. These functions are so directly related to the corresponding ‘tan’-functions that they are not included in the Math and BigMath modules. It is sometimes useful to have them, though.
120 |
# File 'lib/float_ext.rb', line 120 def cot; Math.cos(self)/Math.sin(self); end |
#coth ⇒ Object
121 |
# File 'lib/float_ext.rb', line 121 def coth; Math.cosh(self)/Math.sinh(self); end |
#dis(x) ⇒ Object
For all Float x, y we have 0 <= x.dis(y) <= 1 and x.dis(x) = 0 It is a kind of relative distance which should be in the order of magnitude of the smalles positive representable number if x and y are known to differ only by numerical noise.
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/float_ext.rb', line 167 def dis(x) xf = x.to_f a = abs b = xf.abs d = (self - xf).abs s = a + b return 0.0 if s.zero? d1 = d/s d < d1 ? d : d1 end |
#erf ⇒ Object
error function
126 |
# File 'lib/float_ext.rb', line 126 def erf; Math.erf(self); end |
#erfc ⇒ Object
complemetary error function
129 |
# File 'lib/float_ext.rb', line 129 def erfc; Math.erfc(self) ; end |
#exp ⇒ Object
80 |
# File 'lib/float_ext.rb', line 80 def exp; Math.exp(self); end |
#frexp ⇒ Object
Warning! x.frexp differs from Math.frexp(x). We need exponent 10 not 2, so we don’t use:
def frexp; Math.frexp(self);end
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/float_ext.rb', line 135 def frexp # puts "arg of frex="+to_s if zero? [ 0.0, 0] elsif self > 0.0 y = Math.log10(self) yf = y.floor yfrac = y - yf [ 10.0 ** yfrac, yf.to_i] else y = Math.log10(-self) yf = y.floor yfrac = y - yf [ - 10.0 ** yfrac, yf.to_i] end end |
#hypot(y) ⇒ Object
x.hypot(y) is an efficient and accurate representation of the square root of x*x + y*y.
67 |
# File 'lib/float_ext.rb', line 67 def hypot(y); Math.hypot(self,y); end |
#integer? ⇒ Boolean
Since Float is not Fixnum or Bignum we return ‘false’. In scientific computation there may be the need to use various types of ‘real number types’ but there should be always a clear-cut distinction between integer types and real types.
182 |
# File 'lib/float_ext.rb', line 182 def integer?; false; end |
#inv ⇒ Object
It is convenient to have inversion (the multiplicative analogon of the unary - operation) as a member function.
71 |
# File 'lib/float_ext.rb', line 71 def inv; 1.0/self; end |
#ldexp(n) ⇒ Object
Warning! x.ldexp differs from Math.ldexp(x). We need exponent 10 not 2, so we don’t use:
def ldexp(n); Math.ldexp(self,n);end
156 |
# File 'lib/float_ext.rb', line 156 def ldexp(n); self * (10.0 ** n.to_i); end |
#log ⇒ Object
method log (unlike function Math.log) is defined in a mathematically reasonable manner also for negative arguments.
83 |
# File 'lib/float_ext.rb', line 83 def log; Math.log(abs); end |
#log10 ⇒ Object
method log10 (unlike function Math.log10) is defined in a mathematically reasonable manner also for negative arguments.
87 |
# File 'lib/float_ext.rb', line 87 def log10; Math.log10(abs); end |
#prn(name) ⇒ Object
Print. Output to console, together with a name which is given by the argument.
200 201 202 |
# File 'lib/float_ext.rb', line 200 def prn(name) puts " #{name} = " + to_s end |
#pseudo_inv ⇒ Object
pseudo inverse, which always exists
74 |
# File 'lib/float_ext.rb', line 74 def pseudo_inv; zero? ? 0.0 : 1.0/self; end |
#real? ⇒ Boolean
Although there may be technical variants in representing real numbers, these all should answer this question with ‘yes’ since they all model mathematical real numbers.
187 |
# File 'lib/float_ext.rb', line 187 def real?; true; end |
#round(*arg) ⇒ Object
Returns a real number, the significand of which has not more than n digits. Notice that there is also a function round which takes no argument and which returns an integer number. This function replaces function Float#round.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/float_ext.rb', line 208 def round(*arg) n = arg.size case n when 0 (self + 0.5).floor # output is integer when 1 m = arg[0].to_i x = frexp y = x[0].ldexp(m) (y + 0.5).floor.to_f.ldexp(x[1] - m) else fail "needs 0 or 1 arguments" end end |
#sin ⇒ Object
89 |
# File 'lib/float_ext.rb', line 89 def sin; Math.sin(self); end |
#sinh ⇒ Object
90 |
# File 'lib/float_ext.rb', line 90 def sinh; Math.sinh(self); end |
#sqrt ⇒ Object
63 |
# File 'lib/float_ext.rb', line 63 def sqrt; Math.sqrt(self); end |
#tan ⇒ Object
99 |
# File 'lib/float_ext.rb', line 99 def tan; Math.tan(self); end |
#tanh ⇒ Object
100 |
# File 'lib/float_ext.rb', line 100 def tanh; Math.tanh(self); end |
#to_0 ⇒ Object
Providing the neutral element of addition via a method.
193 |
# File 'lib/float_ext.rb', line 193 def to_0; 0.0; end |
#to_1 ⇒ Object
Providing the neutral element of multiplication via a method.
196 |
# File 'lib/float_ext.rb', line 196 def to_1; 1.0; end |