Using type double in a program.

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

Using type double in a program.

314 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by IntStarFoo on Sat Dec 08 16:15:10 MST 2012
Hi There,  I have a problem using the type double in a program on LPC1769.  I can sort of demonstrate my issue using the code below... Can someone explain the results of this code?  The debugger appears to be showing it correctly but when it prints, it gives me unexpected results.

double doubleA = 0;
double doubleB = 0;
double doubleC = 0;
fprintf(stdout, "Using %%d: doubleA:%d, doubleB:%d, doubleC:%d\n", doubleA, doubleB, doubleC);
fprintf(stdout, "Using %%f: doubleA:%f, doubleB:%f, doubleC:%f\n", doubleA, doubleB, doubleC);
doubleA = 0xFF;
doubleB = 0xFF + 0xFF;
doubleC = 0xFF * 0xFF;
fprintf(stdout, "Using %%d: doubleA:%d, doubleB:%d, doubleC:%d\n", doubleA, doubleB, doubleC);
fprintf(stdout, "Using %%f: doubleA:%f, doubleB:%f, doubleC:%f\n", doubleA, doubleB, doubleC);


Produces These results...

Using %d: doubleA:0, doubleB:0, doubleC:0
Using %f: doubleA:, doubleB:, doubleC:
Using %d: doubleA:0, doubleB:1081073664, doubleC:0
Using %f: doubleA:, doubleB:, doubleC:
0 Kudos
Reply
2 Replies

298 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Sun Dec 09 05:47:33 MST 2012

Quote: IntStarFoo
I got it working, but I'm still not exactly sure what the problem was.  Here are my steps for getting it working...

1. Create a new project, add code, compile and run. (Works great.)
2. Compare project settings between my existing project and the new project and noticed a few things...
a. #include "stdio.h" in my was different than #include <stdio.h> so I changed that.



So you were using a local version of the stdio.h header file then? I suspect that the problem here is the your local header file does not actual declare fprintf in the same way that the C library actual defines it.

If you want to use the standard Redlib (or Newlib) C library functions, then you must be including the corresponding header files, not one from another source.

Regards,
CodeRedSupport
0 Kudos
Reply

298 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by IntStarFoo on Sat Dec 08 17:34:04 MST 2012
I got it working, but I'm still not exactly sure what the problem was.  Here are my steps for getting it working...

1. Create a new project, add code, compile and run. (Works great.)
2. Compare project settings between my existing project and the new project and noticed a few things...
a. #include "stdio.h" in my was different than #include <stdio.h> so I changed that.
b. Define order in the new project is:
__REDLIB__
DEBUG
__CODE_RED
__USE_CMSIS=CMSISv2p00_LPC17xx
Define order in old project was:
DEBUG
__CODE_RED
__USE_CMSIS=CMSISv2p00_LPC17xx
__REDLIB__

So I changed that to match

3.  Make changes to old project, compile and run.  (Works Great. - results below)

Using %d: doubleA:0, doubleB:0, doubleC:0
Using %f: doubleA:0.000000, doubleB:0.000000, doubleC:0.000000
Using %d: doubleA:0, doubleB:1081073664, doubleC:0
Using %f: doubleA:255.000000, doubleB:510.000000, doubleC:65025.000000

I'm guessing that somehow I was using some other version of some library that was giving unexpected results.  Either changing the define order or only looking in the C lib folders (via <stdio.h>) caught the right one.

Thanks,

IntStarFoo
0 Kudos
Reply