Howdy,
>> It may work, or it may not. If it "works" - in the sense of making the
>> test failure go away - then I would expect it to bite someone later when
>> they just happen to hit upon a number where the floating point rounding
>> error goes the other way.
>>
>> Check that it's in an acceptable range.
I heartily agree with this. Math::GSL has thousands of tests like
this, which is why Math::GSL::Test
has a number of easy-to-use functions to do this.
> Perhaps I should stringify and then re-numberify the value inside round()
> instead?
>
I very much recommend that you look at is_similar() in
Math::GSL::Test, it has implemented at least a
few wheels that you are destined to want:
is_similar($x,$y;$eps,$similarity_function)
is_similar($x,$y);
is_similar($x, $y, 1e-7);
is_similar($x,$y, 1e-3, sub { ... } );
Return true if $x and $y are within $eps of each other, i.e.
abs($x-$y) <= $eps
If passed a code reference $similarity_function, it will pass $x and $y
as parameters to it and will check to see if
$similarity_function->($x,$y_) <= $eps
The default value of $eps is 1e-8. Don't try sending anything to the
Moon with this value...
You can use the $similarity_function to implement relative error
bounds and there is also a is_similar_relative() wrapper. All of these
functions take array references of floating point values and map over
them. The reason these methods are in Math::GSL::Test is because they
also correctly handle GSL_NAN correctly, so you can ignore those bits.
Cheers,
--
Jonathan Leto
[email protected]
http://leto.net