# Pow function

• 02-12-2013
Pow function
I am trying to find the nth root of a function.This is my code
Code:

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
double a=625,b=1/2;

double x=pow(a,b);
cout<<x;
}

here its returning x=1. that is though I have made b double, the function is taking it as an integer. Why is that?
• 02-12-2013
1 and 2 are both integers. integer division yields an integer. the result is promoted to a double after the integer division is performed.

One of these should work.
float a = 0.5;
float a = 1f / 2f; // f when added to a literal makes it floating point
• 02-12-2013
Quote:

1 and 2 are both integers. integer division yields an integer. the result is promoted to a double after the integer division is performed.

One of these should work.
float a = 0.5;
float a = 1f / 2f; // f when added to a literal makes it floating point

I have tried this. The 1st expression "float a=.5" works. but I need the 2nd type.When I use 1f/2f it says unable to find literal operator. my code is
Code:

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float a=1f/2f;
double x=pow(25,a);
cout<<x<<endl;
}

• 02-12-2013
Apologies, proves how little I know.

I believe that f actually is used to promote a double to a float. You just need to use a = 1.0/2.0; (or add f if you want to use floats).
• 02-12-2013
std10093
Code:

b=1.0/2;
What is happening is that you divide two integers, so the result is an integer. The result takes the type of the stronger type of the members of the mathematical procedure.

So, I divided a float (1.0) with an integer, so the result will be a float.
If I divided 1.0 with 2.0, the result, of course, would be a float number again.

// I used the word float as the float numbers, rather than the type of variables.
• 02-12-2013
Elysia
Quote:

1 and 2 are both integers. integer division yields an integer. the result is promoted to a double after the integer division is performed.

One of these should work.
float a = 0.5;
float a = 1f / 2f; // f when added to a literal makes it floating point

f only works on "doubles", so

float a = 1.0f / 2;
float a = 1 / 2.0f;

are both correct. Leaving out the ".0" is not.

Oh, and both operands do not need to be a floating point. Just one is enough. Both works too, of course.
• 02-12-2013
whiteflags
Well, just to be complete:

F can be a digit in a base 16 integer:
0xA1B2C3D4E5F6ULL

F can be a suffix:
3.14F

But it must be one of these, and it's legal if it occurs in these formats.
• 02-13-2013
grumpy
Quote:

I believe that f actually is used to promote a double to a float.

Your belief is mistaken. The 'f' is a convention that informs the compiler the literal is of type float. There is no type conversion required.

And, even it there was a type conversion, the conversion from double to float is not a promotion. A promotion is a type conversion to a larger type that doesn't lose precision. So conversion from float to double is a promotion (as a double can represent all values that a float can) but the reverse is not true (conversion from double to float can lose precision).
• 02-13-2013
Duly noted.

I think I'll refrain from commenting next time I'm not so confident in my answer.
• 02-13-2013
Elkvis
Quote:

Originally Posted by grumpy
The 'f' is a convention that informs the compiler the literal is of type float.

just a small note to add to this: if I'm not mistaken, all floating point literals in C and C++ are considered to be of type double, unless otherwise qualified.
• 02-14-2013
grumpy
Quote:

Originally Posted by Elkvis
just a small note to add to this: if I'm not mistaken, all floating point literals in C and C++ are considered to be of type double, unless otherwise qualified.

You're certainly not mistaken.

Just to hammer the point of my previous post, that doesn't mean that the 'f' causes type conversion. The compiler is not required to store the value in a double, and then convert it to a float. So
Code:

float x = 1.0f;
is certainly not required to produce a double (whether in the implementation of the compiler itself, or in code it emits) with value 1.0, and then convert it to be of type float before storing it in x.