Sign in to follow this  
Nicholas Kong

What is the significance of 0.89f?

Recommended Posts

Nicholas Kong    1535

I was looking through some source code which said:

 

 

private static float GROUND_INERTIA = 0.89f;
private static float AIR_INERTIA = 0.89f;
 
And then I started having these questions:
Why 0.89 of all numbers and why is the above declared as a float instead of a double? Is it always like this? From my research, float is a 32 bit single precision number. Why not use a data type that covers more precision?
 
I also notice I have a project I work on, I used System.currentTimeMillis but it gives you it as data type long and the time value fluctuates between .89 and .95 when I use println to print it out.
 
From what I know from books: double has better precision than a long. Why not use a double? I only always use double and int when I declare variables.
Edited by warnexus

Share this post


Link to post
Share on other sites
Waterlimon    4398
No need to use double if you only need to represent something like 0.89.

Doubles are double the length of a float (duh), and possibly slower to operate on, so unless you need all the bits (long operations where errors can accumulate, actual need for high precision...), theres no reason to use double.

Especially since for example graphics like to use floats, people are used to them, and only switch to doubles if its needed.

Kind of like you use ints by default, instead of a 64 bit variant.

Share this post


Link to post
Share on other sites
Matias Goldberg    9576

Why 0.89 of all numbers

It's probably an arbitrary choice or something found through experimentation (empiric observation)

Why not use a data type that covers more precision?

It is true that 0.89 cannot be represented exactly by a float, and neither can a double; but it can represent it more precisely.
However the precision difference is subtle unless you're into highly accurate scientific simulations.

Once we leave the tiny difference a part, main reason is performance. 32-bit calculation are between 1.25x-4x faster depending on the device/architecture that executes them, they need half as RAM, and they waste less memory because of padding alignment (i.e. a long followed by an double requires 16 bytes instead of 12; where a long followed by a float only needs 8 bytes).

From what I know from books: double has better precision than a long. Why not use a double? I only always use double and int when I declare variables.

Double is for 64-by floating point arithmetic (an approximation of "Real" numbers from algebra) while long is a plain 32-bit integer. They're used for entirely different purpose. The 64-bit integer version is "long long" (or "__int64")

Double takes as much as ram as an __int64, and they both take twice as ram as long.
In some devices long (or even long long) is astromically faster than double (i.e. the case of Android devices that lack a floating point unit), while in other architectures there's not much difference.

You can't do bitwise logic to floating point variables (i.e. double) and if you cast back and forth to & from __int64 to do it, you'll get two LHS (load hit store) on most architectures which is a very serious performance penalty.

If you try to use double to store a 32-bit pointer, you also need to cast it, which can cause an LHS.
If you try to use double to store a 64-bit pointer, you can't, big numbers will be truncated and the program in the best case scenario will crash.

That's because __int64 can go as big as 18.446.744.073.709.551.615; while double's max integer value that can be represented is 9.007.199.254.740.992; values bigger than that start to get rounded to the closest representable even number.
long can go as big as 4.294.967.295 btw.

You may want to pass by and look at common mistakes list; and also experiment with an interactive floating point to binary converter (in java) that has educational purposes

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this