Mixing various Frameworks and Libraries I often see code like this
float floatValue = 0.5f;
float returnValue = 0.0f;
returnValue = (float)[object someMethod:(double)floatValue]; //returns double
Is there any overhead in casting back-and-forth between double
and float
?
I'm mainly interested in objective-c and the llvm compiler but would happy up vote answers for other languages and compilers.
Casting between float
and double
is a single conversion instruction in either direction on both ARM and x86. There are some cases in which the compiler is able to eliminate these conversions (when it can prove that they would not effect the result of the computation), but that will not be the case in examples like those you listed.
So, there is a small amount of overhead. A few extra casts at high-level interface boundaries won't have a noticeable effect. However, you don't want to convert every value in a tight loop if you can avoid it.