Floating point with CW for Coldfire v4.0.2.4 and MCF5272

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Floating point with CW for Coldfire v4.0.2.4 and MCF5272

2,686 Views
sbk
Contributor I
Hello, I'm using Codewarrior for Coldfire v4.0.2.4, targeting a MCF5272. My project is set to perform software floating point operations. I have a function that returns a float as follows:

float GetProcessorPeriodF() {
  float fPeriod=0;
  fPeriod = (float) 1 / 48;
  fPeriod *= 1000;
  return (float) fPeriod;
}

The function produces the right float number (20.83) but when it returns, the float variable that gets the value looks like this:
(fTest = GetProcessorPeriodF() fTest = 1.101442e+09

I don't know what's going on. I'll appreciate your help. Thank you, Martin


J2MEJediMaster added CW version and MCU type to header, cleaned up code


Message Edited by J2MEJediMaster on 2007-05-30 09:34 AM
Labels (1)
0 Kudos
4 Replies

346 Views
J2MEJediMaster
Specialist I
I don't know what's going on either, but I'd suggest making the compiler more aware that it's dealing with floating-point constants might help. Like so:


float GetProcessorPeriodF() {
   float fPeriod = 0.0F;
   fPeriod =  1.0F / 48.0F;
   fPeriod *= 1000.0F;
   return fPeriod;  // Not sure why you needed the typecast when the variable and return casting are
 }                         //   already declared float

---Tom

0 Kudos

346 Views
sbk
Contributor I
Hi Tom, thank you for the response. I have the function I shown in a file called: microp.c and the function prototype in the corresponding header file: microp.h It turns out that I was not including microp.h (#include "microp.h") in the file where I was calling the function (i.e.: main.c). Once I included the header file containing the prototype the variable receiving the return value started taking the right value. I should have included the header file to start with, but Codewarrior should detect that instead of just assigning/creating/?? a bad value for the return of the function. Martin
0 Kudos

346 Views
CrasyCat
Specialist III
Hello
 
According to the ANSI C standard CodeWarrior Compiler is generating a warning message when you are calling a function and there is no prototype defined for it.
 
This is a requirement from the C standard.
 
Note that you can ask the compiler to generate an error message instead of a warning in that case.
If you are building from the IDE, Check the "Require Function Prototypes" box in the "C/C++ Compiler" Panel.
 
If you are building from the command line add option -r to your compiler command line
 
I hope that helps.
 
CrasyCat
0 Kudos

346 Views
CompilerGuru
NXP Employee
NXP Employee
This really looks like the good old missing prototype bug.

Some math.
According to

http://babbage.cs.qc.edu/IEEE-754/Decimal.html

is the hex representation of 20.83
0x41A6A3D7 or decimal 1101439959.
Now display this with float precision and you get the "1.101442e+09"
Or in other words: Always use prototypes.

Daniel
0 Kudos