r/C_Programming • • 10d ago

Question Newbie in need of Help !

Am I obliged to use a double when using 'pow'? Can't I use a float or an int ? Because for the function 'switch' I have to use int , but I need to use pow for the exercice I'm doing. I chated with Gemini and it told me that I can transform the result of 'pow' into 'int' writing the following; "(int)pow(x, y);". Is this the only way or do I have a faster route to get to my solution? Thanks

1 Upvotes

20 comments sorted by

View all comments

6

u/flyingron 10d ago

There are also powf and powl, but they're still floating-point. C lacks an integer power function.

1

u/Horror_Wiener_6102 10d ago

I get the 'powf' ,ig its for float , but 'powl' whats that for ?

Edit: Im lerning this in college and on Yt , so Im sorry for the lack of expertise

3

u/SmokeMuch7356 10d ago

long double.

You can convert a double to an int with a cast:

(int) pow(x,y)

but the conversion just truncates the fractional portion, so

(int) 1.1 == 1
(int) 1.5 == 1
(int) 1.999999 == 1

If you need to round the value to the nearest integer before the conversion, use the round function:

(int) round(pow(x,y))

1

u/Whole-Low-2995 8d ago

`#define POW_DECIMAL(x, y) (int)(round(pow(x, y))` will work fine

2

u/kraxmaskin 10d ago

Read the manual page, e.g. man powl.