Thread: Pow function

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    114

    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?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    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

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Quote Originally Posted by DeadPlanet View Post
    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;
    }

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    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).

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by DeadPlanet View Post
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by DeadPlanet View Post
    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).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Duly noted.

    I think I'll refrain from commenting next time I'm not so confident in my answer.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by grumpy View Post
    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.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elkvis View Post
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  2. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  3. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM