Class: Float
Overview
Ruby base class Float extension
Mainly to be able to compare two floats with specific accuracy
Class Method Summary collapse
-
.floatlist_include?(floatlist, float, epsilon = 0.0000001) ⇒ Boolean
check if an Float item is included in a Float list, with specific precision assert( Float.floatlist_include?( [1.0,2.0,3.0001,4.0], 3.0, 0.001 ) ) assert_equal( false, Float.floatlist_include?( [1.0,2.0,3.0001,4.0], 3.0, 0.0001 ) ).
-
.sort_float_list(floatlist, epsilon = 0.0000001) ⇒ Object
sort and remove duplicated elements of a float list with specific precision assert_equal( [0.25], Float.sort_float_list([0.26,0.25], 0.02 ) ) assert_equal( [0.25,0.26], Float.sort_float_list([0.26,0.25], 0.01 ) ).
Instance Method Summary collapse
-
#fequal?(other, epsilon = 0.0000000001) ⇒ Boolean
compare two Float with specific precision assert( 0.25.fequal?( 0.26,0.02) ) assert_equal( false, 0.25.fequal?( 0.26,0.01) ).
Class Method Details
.floatlist_include?(floatlist, float, epsilon = 0.0000001) ⇒ Boolean
522 523 524 525 526 527 528 529 |
# File 'lib/utils.rb', line 522 def Float.floatlist_include?( floatlist, float, epsilon=0.0000001 ) floatlist.each do |item| if item.fequal?( float, epsilon ) return true end end return false end |
.sort_float_list(floatlist, epsilon = 0.0000001) ⇒ Object
508 509 510 511 512 513 514 515 516 517 |
# File 'lib/utils.rb', line 508 def Float.sort_float_list( floatlist, epsilon=0.0000001 ) floatlist = floatlist.uniq.sort result = [floatlist[0]] floatlist[1..-1].each do |item| if not item.fequal?( result[-1], epsilon) result.push( item ) end end return result end |
Instance Method Details
#fequal?(other, epsilon = 0.0000000001) ⇒ Boolean
compare two Float with specific precision
assert( 0.25.fequal?( 0.26,0.02) )
assert_equal( false, 0.25.fequal?( 0.26,0.01) )
501 502 503 |
# File 'lib/utils.rb', line 501 def fequal?( other, epsilon=0.0000000001 ) return ((self - other).abs < epsilon) end |