junit - Testing for upper bound violation in Java -


i'm trying use junit test java program, , i'm not sure how go testing upper-bound violations.

specifically, have written simple program convert between kilometers , miles.

for example, here method converting miles kilometers

    public static double miletokm(double mile){     //1.1170347260596139e308 / 0.621371192 = double.max_value     try{         if (mile < 0 || mile > 1.1170347260596139e308){             throw new illegalargumentexception();         }         else             return mile / 0.621371192;}     return 0; } 

so, guess question two-fold: first, why can't conjure exception when try

miletokm(1.1170347260596139e308 + 1) 

in junit? assume it's rounding issue, if that's case how can exception thrown?

second, method convert km mile, want throw exception if parameter greater double.max_value. how can pass such parameter? can junit test pass if pass parameter double.max_value * 10, message in console (this in eclipse mars 4.5.1, btw) saying 'max = 1.7976931348623157e308'. parameter has double can't bigdecimal or that.

ok, lied, question three-fold. what's this:

double value =  double.max_value * 0.621371192; //max_value * conversion factor system.out.println(value); 

prints 1.1170347260596138e308, these 2 statements

system.out.println(value / 0.621371192); system.out.println(double.max_value); 

print 1.7976931348623155e308 , 1.7976931348623157e308, respectively. in other words, expect these 2 values both equivalent double.max_value, first statement has 5 right before e, instead of 7. how can fix this? much, hope isn't prolix.

you're confused floating point numbers.

firstly, number 1.1170347260596139e308 + 1 is not representable using primitives in java, doubles have ~16 significant digits, , addition requires 308 significant digits.

secondly, float/double operations not idempotent if use intermediate storage (and of times without it). floating point operations lose accuracy, , arithmetic methods retain accuracy on large computations (think weather models) sought after in scientific sector.

thirdly, there's double.max_value, represents largest representable number in primitive in java; other value x such x > double.max_value can hold double.positive_infinity, , that's not real number.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -