Re: rounding off double values



jimgardener writes:

i am comparing elements of two double[] .one is an expected set like
double[] expected=new double[]{ 1.246,2.343,4.568};

while the second array is result of some calculation
public double[] mycalculations(){
double[] res=null;
.....//calculate the array
...
return res;
}
and can be
{ 1.246,2.3430000001,4.56800002 }

for practical purposes these two are equal,when you round off the
elements to 3 decimal places.if i want to unit test my code i need to
compare elements of both arrays .How do i do the rounding off?
DecimalFormat only helps me to print or display the numbers.I need
some way to compare like

for(int i=0;i<expected.length;i++){
assertEquals( roundoff( expected[i] ),roundoff( result[ i] ) );
}

can anybody suggest a way to do this?

If you know that all your numbers are of that magnitude, you may get
by with asserting that the absolute difference of the expected and
observed value is smaller than a threshold, like so:

assert(Math.abs(result[i] - expected[i]) < 0.00000001)

This fails with numbers very far from 0, where a negligible difference
can be much larger, and numbers very close to 0 where that threshold
is already huge compared to the numbers themselves. Think of 1e14 and
1e-14. That's where floating points becomes scary, but maybe you don't
need to go there. Two terms to search for are "absolute error" and
"relative error".

(There is a concept of ulps, though: units in the last position. It is
possible to find out how many floating point numbers there are between
the expected and observed value. They are numerically much denser near
zero than far from zero, but this seems to measure whether the program
worked as well as it can given that you use floating point.)
.



Relevant Pages

  • Re: This calculation is just wrong / computer cant count!
    ... result is displayed on the screen (in an edit boxes) along with all the ... You cannot perform floating point math on a computer and compare the ... considered zero. ...
    (microsoft.public.vc.mfc)
  • Re: comparison on non-integer types
    ... http://c-faq.com/thatisall about floating point numbers. ... mathematically but which do not compare the same. ... if you're writing a qsort compar function for an array of floats. ...
    (comp.lang.c)
  • Re: C++ more efficient than C?
    ... for x. bsearch() will compare it with x+2e, ... then you found the number that you were searching for, if it is floating ... says there is no such value in the array. ...
    (comp.lang.c)
  • hi
    ... contains floating points value.i have to store the data by using array ... of structure.i have to compare the previous value and check the ...
    (microsoft.public.vc.mfc)
  • Re: result of strncmp(a,b,0);
    ... The meaning of, isn't the length of either array in ... asking memcmpor strncmpto compare up to zero bytes is not described ...
    (comp.std.c)