Question

I am working on a LibGDX program in Java 7 64 bits.

When I use junit to test a function which receives a float as parameter, I got a strange result. I call the function using 123.123456f as parameter, the function receives 123.12346. Why does this happen?

When I use 12.123456f as parameter, it got the correct result. 123.12345f still works.

Hence I use System.out.println(...) to check the input.

It is not important to me, but I just want to know why. Thank you very much!

Regards, Antony

Was it helpful?

Solution

f means float which means single-precision IEEE-754 floating-point number. They aren't very precise, they only have roughly seven significant digits. You can double that (!) by using d for double, which is a double-precision floating-point number, giving roughly 15 digits of precision. Provided, of course, that whatever you're passing this into accepts doubles and not just floats. If it needs any reasonable precision, it should.

Note, though, that even doubles have issues; they have greater precision, not perfect precision. IEEE-754 floating point is designed for rapid calculation and compact storage. The classic imprecision example, even with doubles, is 0.1 + 0.2, which comes out as 0.30000000000000004.

If you were dealing with currency figures (I think, with that library, you aren't), you might look at BigDecimal, which works more like we're used to working, with an arbitrary number of digits. They're much bigger and much slower, and have their own issues (like the fact they can't accurately represent 1 / 3), but for currency they can be a better choice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top