4

I am working my way through previous exam papers trying to build my experience in Java. There are two answers to this particular question. The first is my own which seems straight forward and the second is that of my lecturer, which seems confusing to me at this particular stage of my Java development.

Here is my code:

public class InClassTestTwoQ2
{
public static void main(String[] args){
    double sum = 3.14;

    System.out.println(test(sum));
    System.out.println(testTwo(sum));
}

public static boolean test(double sum){
    return sum != 3.14; //My boolean test return type
    }
public static boolean testTwo(double sum){
    return Math.abs(sum - 3.14) > 1e-14; //Lecturer boolean test return type
    }
}

Is using Math.abs a better option here? Also, I am not sure what the 1e-14 is doing? Can someone explain any possibilities as to why my lecturer has returned his boolean statement this way? Mine seems straight forward where as I would have never done it his way?

Also, please forgive any errors in my code. I am still learning Java. Many thanks.

PrimalScientist
  • 831
  • 5
  • 17
  • 27
  • This is how floating types work. You'll have to read more about them to understand it completely. –  Mar 17 '13 at 12:42
  • Okay thanks. I will research further then. – PrimalScientist Mar 17 '13 at 12:43
  • 1
    Take a look at this article: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html. I understand there is a little too much of information. You should be able to find other useful articles on the web. –  Mar 17 '13 at 12:43
  • Thanks for the link there. I will take a look! – PrimalScientist Mar 17 '13 at 12:45

2 Answers2

2

To give you an overview on why your lecturer's answer is better whereas your answer is not as straightforward as you think, consider the following:

3.1444440 and 3.1444441

Are they equal? Well in Java if you compare them simply by == then you'll get false. This is why you should never compare doubles/floating types with ==. The best way to compare floating types is by using a tolerance value. You want to test that the result of subtracting the two floating types is WITHIN that tolerance value. For example the tolerance value your teacher used, which is 1e-14. So if sum-3.14 is within the tolerance value, then the two numbers are considered equal.

Also note that Math.abs gives you the absolute value of the subtraction, so you never get a negative number, otherwise you will not get a correct result.

Hope this gives you an overview.

Kakalokia
  • 3,191
  • 3
  • 24
  • 42
  • 2
    It might be obvious for most of us, but I still want to point out that 1e-14 is another way of saying 1*10^-14 (one times ten to the power of minus fourteen) – Sentry Mar 17 '13 at 12:53
  • That is a great answer. Thanks. To be honest, looking at this my answer is poor. This is good to know. – PrimalScientist Mar 17 '13 at 12:56
2

The one that your instructor posted involves usage of the floating point epsilon - which in your case is 1.0e14 (refer to this link):

http://en.wikipedia.org/wiki/Machine_epsilon

As for why you want to use this, and why it is better - refer to this link:

http://www.ibm.com/developerworks/java/library/j-jtp0114/

Which states:

"If you don't know the scale of the underlying measurements, using the test "abs(a/b - 1) < epsilon" is likely to be more robust than simply comparing the difference"

One thing to note is that all platforms and languages have to deal with floating point comparisons on some level. Although this conversation isn't relevant to your language (it's in C++), some of the points made in that discussion apply to Java as well.

What is the most effective way for float and double comparison?

Community
  • 1
  • 1
jrd1
  • 10,358
  • 4
  • 34
  • 51
  • This is good. When I first saw my teachers answer I was thinking why have you answered it like that? We actually thought it was a really crazy answer!?! But on reflection with regards to these comments, the answer is really good. Also, I have learned something here. The importance of floating points so to speak. – PrimalScientist Mar 17 '13 at 12:59
  • 1
    You're welcome! Although, I must say you have a great instructor - since I didn't learn about floating point comparison and the importance of it, until much, much later. – jrd1 Mar 17 '13 at 13:07
  • 1
    He is a really good instructor, and he is patient with us. To be honest, he goes through things really well. – PrimalScientist Mar 17 '13 at 13:10
  • Is it possible to tick both answers? These are both great and do give information about answering my original question!! – PrimalScientist Mar 17 '13 at 13:13
  • 1
    @PrimalScientist, unfortunately *no*. You can choose either, or neither. It's entirely up to you to determine which one answered your question better. In most cases, it's one, in some cases, none. Either way, it's happened before - so you're not alone in that regard. I'm just simply glad I could help, and that I was helpful. Good luck with your decision! – jrd1 Mar 17 '13 at 13:19
  • I made it. But for the record I wanted to choose both. Although one gets a tick, they actually both should!! Thanks for all your help by the way. Top stuff and really helpful. Thanks. – PrimalScientist Mar 17 '13 at 13:23
  • 1
    Not a prob, @PrimalScientist. You're most welcome. Things like these happen all the time. The main thing is that your problem was addressed, _and_ you learned something in the process! _And_, we were able to help you! Win-win-win! ;) – jrd1 Mar 17 '13 at 13:27