EmbeddedRelated.com
Forums
Memfault Beyond the Launch

Codewarrior - IEEE32, float, double and sprintf()

Started by codewarr2000 December 13, 2004
I require a floating point number with at least 12 decimal places.
Using Codewarrior HCS12, I turn on -Cf for IEEE32 doubles.

I am using Banked memory model.

sprintf(&buffer[0],"blahblah",blah) does not display correctly any
floats, ints, doubles when used through TERM_WriteString(buffer);

Is it just sprintf() and the internal data is ok, or is the data bad?
Is there something else I need to do?
Is there another library I need to use?

Thanks in-advance.
Pre-Note: -Cf enables IEEE64 doubles (but I think this is just a typo on
your side?)

- Did you link the correct library using (see as well
\lib\HC12c\readme.txt)?
You need to make sure that you link the support/ansi library as well with
-Cf set.
In your case this would be probably ansibf.lib.

- Turn on compiler option -Wpd (which flags implicit parameter declarations
with an error).
Maybe you use sprintf() without the proper prototype/header file included.

Erich
Yes I did mean IEEE64.

So I needed ansibf.lib. -- done.
-Cf -- done

Running pure C, not C++.

#include <float.h>
#include <stdio.h>

This is the code:
float a;
double b;

a.012345;
b.012345678901; // 12 decimal

TERM_WriteString("Test Floats and Doubles:\n");
sprintf(&printBuffer[0],"DBL_DIG=%d\n", DBL_DIG);
TERM_WriteString(printBuffer);

sprintf(&printBuffer[0],"float a=%2.6f double b=%2.6f\n",a,b);
TERM_WriteString(printBuffer);
sprintf(&printBuffer[0],"float a=%2.6f double b=%2.12g\n",a,b);
TERM_WriteString(printBuffer);
sprintf(&printBuffer[0],"float a=%2.6f double b=%g\n",a,b);
TERM_WriteString(printBuffer);



This is the output to the serial post terminal:

Test Floats and Doubles:
DBL_DIG
float a.012345 double b.012346
float a.012345 double b.01234572
float a.012345 double b.0123

What is going wrong?

Thanks again in-advance.

Memfault Beyond the Launch