0
public double hypotenuse()
{       
    //return Math.hypot(leg, leg); //returns 7.0710678118654755
    //return 0.1 * Math.floor(Math.hypot(leg, leg) * 10); //returns 7.0
    //return 0.1 * Math.ceil(Math.hypot(leg, leg) * 10); //returns 7.1000000000000005
    return 0.1 * Math.round(Math.hypot(leg, leg) * 10); //returns 7.1000000000000005
}

I am trying to round to the nearest 10th place, so this number should round up to 7.1. Why does this method work with Math.floor but not with Math.round? Does anyone have any insight?

Thanks,

Sky

Millie Smith
  • 4,536
  • 2
  • 24
  • 60
Skylin
  • 3
  • 1
  • 2
  • 2
    What is this language? – Eugene Sh. Mar 13 '15 at 19:43
  • Different language, but likely on-point: http://stackoverflow.com/q/6875007/239394. It appears to me that you are just experiencing double-precision rounding issues. – Andrew Mar 13 '15 at 20:44
  • It's for an assignment. It was specified to use a double and round to the nearest tenth place. I'll keep BigDecimal in mind though. – Skylin Mar 17 '15 at 15:22

3 Answers3

0

Nevermind, this is what I found that works.

public double hypotenuse()
{       
    double hypot = Math.hypot(leg, leg);
    String str = String.format("%1.1f", hypot);
    hypot = Double.valueOf(str);
    return hypot;

}
Millie Smith
  • 4,536
  • 2
  • 24
  • 60
Skylin
  • 3
  • 1
  • 2
0

In regards to your answer to yourself, you don't want to convert an object of type A to type B, operate on it, and then convert it back to type A. Not if you can do anything else.

The following does what you want it to do:

public double hypotenuse()
{
    return Math.round(Math.hypot(leg, leg) * 10) / 10.0;
}

I'm not entirely sure why. It would take some digging into what this compiles down to at the assembly level.

The real problem is that each double in Java only has a limited number of bits to store its decimal portion. There are an infinite number of possible real numbers in real life. There is no way you can represent every single real number with only 64 bits. This is why you should avoid floating point calculations in banking apps and software that requires the number to be exact. Multiplying a few doubles together can introduce quite a significant error margin in the calculation. See this explanation on another SO answer, which does a great job of explaining this further.

If you really need it to be exact, use BigDecimal.

Community
  • 1
  • 1
Millie Smith
  • 4,536
  • 2
  • 24
  • 60
  • I knew there was a way to do it with one line of code! And I swear I tried this and it didn't work, but it's working now. Thank you! :-) I've run into this rounding issue in several programs now and it has caused me so much stress! Thank you so much for your help! – Skylin Mar 15 '15 at 03:27
  • You're welcome. If you care about perfect precision, though, you should really use `BigDecimal`. – Millie Smith Mar 15 '15 at 03:28
  • @Skylin, may I ask why you care so much about the numbers being exact? – Millie Smith Mar 15 '15 at 03:33
-1

If this is for JS:

The Math.round() function returns the value of a number rounded to the nearest integer.