How the conversion is performed is compiler and CPU specific. With x86 CPUs with integrated FPU, the FPU is used to perform the conversion. To check this, you can generate an assembler output file when compiling:
fld QWORD PTR __real@3ff3ae147ae147ae
fstp QWORD PTR _d$[ebp]
fld QWORD PTR _d$[ebp]
fstp DWORD PTR _f$[ebp]
fld
loads a value and pushes it on the FPU stack.
fstp
pops the value from the stack and stores it in memory. The size of the memory pointer specifies the floating point type and the FPU performs the necessary conversion (the FPU uses internally a 80 bit extended format).
So the conversion from
double
to
float
will always give the same result for a specific
double
value here. It can be assumed that all x86 C/C++ compilers use these FPU commands to perform the conversion.
Regarding your function:
If the input value is of type
float
, you should do all calculations with
float
values if posssible (e.g. no polynomials of high order). This avoids problems with rounded values when comparing.