what to use for floating numbers in embedded C???

Status
Not open for further replies.

Nidhitrivedi

Junior Member level 2
Joined
Feb 6, 2012
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,413
Hello all,

In my program i have the ans as floating point value...
Then which command i have to use?? As we have int,char. Likewise floating number is define by what?

I used float but its not working....

Code:
unsigned int finalans;
float temp=0;

finalans=i*795;///final answer will be i*795/100;
temp=(i*795)/100;
//so suppose there are 5 digits to display then,
lcdcmd(0x80);
lcddata((temp/10000)+0x30);
[U][B]lcddata(((temp%1000)/10)+0x30);[/B][/U] (here the error is coming of bad operand)
lcddata(((temp%100)/10)+0x30);
lcddata((temp%10)+0x30);

lcddata function is to diplay the value on lcd.
lcdcmd is to display first character on lcd.

Thanks to all....
 
Last edited by a moderator:

Firstly, the specific error is generated because the "%" operator is undefined for float. You need to perform an explicite type cast before, either (long)temp % 1000 or (int)temp % 1000. The correct type depends on the number range of temp.

Secondly, temp=(i*795)/100 doesn't work if i has an int type and it's value is larger than 41. You should write i*7.95 instead.

Finally, it's a stupid idea to use float in this place, because you are performing pure integer arithmetic. Define temp as long and use a type cast in the conversion
temp=((long)i*795)/100;

A more convenient method for the decimal number display uses sprintf() by the way.
 
Thankyou so much it helped me....
 




Can you explain how to do this with the help of sprintf().
A small and basic example will be enough and better for me.

Thanks in advance
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…