3 ms·
I'm going to guess f(double) because floats have always been promoted to doubles for function calls since K&R. But I'm not sure by any means. I'd be ready to ge
by billforsternz 1y ago
I'm going to guess f(double) because floats have always been promoted to doubles for function calls since K&R. But I'm not sure by any means. I'd be ready to get more explicit if I needed some specific behaviour.
- tredre3 1y agoBy mentioning K&R you seem to imply that C also promotes floats to doubles in function calls? But that is not the case, floats are passed as floats, as you'd expect. You can try it yourself on godbolt all the way back to GCC 3, test(float x) has always emitted movss and test(double x) will result in movsd/movlpd.
- jcranmer 1y agoUnless you're calling a variable-argument function--floats are promoted to doubles for variable-argument functions: > The arguments are implicitly converted, as if by assignment, to the types of the corresponding parameters, taking the type of each parameter to be the unqualified version of its declared type. The ellipsis notation in a function prototype declarator causes argument type conversion to stop after the last declared parameter, if present. The integer promotions are performed on each trailing argument, and trailing arguments that have type float are promoted to double. These are called the default argument promotions. No other conversions are performed implicitly
- DSMan195276 1y agoIt's a difference of whether the function arguments are declared or not. If you declare a `void foo()`, and then call `foo((float)f)`, the `foo()` function is actually passed a `double` as the first argument rather than a `float`. If you instead change the declaration to `void foo(float)` then it gets passed as a `float`. Ex: https://godbolt.org/z/TKjz3Tqqr https://godbolt.org/z/TKjz3Tqqr
- billforsternz 1y agoA misconception on my part, apologies. I had somehow conflated the automatic conversion from float to double that occurs in some circumstances to something that occurs in all circumstances. Or perhaps taken a quirk from a 1980s compiler (Turbo C maybe?) to be standard behaviour. Or something like that.
- deleted 1y ago[deleted]