Java float division precision?

You're dividing integers, which means that you're using integer division.

You're dividing integers, which means that you're using integer division. In integer division the fractional part of the result is thrown away. Try the following: float res = (float) quantity / standard; ^^^^^^^ The above forces the numerator to be treated as a float which in turn promotes the denominator to float as well, and a float-division is performed instead of an int-division.

Another common method is to multiply the numerator or denominator by 1.0. By the way, there is almost no reason to ever use float instead of double, unless you really care about saving 4 bytes. – Paul Tomblin Sep 2 at 16:49 Yep.Agreed.... – aioobe Sep 2 at 16:51 @Paul: and so there is no reason to ever use int instead of long, unless... :-) – Carlos Heuberger Sep 2 at 21:37 @Carlos, maybe not in Java (actually, I don't know), but in C or C++, int can be considerably faster than long, whereas thanks to "math coprocessors" (some of remember when these were separate chips) doubles are often faster than floats because they don't have to be down converted.

– Paul Tomblin Sep 2 at 22:08.

If you concerned about precision I would suggest using double which has more than double the number of digits of precision. However floating point only accurately represents fractions which are a sum or powers of 0.5. This means 0.6 is only approximately represented.

This doesn't have to be a problem with appropriate rounding. Double d = (double) 6800 / 500; or double d = 6800.0 / 500.

Is there any perfomance differences between double and float?....I've been doing some light reading and I might be a little concerned regarding memory usage, the machine on which the software is going to be deployed is rather old – Julian C. Sep 6 at 19:08 double uses 4 more bytes. If memory is very tight it can be worth using float and if you have a million of these, it could save 4 MB.

If you don't have millions of these it probably not worth worrying about. – Peter Lawrey Sep 6 at 20:47 Nah, machine's not THAT old – Julian C. Sep 20 at 14:58.

Won't make a damn bit of difference if both numerator and denominator are ints. – Paul Tomblin Sep 2 at 16:48 Sure, but nowhere does it state they were both declared at ints. – Ken Sep 2 at 16:52 1 If you look at the results he got, it was obvious it was integer division, not rounding.

– Paul Tomblin Sep 2 at 19:22.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions