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] How to perform equation calculations in Embedded C

Status
Not open for further replies.

DivyaKrishnappa

Newbie level 6
Joined
Apr 23, 2014
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
103
Hi..

Gain = 0.506585(Float value)
Avalue = 00001120(unsigned long value)
Bvalue = 0000096A(unsigned long value)
Result(unsigned long value)
My equation is
Result = Gain * (Avalue - Bvalue)
How to write the program for this equation in C..? I know this is very simple.. Though I am facing problem in it.. Help me to solve this..?
 

cast avalue and bvalue as float, then cast Gain * (Avalue - Bvalue) as unsigned long
 

A little detailed DIY explanation.

I'm assuming you have some background in some of the 'C' standards and you are using a standard compliant compiler that could work with floating point numbers. I'm also assuming you are not looking for ignoring the fraction from the result completely - like floor().

Having said that, the result of a floating point conversion normally cannot be a 'unsigned long', at least without conversion. So, I would do something like considering the number of significant digits in the fraction from the floating point result and ignore the rest while converting it into 'unsigned long'. Now, the considered fraction digits could be from binary, decimal or hexadecimal formats.

As an example, assuming gain is in decimal and rest are hexadecimal, the result of your equation is:

0x3E7.FFB0B36BC == 999.998789991 decimal.

Depending upon my choice of retaining 4 hex digits from fraction, my result in 'unsigned long' would be:

0x3E7FFB0 = 65535920 decimal.

Now it would be a little different if I would like to choose 4 decimal digits instead of four hex digits - I would get 999.9987. But I think you get the idea.

Also note, some compilers use 24-bit floating point format. You might need to read your compiler documentation. Other useful place is Wikipedia: https://en.wikipedia.org/wiki/IEEE_floating_point
 

The "result" variable is of type unsigned long value ,as per your equation ,it will loose the floating point value i.e.. truncate at decimal point.Are you sure ,this is what you want to do?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top