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.

[SOLVED] arithmetic operation in C (2 decimals)

Status
Not open for further replies.

hamsiii

Member level 2
Joined
Apr 21, 2011
Messages
42
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,595
Hi all,
I have a problem of acquiring ADC data. The data should be 562 and constant, stored in ADC (10bit) memory.
To calculate the exact voltage (which is 2.74V), the operation is;
Code:
float x = (ADC_Result*60)/1022;
float y = (x*5)/60;
However, at this point, I get 32 for x and 0 for y as I should get 32.99... for x and 2.74... for y. Dividing operators do not provide me any decimals and they round the number down. Would please help me to avoid this and get the exact numbers?

I would not use printf, instead I will send the data into LCD. So, the %.2f function will not be any use for me. So, the data will be stored in a specifier like float or int.
 

You can use typecasting, usually typecasting one variable will do all the calculations in the provided type or you can typecast the other numbers too.

Code:
float x = ((float)ADC_Result*60)/1022;
float y = ((float)x*5)/60;

I think it will even work if you add a decimal but I would suggest the typecasting method to be in control

Code:
float x = (ADC_Result*60.0)/1022;
float y = (x*5.0)/60;

Alex
 
Have you tried putting the decimal points in?

float x = (ADC_Result * 60.0) / 1022.0;
float y = (x * 5.0) / 60.0;
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top