# Float or Double

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

My code is like that:

float/double angle;
..
..=Math.sin(angle);
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:

float
0
Commented:
Float in Java is 32 bits while double is 64. Hence, doing mathematics (on the machine level) is faster for float.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Thanks I just wanted to be sure, before going through my code !
0
Author 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)

0
Commented:
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,
OR
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.

0
Author 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:

www.student.dtu.dk/~c971448/3ugers
I will upload it in 2 dayes !

But thanks for the help !
0
Author 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 !!
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.