Class: Nio::Tolerance
- Inherits:
-
Object
- Object
- Nio::Tolerance
- Includes:
- StateEquivalent
- Defined in:
- lib/nio/flttol.rb
Overview
This class represents floating point tolerances for Float numbers and allows comparison within the specified tolerance.
Class Method Summary collapse
- .big_epsilon(n = 1, mode = :sig) ⇒ Object
- .decimals(d = 0, mode = :abs, rounded = true) ⇒ Object
- .epsilon(n = 1, mode = :sig) ⇒ Object
- .fraction(f) ⇒ Object
- .percent(p) ⇒ Object
- .permille(p) ⇒ Object
- .sig_decimals(d = 0, mode = :abs, rounded = true) ⇒ Object
Instance Method Summary collapse
-
#[](x) ⇒ Object
Shortcut notation for get_value.
-
#apprx_i(x, result = Integer) ⇒ Object
If the argument is close to an integer it rounds it and returns it as an object of the specified class (by default, Integer).
-
#apprx_i?(x) ⇒ Boolean
Returns true if the argument is approximately an integer.
-
#aprx_equals?(x, y) ⇒ Boolean
Approximate equality within tolerance.
-
#big_epsilon(n = 1, mode = :sig) ⇒ Object
As #epsilon but using a somewhat bigger (about twice) precision that assures associative multiplication.
-
#decimal? ⇒ Boolean
Returns true for decimal-mode tolerance.
-
#decimals(d, mode = :abs, rounded = true) ⇒ Object
This initializes a Tolerance with a given number of decimals.
-
#epsilon(times_epsilon = 1, mode = :sig) ⇒ Object
Initialize with a multiple of the internal floating-point precision.
-
#equals?(x, y) ⇒ Boolean
Essential equality within tolerance.
-
#fraction(f) ⇒ Object
Initialize with a relative fraction.
-
#get_value(x) ⇒ Object
Return tolerance relative to a magnitude.
-
#greater_than?(x, y) ⇒ Boolean
Comparison within tolerance.
-
#initialize(t = 0.0, mode = :abs, decmode = false) ⇒ Tolerance
constructor
The tolerance mode is either :abs (absolute) :rel (relative) or :sig (significant).
-
#less_than?(x, y) ⇒ Boolean
Comparison within tolerance.
-
#magnitude ⇒ Object
Returns the magnitude of the tolerance.
-
#mode ⇒ Object
Returns the mode (:abs, :rel, :sig) of the tolerance.
-
#num_class ⇒ Object
The numeric class this tolerance applies to.
-
#num_decimals ⇒ Object
Returns the number of decimal digits of the tolerance.
-
#percent(x) ⇒ Object
Initialize with a percentage.
-
#permille(x) ⇒ Object
Initialize with a per-mille value.
-
#sig_decimals(d, rounded = true) ⇒ Object
This initializes a Tolerance with a number of significant decimal digits.
-
#zero?(x, compared_with = nil) ⇒ Boolean
Comparison within tolerance.
Methods included from StateEquivalent
Constructor Details
#initialize(t = 0.0, mode = :abs, decmode = false) ⇒ Tolerance
The tolerance mode is either :abs (absolute) :rel (relative) or :sig (significant). The last parameter is a flag to specify decimal mode for the :sig mode
113 114 115 |
# File 'lib/nio/flttol.rb', line 113 def initialize(t=0.0, mode=:abs, decmode=false) set t, mode, decmode end |
Class Method Details
.big_epsilon(n = 1, mode = :sig) ⇒ Object
366 367 368 |
# File 'lib/nio/flttol.rb', line 366 def Tolerance.big_epsilon(n=1, mode=:sig) Tolerance.new.big_epsilon(n, mode) end |
.decimals(d = 0, mode = :abs, rounded = true) ⇒ Object
357 358 359 |
# File 'lib/nio/flttol.rb', line 357 def Tolerance.decimals(d=0, mode=:abs,rounded=true) Tolerance.new.decimals(d,mode,rounded) end |
.epsilon(n = 1, mode = :sig) ⇒ Object
363 364 365 |
# File 'lib/nio/flttol.rb', line 363 def Tolerance.epsilon(n=1, mode=:sig) Tolerance.new.epsilon(n, mode) end |
.fraction(f) ⇒ Object
369 370 371 |
# File 'lib/nio/flttol.rb', line 369 def Tolerance.fraction(f) Tolerance.new.fraction(f) end |
.percent(p) ⇒ Object
372 373 374 |
# File 'lib/nio/flttol.rb', line 372 def Tolerance.percent(p) Tolerance.new.percent(p) end |
Instance Method Details
#[](x) ⇒ Object
Shortcut notation for get_value
162 163 164 |
# File 'lib/nio/flttol.rb', line 162 def [](x) return x.nil? ? @t : get_value(x) end |
#apprx_i(x, result = Integer) ⇒ Object
If the argument is close to an integer it rounds it and returns it as an object of the specified class (by default, Integer)
296 297 298 299 |
# File 'lib/nio/flttol.rb', line 296 def apprx_i(x,result=Integer) r = x.round return equals?(x,r) ? Nio.numeric_cast(r,result) : x end |
#apprx_i?(x) ⇒ Boolean
Returns true if the argument is approximately an integer
291 292 293 |
# File 'lib/nio/flttol.rb', line 291 def apprx_i?(x) equals?(x,x.round) end |
#aprx_equals?(x, y) ⇒ Boolean
Approximate equality within tolerance
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/nio/flttol.rb', line 207 def aprx_equals?(x,y) case @mode when :sig if @decimal_mode begin x_exp = Math.log10(x.abs) #x_exp = x_exp.finite? ? x_exp.ceil : 0 x_exp = x_exp.finite? ? x_exp.floor+1 : 0 rescue x_exp = 0 end begin y_exp = Math.log10(y.abs) #y_exp = y_exp.finite? ? y_exp.ceil : 0 y_exp = y_exp.finite? ? y_exp.floor+1 : 0 rescue y_exp = 0 end (y-x).abs <= @t*(10**([x_exp,y_exp].max-@@dec_ref_exp)) else z,x_exp = Math.frexp(x) z,y_exp = Math.frexp(y) (y-x).abs <= Math.ldexp(@t,[x_exp,y_exp].max-@@ref_exp) # (y-x).abs <= @t*(2**([x_exp,y_exp].max-@@ref_exp)) end when :rel (y-x).abs <= @t*([x.abs,y.abs].max) #reference value is 1 when :abs (x-y).abs<=@t end end |
#big_epsilon(n = 1, mode = :sig) ⇒ Object
As #epsilon but using a somewhat bigger (about twice) precision that assures associative multiplication.
142 143 144 145 |
# File 'lib/nio/flttol.rb', line 142 def big_epsilon(n=1, mode=:sig) t = Math.ldexp(0.5*n,3-Float::MANT_DIG) # n*(2*Float::EPSILON/(1-0.5*Float::EPSILON)**2) set t, mode end |
#decimal? ⇒ Boolean
Returns true for decimal-mode tolerance
311 312 313 |
# File 'lib/nio/flttol.rb', line 311 def decimal? @decimal_mode end |
#decimals(d, mode = :abs, rounded = true) ⇒ Object
This initializes a Tolerance with a given number of decimals
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/nio/flttol.rb', line 119 def decimals(d, mode=:abs, rounded=true) @mode = mode @decimal_mode = true @d = (d<=0 || d>Float::DIG) ? Float::DIG : d @t = 10**(-@d) @t *= 0.5 if rounded self end |
#epsilon(times_epsilon = 1, mode = :sig) ⇒ Object
Initialize with a multiple of the internal floating-point precision.
136 137 138 |
# File 'lib/nio/flttol.rb', line 136 def epsilon(times_epsilon=1, mode=:sig) set Float::EPSILON*times_epsilon, mode end |
#equals?(x, y) ⇒ Boolean
Essential equality within tolerance
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/nio/flttol.rb', line 170 def equals?(x,y) case @mode when :sig if @decimal_mode begin x_exp = Math.log10(x.abs) #x_exp = x_exp.finite? ? x_exp.ceil : 0 x_exp = x_exp.finite? ? x_exp.floor+1 : 0 rescue x_exp = 0 end begin y_exp = Math.log10(y.abs) #y_exp = y_exp.finite? ? y_exp.ceil : 0 y_exp = y_exp.finite? ? y_exp.floor+1 : 0 rescue y_exp = 0 end (y-x).abs <= @t*(10**([x_exp,y_exp].min-@@dec_ref_exp)) else z,x_exp = Math.frexp(x) z,y_exp = Math.frexp(y) (y-x).abs <= Math.ldexp(@t,[x_exp,y_exp].min-@@ref_exp) # (y-x).abs <= @t*(2**([x_exp,y_exp].min-@@ref_exp)) end when :rel (y-x).abs <= @t*([x.abs,y.abs].min) #reference value is 1 when :abs (x-y).abs<@t end end |
#fraction(f) ⇒ Object
Initialize with a relative fraction
148 149 150 |
# File 'lib/nio/flttol.rb', line 148 def fraction(f) set f, :rel end |
#get_value(x) ⇒ Object
Return tolerance relative to a magnitude
166 167 168 |
# File 'lib/nio/flttol.rb', line 166 def get_value(x) rel(x) end |
#greater_than?(x, y) ⇒ Boolean
Comparison within tolerance
244 245 246 |
# File 'lib/nio/flttol.rb', line 244 def greater_than?(x,y) less_than?(y,x) end |
#less_than?(x, y) ⇒ Boolean
Comparison within tolerance
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/nio/flttol.rb', line 248 def less_than?(x,y) case @mode when :sig if @decimal_mode begin x_exp = Math.log10(x.abs) #x_exp = x_exp.finite? ? x_exp.ceil : 0 x_exp = x_exp.finite? ? x_exp.floor+1 : 0 rescue x_exp = 0 end begin y_exp = Math.log10(y.abs) #y_exp = y_exp.finite? ? y_exp.ceil : 0 y_exp = y_exp.finite? ? y_exp.floor+1 : 0 rescue y_exp = 0 end y-x > @t*(10**([x_exp,y_exp].max-@@dec_ref_exp)) else z,x_exp = Math.frexp(x) z,y_exp = Math.frexp(y) y-x > Math.ldexp(@t,[x_exp,y_exp].max-@@ref_exp) # y-x > @t*(2**([x_exp,y_exp].max-@@ref_exp)) end when :rel y-x > @t*([x.abs,y.abs].max) #reference value is 1 when :abs x-y<@t end end |
#magnitude ⇒ Object
Returns the magnitude of the tolerance
303 304 305 |
# File 'lib/nio/flttol.rb', line 303 def magnitude @t end |
#mode ⇒ Object
Returns the mode (:abs, :rel, :sig) of the tolerance
315 316 317 |
# File 'lib/nio/flttol.rb', line 315 def mode @mode end |
#num_class ⇒ Object
The numeric class this tolerance applies to.
107 108 109 |
# File 'lib/nio/flttol.rb', line 107 def num_class Float end |
#num_decimals ⇒ Object
Returns the number of decimal digits of the tolerance
307 308 309 |
# File 'lib/nio/flttol.rb', line 307 def num_decimals @d end |
#percent(x) ⇒ Object
Initialize with a percentage
152 153 154 |
# File 'lib/nio/flttol.rb', line 152 def percent(x) fraction x/100.0 end |
#permille(x) ⇒ Object
Initialize with a per-mille value
156 157 158 |
# File 'lib/nio/flttol.rb', line 156 def permille(x) fraction x/1000.0 end |
#sig_decimals(d, rounded = true) ⇒ Object
This initializes a Tolerance with a number of significant decimal digits
131 132 133 |
# File 'lib/nio/flttol.rb', line 131 def sig_decimals(d, rounded=true) decimals d, :sig, rounded end |
#zero?(x, compared_with = nil) ⇒ Boolean
Comparison within tolerance
285 286 287 |
# File 'lib/nio/flttol.rb', line 285 def zero?(x,compared_with=nil) compared_with.nil? ? x.abs<@t : x.abs<rel(compared_with) end |