Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Strange problem with sprintf function in CCS4 compiler

Status
Not open for further replies.

scorrpeio

Full Member level 5
Joined
Dec 26, 2006
Messages
286
Helped
10
Reputation
20
Reaction score
9
Trophy points
1,298
Activity points
3,496
Hello,

I have variables in float which I am interested in converting to char array.
For this I used sprintf function and enabled full support to library functions in Code Composer Studio 4.

Code:
//global declaration
// Struct_Parameters_RunMode.f32TotalisedFlow is a float type in a structure
UINT8_T  G_u8TotalisedFlow_ASCII[13]	= {0,0,0,0,0,0,0,0,0,0,0,0,0};
UINT8_T   G_u8Size_Totalised		= 0;

Struct_Parameters_RunMode.f32TotalisedFlow = 1234506789;
	sprintf( (UINT8_T*)G_u8TotalisedFlow_ASCII, "%.0f", Struct_Parameters_RunMode.f32TotalisedFlow );
	while(G_u8TotalisedFlow_ASCII[G_u8Size_Totalised]!= DEF_NULL )
	{
	 	G_u8Size_Totalised++;
	}

when I see the output of this piece of code
I see like this ...
Code:
G_u8TotalisedFlow_ASCII[13]	= {'1','2','3','4','5','0','6','7','9','2'};

I dont understand why is this happening.
If I give a value less than 8 digits (12345678) I get desired output.

I have made stack size 1000bytes and heap size 960 bytes. I think, this is sufficient for sprintf function.

Please let me know, how to resolve this issue.
 

I guess you'll want to think about resolution of float variables in terms of exact decimal digits.
 

Actually, if no of digits in float variable exceeds 8 (i.e. value greater than 9999999) sprintf can't retrieve the accurate char array equivalent to that float value.
That is where I am looking into as why would this happen?

- - - Updated - - -

1234506789 is what I am passing to sprintf and 1234506792 is what I am retrieving in char array.
 

The highest number that can be represented in float without possible truncation errors is 2^24 - 1. Above this limit, some numbers may be still correct by chance.
 
Okay....
this explains why am I getting an erroneous value.
So, I will have to write my own "number to char" function.
Any reference of how to convert float value to char?
 

hi,

before sprintf all numbers as converted to string then you can receive as string.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top