tags:

views:

170

answers:

3

Hey guys, I had some questions about putting f next to literal values. I know it defines it as a float but do I really need it? Is this 2.0f * 2.0f any faster then 2.0 * 2.0? and if I have a variable like float a = 2.0; is this 2.0f * a any faster then 2.0 * a?

+3  A: 

I's rarely about speed (at least directly), but the fact that otherwise the compiler will warn about converting double to float.

Jerry Coffin
Ok but on the a * 2.0 example is that really using double multiplication?
Justin Meiners
In theory, I believe that should result in multiplying two `double`s, that converting the result to a `float`. In reality, with it being 2.0, chances are that the compiler can/will figure out that it can use `float` throughout. OTOH, there are mandatory limits on optimizing FP math (especially in C99), so making it explicit could help.
Jerry Coffin
@Jerry Coffin yeah I was just using 2.0 as an example. Thanks for the help.
Justin Meiners
Sometimes, it is also about accuracy. Assuming your variable names do not reflect their types, 2.0f actually warns anyone else reading the code that they need to keep in mind the fact that they are dealing with a float and not a double. This becomes handy knowledge when debugging things like loops with lots of comparisons in them.
carleeto
+7  A: 

Sometimes you need it to explicitly have type float, like in the following case

float f = ...;
float r = std::max(f, 42.0); // won't work; (float, double).
float r = std::max(f, 42.0f); // works: both have same type
Johannes Schaub - litb
+2  A: 

AFAIK, on "normal" PCs (x86 with x87-like mathematical coprocessor) the difference in speed is irrelevant, since the calculations are internally done anyway in 80-bit precision.

Floats may gain importance when you have large arrays of floating-point numbers to manage (scientific calculations or stuff like that), so having a smaller data type may be convenient, both to use less memory and to be faster to read them from RAM/disk.

It may also be useful to use floats instead of doubles on machines that lack a floating point unit (e.g. most microcontrollers), where all the floating-point arithmetic is performed in software by code inserted by the compiler; in this case, there may be a gain in speed operating on floats (and in such environments often also every bit of memory matters).

On PCs, IMO you can just use double in "normal" contexts, just try to avoid mixing datatypes (double, floats, ints, ...) in the same expression to avoid unnecessary costly conversions. Anyhow, with literals the compiler should be smart enough to perform the conversion at compile time.

Matteo Italia
Yep I am using iphone.
Justin Meiners
Since what I've read, the iPhone processor is an ARM1176 with the optional FPU provided (see here: http://www.arm.com/products/processors/technologies/vector-floating-point.php); I'm no expert of it, but I don't think that speed changes a lot with floats/doubles in this case. My only concern is that it has "16 double precision or 32 single precision registers", so using only floats you may gain something from the additional registers, but it may depend also on how the environment is set up.
Matteo Italia
Well I know you can but on the iphone double is definitely slower then float.
Justin Meiners
The only way to find out is profiling. Anyhow, if you want to go safe and you don't need the additional precision of doubles, just use floats.
Matteo Italia
Yeah that is what I am doing now and I think I will stick with it. Thanks for the help.
Justin Meiners
Turns out that someone else already had a similar question: have a look at http://stackoverflow.com/questions/1622729/double-vs-float-on-the-iphone
Matteo Italia
Thanks For all the help!
Justin Meiners
No problem. :-)
Matteo Italia