CW6.3 printf floating point

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

CW6.3 printf floating point

5,055 Views
tartiz
Contributor I
I'm using CW6.3 with M52233DEMO board.
I try to use floating point.
It is OK for calculates but not for printf (?).
 
The followings source code  ( wheare x=1038.110) :
 
printf(sf,"%5.2f\n\r%6.3f\n\r%7.4f\n\r", x, x, x);
 
produce the following output on the uart debug console :
 
%5.2f
%5.2f
%6.3f
%7.4f
 
Why ?
 
 
 
 
Labels (1)
0 Kudos
Reply
7 Replies

1,105 Views
J2MEJediMaster
Specialist I
Your printf statement begins with a variable (?) "sf," rather than the control string that describes the formatting to be used. The control string should be the first argument in printf(), not the second one, as it is in your example.

---Tom
0 Kudos
Reply

1,105 Views
tartiz
Contributor I
I have failed the source line .
I'm using the M52233DEMO board and the CodeWarrior 6.3 Special edition.
 
Now there is the correct code for printf test and the output on the UART console.
.
 
/*
 * File:  main.c
 * Purpose:  sample program
 *
 */

#include <stdio.h>
 
char sf[80];
 int i = 25;
   char c = 'M';
   short int d = 'm';
   static char s[] = "Metrowerks!";
   static char pas[] = "\pMetrowerks again!";
   float f = 3.1415926;
   double x = 3.1415926;
   int count;
   int fi;
   int xi;
   float input_f;
  
int main()
{
int count;
  
    printf("\n\r---------------------------------------\n\r");
    printf("Hello World 2 in C\n\r");
 
 fflush(stdout);
 
  
   printf("\n\r\n\r");
   printf("%s printf() demonstration:\n\r%n", s, &count);
   printf("The last line contained %d characters\n\r",count);
   printf("Pascal string output: %#20s\n\r", pas);
   printf("%-4d 0x%x 0x%06x %-5o\n\r", i, i, i, i);
   printf("%*d\n", 5, i);
   printf("%4c %4u %4.10d\n\r", c, c, c);
   printf("%4c %4hu %3.10hd\n\r", d, d, d);
   printf("$%5.2f\n\r", f);
  
   fi=(int)(f*100000000.);
   xi=(int)(x*100000000.);
   printf("fi  (int)(x*1000000000.)=%d\n\r",fi);
   printf("xi  (int)(x*1000000000.)=%d\n\r",xi);
 printf("input float : ");
 scanf("%f",&input_f);
    printf("\n\r  inputf*1000=%d\n\r",(int)(input_f*1000.));
 fflush(stdout);
 while(1); // Idle
 
 return 0;
}

---------------------------------------
Hello World 2 in C

Metrowerks! printf() demonstration:
The last line contained 37 characters
Pascal string output:    Metrowerks again!
25   0x19 0x000019 31
   25
        M   77 0000000077
   m  109 0000000109
$%5.2f
fi  (int)(x*1000000000.)=314159250
xi  (int)(x*1000000000.)=314159260
input float :
  inputf*1000=0
The red line is wrong : should be 3.14
 
And  the scanf  also  is'nt OK :  should be waiting for input from UART console.
 
0 Kudos
Reply

1,105 Views
J2MEJediMaster
Specialist I
Take a close look at the printf() statement that generates that red line. What's that "$" character doing in there, at the start of the control string?

Sorry, I don't know what could be going wrong with the scanf(). Have you tried any of the code examples that might have a scanf() in it?

---Tom
0 Kudos
Reply

1,105 Views
CompilerGuru
NXP Employee
NXP Employee
my guess (I'm not so familiar with CF), you are using a non floating point version of the library.
One of the largest parts of printf is its floating point support, therefore I can imagine (not sure for CF) that there is a library with floating point and one without floating point. Which library do you use?
I think it's important not to get the floating point (size) overhead unless you really use it.

Daniel
0 Kudos
Reply

1,105 Views
tartiz
Contributor I
I'm using the default libraries included in the project when I have selected  M52235EVB stationery and  C.
0 Kudos
Reply

1,105 Views
CompilerGuru
NXP Employee
NXP Employee
The printf out of the library used in the statinery project does not support to print floating point numbers.
Change it to another library, see the
Help\PDF\Targeting_ColdFire.pdf
manual for details what the library filenames do mean. This manual also explains that the printf support for the size optimized libs does not support floating points.

For example, for the Console Debug build target, use
C_TRK_4i_CF_MSL.a
instead of the size optimized
C_TRK_4i_CF_SZ_MSL.a
library.

Note that my little demo app got about 3 times bigger when switching to the non size optimized library.

Daniel
0 Kudos
Reply

1,105 Views
tartiz
Contributor I
I verified : OK!  for Console debug.
Tanks very much.
Tiziano
0 Kudos
Reply