The Podman wrote: > As some point, I'm asking Ruby to assert_equal(a,b), where a and b are > Vectors (from the matrix.rb file). Unfortunatley one test fails, > specifically in the case where Vector[27.8, 0.0] does not equal > Vector[27.8, 0.0]. After much head scratching and playing about with the > debugger, I realised it's the age old problem of floating point > comparison. For some reason I just didn't expect to have to worry about > this in Ruby! Annoyingly, I don't know how to go about "fixing" my > problem. and my problem is this: > You may find something like this useful. Its based on the tolerant compare function used in APL where it is used as the definition of equality for floating point numbers. Note that in APL fuzz is actually a special global that can be set as necessary to change the relative tolerance. Note that this is quick and dirty code and does not handle the special case when one of the arguments is zero. This value of fuzz would be appropriate for "equality within 10 decimal places". Ruby has a variety of means to extend or modify assert_equal to work this way. -------------------------------- def fuzzy_equals( x, y ) fuzz = 1.0E-10 max = [x.abs, y.abs].max ( x - y ).abs / max < fuzz end class TC_Fuzzy < Test::Unit::TestCase def test_01 assert( fuzzy_equals( 17 + 1.0E-10, 17 ) ) assert( fuzzy_equals( 1.7 + 1.0E-10, 1.7 ) ) assert( ! fuzzy_equals( 0.7 + 1.0E-10, 0.7 ) ) assert( fuzzy_equals( 17 - 1.0E-10, 17 ) ) assert( fuzzy_equals( 1.7 - 1.0E-10, 1.7 ) ) assert( ! fuzzy_equals( 0.7 - 1.0E-10, 0.7 ) ) assert( fuzzy_equals( -17 + 1.0E-10, -17 ) ) assert( fuzzy_equals( -1.7 + 1.0E-10, -1.7 ) ) assert( ! fuzzy_equals( -0.7 + 1.0E-10, -0.7 ) ) assert( fuzzy_equals( -17 - 1.0E-10, -17 ) ) assert( fuzzy_equals( -1.7 - 1.0E-10, -1.7 ) ) assert( ! fuzzy_equals( -0.7 - 1.0E-10, -0.7 ) ) end end ---------------------------------- Bill Rutiser