This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Strange Float Behaviour

Guys,

I am having a problem with float division using PK51. I was wondering if anyone could explain this or has seen it before.

I have a float, and want to divide it by 36000. If I do this directly, I get strange results, even -ve numbers although all the variables are +ve.

accrued_seconds =(total_accrued_seconds/36000);

Both variables are floats, however, if I do the following

accrued_seconds = (unsigned long)(total_accrued_seconds/10);

accrued_hours = (float)(accrued_seconds/3600);

I get the correct result. For some reason that I don't understand using too big a divisor in the first code causes an error but not a two step as in the second. I have also tried the two step without the cast to an unsigned long and I get the errored result.

I tired the same thing on GCC on a PC and of course it works fine.

Any clues anyone?

Cheers,

Dirk

Parents
  • "And using a negative integer value instead of switching to an unsigned constant should merrit a warning."

    In the case of C51 with 16-bit int and 32-bit long, if it does in fact use the integer value -29536 for for the constant 36000, then it is not abiding by the standard, which says it should have treated 36000 as 'long int'. "Switching" to an unsigned type should not be an option in this case.

Reply
  • "And using a negative integer value instead of switching to an unsigned constant should merrit a warning."

    In the case of C51 with 16-bit int and 32-bit long, if it does in fact use the integer value -29536 for for the constant 36000, then it is not abiding by the standard, which says it should have treated 36000 as 'long int'. "Switching" to an unsigned type should not be an option in this case.

Children