Float or Double

Hey I want to use the fastest type,
is i float, or double ?

My code is like that:

float/double angle;
Who is Participating?
hsaqallahConnect With a Mentor Commented:
Float in Java is 32 bits while double is 64. Hence, doing mathematics (on the machine level) is faster for float.

win32Author Commented:
Thanks I just wanted to be sure, before going through my code !
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

win32Author Commented:
Hmm I am sure that float is fastest, but, when i write, like this

float x, angle;

x = Math.sin(angle);

Compile error occure, (cast error)

So I have to write

x = (float) Math.sin(..);

Ok Math.sin() returns double, where can i find a sin() that returns float ??

You say it is faster to use float, but I don't think so if I have to cast the result !!

And also I want to optimise my code (so I nead a sin() that returns flaot)

PS. Please help, me ! :-)

Unfortunately, I think we're all stuck with the double sin() because Sun didn't provide a method that returns float. And your casting statement will it worse, because casting needs CPU time.
So, either you store your results in double variable and cast them LATER after you finish the time-critical operation you're doing,
if you're looking for speed, you can use native code from Java, but his means that your code will NOT be pure Java and will run only the platform on which you have the native library.

By the way, the method sin() takes a 'double' argument, this means that actually, there is an implicit conversion taking place to convert 'angle' to double.

win32Author Commented:
Year thats a problem, it seems that it goes faster with double, Sorry I can't wait to cast it, the calculation is needed inside a Render loop in Java3D, so I nead the calculation for eatch frame * 5 times, I am running (best case) 20FPS, thats 100 times pr. sec

If you want to take a look at the code, it you can get it at:

I will upload it in 2 dayes !

But thanks for the help !
win32Author Commented:
Hmm year I could also use precalculation of the angles I am using, but by doing that I think my code looks like old C. I want to keap it object oriented !!
All Courses

From novice to tech pro — start learning today.