Could you show more of the code? Specifically the actual declarations for the variables would be helpful. I find no such issue with this minimal code sample.
#include <stdio.h>
int main(void)
{
float ovrh = 1.0F, wr = 1.0F, z = 1.0F, Fees = ovrh + (wr * z);
printf("Fees = %g\n", Fees);
return 0;
}
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Slight nitpick:
The calculation of two floats yields a double.
fees = 1.0 * 2.0;
Those aren't twofloats (1.0 and 2.0), they're two doubles.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Aha! So your original statement was not entirely accurate.Fees, ovrhd, wr and z are all declared as float.Fees is declared as an array of an array of float -- this is not the same and your compiler was kindly informing you of this. A single float might be Fees[0][0]. Try this (I'm assuming i and j are loop indices -- except that then they must be integral types and not floating point, but I'm used to i and j as loop indices -- and this is inside these loops.
Fees[i][j] = ovrh + (wr * z);
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Ah HAH! As I suspected.
Dave, you are right that '1.0' is a double. However, I still believe that two floats used in a calculation result in a double, in the same way that two chars used in a calculation result in an int. In any case, the compiler can deal with that and generally puts out a warning, not an error.
Actually, two chars used in a calculation result in a char, but there may be overflow. Calculations stay withing their type. But, yes. The reason for the 'double' part of the diagnostic was with this.
const <strong>double</strong> ovrh = 2.27;
Multiplying two floats results in a float; but adding that result to a double promotes the float result to a double. And yes, attempting to then assign this double result to a float should generate a warning.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Oops. I didn't read the comment.// I tried to enter this as a const float, but the compiler didn't like it, so I changed it to a double which solved that one problemIt makes me curious --tnorton: are you saying that the compiler barked about something like this?
const float ovrh = 2.27F;
But the other stuff about the promotions should be correct.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314