Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 4 months ago.
Float precision and implicit conversion, why?
Hello all,
I'm seeing some warnings that i don't fully understand.
I have Pi and a constant float defined as follows:
#ifndef M_PI #define M_PI 3.1415927 #endif const float COXA_ANGLE = M_PI/4.0; //45deg; // Angle of coxa from straight forward (deg)
If i use:
leg[1].footPosCalc.x = tempFootPosY[1]*cos(COXA_ANGLE*2.0) - tempFootPosX[1]*sin(COXA_ANGLE*2.0);
I get warnings like: Warning: Single-precision operand implicitly converted to double-precision in "ik.cpp", Line: 147, Col: 41
If i use:
leg[1].footPosCalc.x = tempFootPosY[1]*cos(COXA_ANGLE*2) - tempFootPosX[1]*sin(COXA_ANGLE*2);
I get no warnings.
I understand that in the first case i'm using 2.0 which is a float, but since its being multiplied against a float, isn't the result another float?
In the multiply by integer '2' case, isn't the two being cast into a float for the multiplication anyway?
In either case, why is it converting to a double precision float? I'm happy with the answer being single precision.
I'm using an LPC4088 and my understanding is that the FPU only works for 32bit single precision floats so i want to make sure i stay in that domain. Is this true and if so am I on the right track?
Thanks a lot, Dan
3 Answers
10 years, 4 months ago.
2.0 (and also your Pi) are not floats but doubles. And the compiler apparantly changes other things also to doubles, instead of the double to float, which gives that warning.
If you replace 2.0 by 2.0f, to explicitly state it is a float and not a double, I expect the warning to go away. ]
10 years, 4 months ago.
float literal (no suffix) defines double
http://en.cppreference.com/w/cpp/language/floating_literal