Parent

Class Index [+]

Quicksearch

Flt::Tolerance

The Tolerance class is a base class for all tolerances.

Particular tolerance kinds (defined by a type of tolerance and the way to specify its value) are implemented in separate classes derived from Tolerance.

Derived classes must implement at least one of the methods relative_to() or relative_to_many() and may also redefine cast_value() and descr_value()

Public Class Methods

big_epsilon(num_class, mult=1) click to toggle source

Define a tolerance magnitude in relation to the ‘big epsilon’ of the floating-point type and context. A multiplier may be specified to scale the big epsilon.

This is a tolerance that makes multiplication associative when used with FloatingTolerance.

     # File lib/flt/tolerance.rb, line 249
249:       def big_epsilon(num_class, mult=1)
250:         context = num_class.context
251:         e0 = context.epsilon
252:         # we could compute with round-up instead of using next_plus, but we can't do that with Float
253:         den = (context.Num(1)-e0/2)
254:         big_eps = context.next_plus(e0*2/(den*den))
255:         big_eps*mult
256:       end
bits(n, rounded=true) click to toggle source

Define a tolerance magnitude as a number of binary digits. If rounded is true it is assumed that results are rounded to n digits; otherwise truncation or directed rounding may occur and the tolerance will be larger.

     # File lib/flt/tolerance.rb, line 235
235:       def bits(n, rounded=true)
236:         digits 10, n, rounded
237:       end
decimals(n, rounded=true) click to toggle source

Define a tolerance magnitude as a number of decimal digits. If rounded is true it is assumed that results are rounded to n digits; otherwise truncation or directed rounding may occur and the tolerance will be larger.

     # File lib/flt/tolerance.rb, line 228
228:       def decimals(n, rounded=true)
229:         digits 10, n, rounded
230:       end
digits(base, n, rounded=true) click to toggle source

Define a tolerance magnitude as a number of digits of the given base. If rounded is true it is assumed that results are rounded to n digits; otherwise truncation or directed rounding may occur and the tolerance will be larger.

     # File lib/flt/tolerance.rb, line 219
219:       def digits(base, n, rounded=true)
220:         v = base**(-n)
221:         v /= 2 if rounded
222:         v
223:       end
epsilon(num_class, mult=1) click to toggle source

Define a tolerance magnitude in relation to the ‘epsilon’ of the floating-point type and context. A multiplier may be specified to scale the epsilon.

     # File lib/flt/tolerance.rb, line 241
241:       def epsilon(num_class, mult=1)
242:         num_class.context.epsilon*mult
243:       end
new(value) click to toggle source
     # File lib/flt/tolerance.rb, line 103
103:     def initialize(value)
104:       @value = value
105:     end

Public Instance Methods

[](x) click to toggle source

Shorthand for value()

     # File lib/flt/tolerance.rb, line 117
117:     def [](x)
118:       value(x)
119:     end
cast_value(num_class) click to toggle source

Returns the tolerance reference value for a numeric class; in derived classes this can be redefined to allow for values which change in value or precision depending on the numeric class or context.

     # File lib/flt/tolerance.rb, line 204
204:     def cast_value(num_class)
205:       num_class.context.Num(@value)
206:     end
descr_value() click to toggle source

Description of the reference value (can be specialized in derived classes)

     # File lib/flt/tolerance.rb, line 209
209:     def descr_value
210:       @value.to_s
211:     end
eq?(x, y) click to toggle source

equals: x == y within tolerance (relaxed)

     # File lib/flt/tolerance.rb, line 158
158:     def eq?(x, y)
159:       (x-y).abs <= relative_to_many(:max, x, y)
160:     end
equal_to?(x, y) click to toggle source

x == correct value y within tolerance

     # File lib/flt/tolerance.rb, line 180
180:     def equal_to?(x, y)
181:       (x-y).abs <= relative_to(y)
182:     end
greater_than?(x,y) click to toggle source

x > correct value y within tolerance

     # File lib/flt/tolerance.rb, line 175
175:     def greater_than?(x,y)
176:       x-y > relative_to(y)
177:     end
gt?(x,y) click to toggle source

greater_than: x > y within tolerance

     # File lib/flt/tolerance.rb, line 153
153:     def gt?(x,y)
154:       x-y > relative_to_many(:max, x, y)
155:     end
integer(x) click to toggle source

If the argument is close to an integer it rounds it

     # File lib/flt/tolerance.rb, line 139
139:     def integer(x)
140:       # return integer?(x) ? x.round : nil
141:       r = x.round
142:       ((x-r).abs <= relative_to(x)) ? r : nil
143:     end
integer?(x) click to toggle source

Returns true if the argument is approximately an integer

     # File lib/flt/tolerance.rb, line 133
133:     def integer?(x)
134:       # Computing the tolerance at x seems the best option here
135:       (x-x.round).abs <= relative_to(x)
136:     end
less_than?(x,y) click to toggle source

x < correct value y within tolerance

     # File lib/flt/tolerance.rb, line 170
170:     def less_than?(x,y)
171:       y-x > relative_to(y)
172:     end
lt?(x,y) click to toggle source

less-than: x < y within tolerance

     # File lib/flt/tolerance.rb, line 148
148:     def lt?(x,y)
149:       y-x > relative_to_many(:max, x, y)
150:     end
relative_to(x) click to toggle source

This method is redefined in derived classes to compute the tolerance value in relation to the value x;

If not redefined, relative_to_many will be used.

     # File lib/flt/tolerance.rb, line 187
187:     def relative_to(x)
188:       relative_to_many(:max, x)
189:     end
relative_to_many(mode, *xs) click to toggle source
 This method is redefined in derived classes to compute the tolerance value in relation to the values xs;
 mode must be either :max or :min, and determines if the largerst (relaxed condition) or smallest

(strict condition) of the relative tolerances is returned.

 If not redefined, relative_to will be used, but redefining this method can be used to optimize the
 performance
     # File lib/flt/tolerance.rb, line 197
197:     def relative_to_many(mode, *xs)
198:       xs.map{|x| relative_to(x)}.send(mode)
199:     end
seq?() click to toggle source

strongly equals: x == y within tolerance (strict)

     # File lib/flt/tolerance.rb, line 163
163:     def seq?
164:       (x-y).abs <= relative_to_many(:min, x, y)
165:     end
to_s() click to toggle source

Description of the tolerance

     # File lib/flt/tolerance.rb, line 122
122:     def to_s
123:       descr_value
124:     end
value(x=nil) click to toggle source

Value of the tolerance for a given (floating-point) quantity

     # File lib/flt/tolerance.rb, line 108
108:     def value(x=nil)
109:       if x
110:         relative_to(x)
111:       else
112:         @value
113:       end
114:     end
zero?(x, y=nil) click to toggle source

Is x nearly zero? (zero within tolerance); if a second argument y is specified: is x nearly zero? compared to y?

     # File lib/flt/tolerance.rb, line 128
128:     def zero?(x, y=nil)
129:       x.zero? || x.abs < value(y || x)
130:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.