junit - Performant way to check java.lang.Double for equality -


what performant way check double values equality.

i understand that

double = 0.00023d; double b = 0.00029d;  boolean eq = (a == b); 

is slow.

so i'm using

double epsilon = 0.00000001d; eq = math.abs(a - b) < epsilon; 

the problem infinitest complaning tests taking time. it's not big deal (1 sec top), made me curious.

additional info

a hard coded since it's expected value, b computed by

  // fyi: current = int, max = int   public double getstatus()   {     double value = 0.0;     if (current != 0 && max != 0)       value = ((double) current) / max;     return value;   } 

update

java.lang.double way

public boolean equals(object obj) { return (obj instanceof double)        && (doubletolongbits(((double)obj).value) ==           doubletolongbits(value)); } 

so 1 assume best practice.

junit has method of checking double 'equality' given delta:

assert.assertequals(0.00023d, 0.00029d, 0.0001d); 

see this api documentation.

as noted in comments, junit slower comparing manually given delta. junit first double.compare(expected, actual) followed (if not equal) math.abs(expected - actual) <= delta.

hopefully answer still useful people not aware junit provides way inexact double equality testing.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -