| Class | Real |
| In: |
lib/rapfp/Real.rb
|
| Parent: | Object |
###
File: Real.rb
Subject: Class for built in floating point tracking error.
Author: Dennis J. Darland
Date: March 30, 2007
Copyright (C) 2007 Dennis J. Darland
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# File lib/rapfp/Real.rb, line 89
89: def *(other)
90: Real.new((@val * other.val), (@err * other.val.abs + other.err * @val.abs) )
91:
92: end
# File lib/rapfp/Real.rb, line 104
104: def **(other)
105: a = self.clone
106: unless other.kind_of?(Real)
107: b = $RConst.one
108: if other > 0 then
109: other.times do
110: b *= a
111: end
112: elsif other < 0
113: (-other).times do
114: b *= a
115: end
116: b = $RConst.one/b
117: end
118: else
119: b = Real.new((@val ** other.val),(other.val * @val ** (other.val - 1.0) * @err))
120: end
121: return b
122:
123: end
# File lib/rapfp/Real.rb, line 81
81: def +(other)
82: Real.new((@val+other.val),(@err+other.err))
83: end
# File lib/rapfp/Real.rb, line 85
85: def -(other)
86: Real.new((@val-other.val),(@err+other.err))
87: end
# File lib/rapfp/Real.rb, line 94
94: def /(other)
95:
96: Real.new((@val / other.val),(@err / other.val.abs + @val.abs * other.err / (other.val * other.val)))
97:
98: end
# File lib/rapfp/Real.rb, line 161
161: def <(other)
162:
163: (@val + @err) < (other.val - other.err)
164:
165: end
# File lib/rapfp/Real.rb, line 173
173: def <=(other)
174:
175: (@val + @err) <= (other.val - other.err)
176:
177: end
# File lib/rapfp/Real.rb, line 185
185: def ==(other)
186:
187: (@val >= other.val) && (@val <= other.val)
188:
189: end
# File lib/rapfp/Real.rb, line 167
167: def >(other)
168:
169: (@val - @err) > (other.val + other.err)
170:
171: end
# File lib/rapfp/Real.rb, line 179
179: def >=(other)
180:
181: (@val - @err) >= (other.val + other.err)
182:
183: end
# File lib/rapfp/Real.rb, line 283
283: def acos
284: if self.abs > $RConst.one then
285: puts "acos out of range"
286: self.display_val("self")
287: return self
288: end
289:
290: Real.new(Math.acos(@val),(@err/Math.sqrt(1.0-@val*@val)).abs)
291:
292: end
# File lib/rapfp/Real.rb, line 267
267: def asin
268: if self.abs > $RConst.one then
269: puts "asin out of range"
270: self.display_val("self")
271: return self
272: end
273: Real.new(Math.asin(@val),(@err/Math.sqrt(1.0-@val*@val)).abs)
274:
275: end
# File lib/rapfp/Real.rb, line 294
294: def atan
295:
296: Real.new(Math.atan(@val),(@err/(1.0+@val*@val)).abs)
297:
298: end
# File lib/rapfp/Real.rb, line 199
199: def cos
200:
201: Real.new(Math.cos(@val),(Math.sin(@val)*@err).abs)
202:
203: end
# File lib/rapfp/Real.rb, line 255
255: def cosh
256:
257: Real.new(Math.cosh(@val),(Math.sinh(@val)*@err).abs)
258:
259: end
# File lib/rapfp/Real.rb, line 223
223: def display_val(str)
224: puts "${str} = ${self.to_s}"
225: end
end
# File lib/rapfp/Real.rb, line 319
319: def erf
320: Real.new(Math.erf(@val),(2.0/Math.sqrt(Math::PI)*(Math.exp(@val*@val*(-1.0)))).abs)
321: end
# File lib/rapfp/Real.rb, line 211
211: def exp
212:
213: Real.new(Math.exp(@val),(Math.exp(@val)*@err).abs)
214:
215: end
# File lib/rapfp/Real.rb, line 59
59: def init
60:
61: @@ZERO = Real(0.0,0.1e-15)
62: @@ONE= Real(1.0,0.1e-15)
63: end
# File lib/rapfp/Real.rb, line 227
227: def log
228: if self <= $RConst.zero then
229: puts "log out of range"
230: self.display_val("self")
231: return self
232: end
233:
234: Real.new(Math.log(@val),(@err/@val).abs)
235:
236: end
# File lib/rapfp/Real.rb, line 238
238: def log10
239:
240: if self <= $RConst.zero then
241: puts "log10 out of range"
242: self.display_val("self")
243: return self
244: end
245: Real.new(Math.log10(@val),(@err/Math.log(@val)/@val).abs)
246:
247: end
# File lib/rapfp/Real.rb, line 193
193: def sin
194:
195: Real.new(Math.sin(@val),(Math.cos(@val)*@err).abs)
196:
197: end
# File lib/rapfp/Real.rb, line 249
249: def sinh
250:
251: Real.new(Math.sinh(@val),(Math.cosh(@val)*@err).abs)
252:
253: end
# File lib/rapfp/Real.rb, line 217
217: def sqrt
218:
219: Real.new(Math.sqrt(@val),(0.5/Math.sqrt(@val)*@err).abs)
220:
221: end