Continue to Site

How can I convert INT A into a floating in C programming ?

Status
Not open for further replies.

glenjoy

Banned
Advanced Member level 3
Joined
Jan 1, 2004
Messages
962
Helped
72
Reputation
146
Reaction score
20
Trophy points
1,298
Location
Philippines
Activity points
0
C programming problem

Hi,

I have this problem, the result of my calculation is of variable type INT A.

Now I will use it to solve a certain problem and store the result in FLOAT B.

The calculation requires that INT A will be raised into power, say A raise to the 5 + A raise 4 + A raise to the 3.

I read the C manual and find out that to raise INT A to a certain power I will need the pow(x,y) function. And to use pow(x,y) Both x and y must be of floating type.

Now how can I convert INT A into a floating type?

Is there any other way to raise a certain variable to a certain power without using pow(x,y)?

Thanks.
 

Re: C programming problem

glenjoy said:
Hi,

I have this problem, the result of my calculation is of variable type INT A.

Now I will use it to solve a certain problem and store the result in FLOAT B.

The calculation requires that INT A will be raised into power, say A raise to the 5 + A raise 4 + A raise to the 3.

I read the C manual and find out that to raise INT A to a certain power I will need the pow(x,y) function. And to use pow(x,y) Both x and y must be of floating type.

Now how can I convert INT A into a floating type?

Is there any other way to raise a certain variable to a certain power without using pow(x,y)?

Thanks.

Code:
calc()
{
  int a;
  float fa,b;

  a = .......
  fa = (float) a;

  b = pow(fa,5.0) + pow(fa,4.0) + pow(fa,3.0);
  //Or b=pow((float)a,5.0) + .....
}

/Bingo
 

C programming problem

Does not work, :(, any other way to raise to a certain power?
 

Re: C programming problem

glenjoy said:
Does not work, :(, any other way to raise to a certain power?

Which compiler are we talking about??

The example Bingo600 gave seems to be OK for me ...

best regards
 

Re: C programming problem

glenjoy said:
I am usign CCS C.

Should be supported:
POW( )

Syntax:
f = pow (x,y)

Parameters:
x and y and of type float

Returns:
A float

Function:
Calculates X to the Y power.

You can also post your question at the CCS forum:
https://www.ccsinfo.com/forum

best regards
 

C programming problem

It seems you are trying to use fixed-point arithmetic. First of all forget about pow() function, you result will overflow. Simple way to implement power function is actually to use series of multiplications. Secondly you have to use Q1.16 format for your input numbers and use fractional multiplication. Search Google for “fixed point” math and “fixed point multiplication”.
Cheers.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top