In C++, sin
has an overload float sin(float f)
. And overload resolution is done on argument type, rather than return type. To force the use of double sin(double d)
you need to cast the argument: sin(static_cast<double>(x))
.
(2) vs (3): the FP standard allows implementations to store intermediate results with greater accuracy than the final result. So the value of s
need not be exactly the same as the intermediate result for sin(f)
in (3).
A lot of this is dependent on your compiler, compiler settings and hardware. For example, if I run your code on my system I get:
( 1) x = 5.6094117463e-02
( 2) x - s = 2.9411166906e-05
( 3) x - sinf(x) = 2.9411166906e-05
( 4) x - float(sinf(x)) = 2.9411166906e-05
( 5) float(x) - float(sinf(x)) = 2.9411166906e-05
( 6) x - sin(x) = 2.9412529899e-05
( 7) s = 5.6064706296e-02
( 8) sinf(x) = 5.6064706296e-02
( 9) sin(x) = 5.6064704933e-02
(10) float(sinf(x)) = 5.6064706296e-02
(20) s_df = 5.6064706296e-02
(21) s_dd = 5.6064704933e-02
(22) s_fd = 5.6064706296e-02
(23) s_ff = 5.6064706296e-02