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

Methods

*   **   +   -   /   <   <=   ==   >   >=   MINUS_ONE   ONE   TWO   ZERO   abs   acos   asin   atan   cos   cosh   display_val   eq   erf   err   exp   frac   ge   gt   init   le   log   log10   lt   ne   neg   new   sin   sinh   sqrt   tan   tanh   to_f   to_s   trunc   val  

Public Class methods

[Source]

    # File lib/rapfp/Real.rb, line 34
34: def initialize(val,err)
35:         @val = val
36:         @err = err
37: end

Public Instance methods

[Source]

    # 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

[Source]

     # 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

[Source]

    # File lib/rapfp/Real.rb, line 81
81: def +(other)
82:         Real.new((@val+other.val),(@err+other.err))
83: end

[Source]

    # File lib/rapfp/Real.rb, line 85
85: def -(other)
86:         Real.new((@val-other.val),(@err+other.err))
87: end

[Source]

    # 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

[Source]

     # File lib/rapfp/Real.rb, line 161
161: def <(other)
162: 
163:         (@val + @err) < (other.val - other.err)
164: 
165: end

[Source]

     # File lib/rapfp/Real.rb, line 173
173: def <=(other)
174: 
175:         (@val + @err) <= (other.val - other.err)
176: 
177: end

[Source]

     # File lib/rapfp/Real.rb, line 185
185: def ==(other)
186: 
187:         (@val >= other.val) && (@val <= other.val)
188: 
189: end

[Source]

     # File lib/rapfp/Real.rb, line 167
167: def >(other)
168: 
169:         (@val - @err) > (other.val + other.err)
170: 
171: end

[Source]

     # File lib/rapfp/Real.rb, line 179
179: def >=(other)
180: 
181:         (@val - @err) >= (other.val + other.err)
182: 
183: end

[Source]

    # File lib/rapfp/Real.rb, line 49
49: def MINUS_ONE
50:         Real(-1.0,Float::EPSILON)
51: end

[Source]

    # File lib/rapfp/Real.rb, line 45
45: def ONE
46:         Real(1.0,Float::EPSILON)
47: end

[Source]

    # File lib/rapfp/Real.rb, line 53
53: def TWO
54:         Real(2.0,Float::EPSILON)
55: end

[Source]

    # File lib/rapfp/Real.rb, line 39
39: def ZERO
40: 
41:         Real(0.0,Float::EPSILON)
42: 
43: end

[Source]

     # File lib/rapfp/Real.rb, line 277
277: def abs
278: 
279:   Real.new(@val.abs,err)
280: 
281: end

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

[Source]

     # File lib/rapfp/Real.rb, line 223
223: def display_val(str)
224:   puts "${str} = ${self.to_s}"
225: end

[Source]

     # File lib/rapfp/Real.rb, line 149
149: def eq(other)
150: 
151:         @val == other.val
152: 
153: end

end

[Source]

     # 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

[Source]

    # File lib/rapfp/Real.rb, line 77
77: def err
78:         @err
79: end

[Source]

     # 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

[Source]

     # File lib/rapfp/Real.rb, line 306
306: def frac
307: 
308:         self - self.trunc
309: 
310: end

[Source]

     # File lib/rapfp/Real.rb, line 143
143: def ge(other)
144: 
145:         @val >= other.val
146: 
147: end

[Source]

     # File lib/rapfp/Real.rb, line 131
131: def gt(other)
132: 
133:         @val > other.val
134: 
135: end

[Source]

    # 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

[Source]

     # File lib/rapfp/Real.rb, line 137
137: def le(other)
138: 
139:         @val <= other.val
140: 
141: end

[Source]

     # 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

[Source]

     # 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

[Source]

     # File lib/rapfp/Real.rb, line 125
125: def lt(other)
126: 
127:         @val < other.val
128: 
129: end

[Source]

     # File lib/rapfp/Real.rb, line 155
155: def ne(other)
156: 
157:         @val != other.val
158: 
159: end

[Source]

     # File lib/rapfp/Real.rb, line 100
100: def neg
101:   Real.new(-val,err)
102: end

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

[Source]

     # File lib/rapfp/Real.rb, line 205
205: def tan
206: 
207:         self.sin/self.cos
208: 
209: end

[Source]

     # File lib/rapfp/Real.rb, line 261
261: def tanh
262: 
263:         self.sinh/self.cosh
264: 
265: end

[Source]

    # File lib/rapfp/Real.rb, line 69
69: def to_f
70:   return @val
71: end

[Source]

    # File lib/rapfp/Real.rb, line 65
65: def to_s
66:          return(@val.to_s + "+/-" + @err.to_s)
67: end

[Source]

     # File lib/rapfp/Real.rb, line 300
300: def trunc
301: 
302:         Real.new((@val.floor),@err)
303: 
304: end

[Source]

    # File lib/rapfp/Real.rb, line 73
73: def val
74:         @val
75: end

[Validate]