Epsilon in Java 1.6

516 views Asked by At

I work on a product called Oracle RPAS as a consultant and the application is developed using C++. In Oracle RPAS , JNI calls can be made to custom developed Java programs to process the data. The data stored in Oracle RPAS for Real is always Double(8 bytes) and same is available in Java. Unfortunately Oracle RPAS is using a hard coded value of 0.0000000001 (1e-09) as epsilon for comparing doubles and other calculations even though data is stored in database as Double. In Java I am not able to find out a way to align the code with this hard coded value. I need to compare the dataset in similar way . These are the mathematical operations, I need to perform mostly min(double x,double Y) , max(double x,double Y) round(double x*double Y)/ double Y) , ceil ,floor etc.

I need help in understanding how epsilon works in Java ? Java version used for development is 1.6

I am not an expert in Java and have some coding experience in it and any starting point will be helpful to tackle this problem.

2

There are 2 answers

0
Adrian Shum On

The whole idea of epsilon should be base on your application logic. If you are going to deal with data which you expect to have value till the 3rd decimal place, then use an epsilon like 0.0001. Or similarly, if you know your application is going to deal with number up to a billion (1,000,000,000), then you know double can be at most accurate til the 5-6th decimal places (15 significant figures minus 10 digits before decimal). Hence choosing 0.000001 as epsilon will be reasonable.

As far as I know, Java does not do any internal epsilon handling during double comparison. It needs to be done explicitly, just like when you are writing C/C++.

Of course you may also consider BigDecimal as an alternative. However you still need to define the precision you want to keep when you are doing BigDecimal arithmetic, with somewhat similar reasoning like epsilon for double.

1
kunji mamu On

In Java you use BigDecimal instead of Double and then you don't need to use any epsilon at all.