printf and float

for everything else
Post Reply
jediengineer
Posts: 192
Joined: Mon Dec 17, 2012 6:24 am

printf and float

Post by jediengineer »

Not sure if this topic has been discussed before or not. I'm trying to output a double precision value to MTTTY. I'm getting weird numbers...

Code: Select all

volatile double numberz[16]
numberz[0] = 5.01021;
numberz[1] = 3.33205
/// etc up to index 15

for (int i = 0; i < 16; i++) printf("Output: %2.6f\r\n", numberz[i]);
The output I'm getting at the terminal screen is:
Output: 21887.000000
Output: 14449.000000

What am I doing wrong?
User avatar
pbreed
Posts: 1088
Joined: Thu Apr 24, 2008 3:58 pm

Re: printf and float

Post by pbreed »

your example code is missing some ; so I assume cut and paste errors...

I ran the following code and got the correct result....

#include "predef.h"
#include <stdio.h>
#include <ctype.h>
#include <startnet.h>
#include <autoupdate.h>
#include <dhcpclient.h>
#include <smarttrap.h>
#include <taskmon.h>


extern "C" {
void UserMain(void * pd);
}


const char * AppName="FloatTest";

void UserMain(void * pd)
{
InitializeStack();/* Setup the TCP/IP stack buffers etc.. */

GetDHCPAddressIfNecessary();/*Get a DHCP address if needed*/
/*You may want to add a check for the return value from this function*/
/*See the function definition in \nburn\include\dhcpclient.h*/

OSChangePrio(MAIN_PRIO);/* Change our priority from highest to something in the middle */

EnableAutoUpdate();/* Enable the ability to update code over the network */

EnableSmartTraps();/* Enable the smart reporting of traps and faults */

EnableTaskMonitor();/*Enable the Task scan utility */


iprintf("Application started\n");

volatile double numberz[16];
numberz[0] = 5.01021;
numberz[1] = 3.33205;
numberz[2] = 23.33205;
numberz[3] = 33.33205;
numberz[4] = 43.33205;
numberz[5] = 53.33205;
numberz[6] = 63.33205;
numberz[7] = 73.33205;
numberz[8] = 83.33205;
numberz[9] = 93.33205;
numberz[10] = 103.33205;
numberz[11] = 113.33205;
numberz[12] = 123.33205;
numberz[13] = 133.33205;
numberz[14] = 143.33205;
numberz[15] = 153.33205;

for (int i = 0; i < 16; i++)
printf("Output: %2.6f\r\n", numberz);


while (1)
{
OSTimeDly(TICKS_PER_SECOND * 1);
}
}
jediengineer
Posts: 192
Joined: Mon Dec 17, 2012 6:24 am

Re: printf and float

Post by jediengineer »

Thanks Paul,

I must have had some form of typo in there. I deleted and retyped it and it worked. Thanks!
Post Reply