How to represent interger powers in C++?

Discussion in 'C++' started by heng, Dec 6, 2006.

  1. heng

    heng Guest

    such as 3^4 ?
     
    heng, Dec 6, 2006
    #1
    1. Advertising

  2. I believe there to be no such representation.
    Try:

    #include <cmath>
    float result = pow(3.0, 4.0)

    Joseph.
     
    Joseph Paterson, Dec 6, 2006
    #2
    1. Advertising

  3. heng

    Daniel T. Guest

    "heng" <> wrote:

    > such as 3^4 ?


    /* pow example */
    #include <cstdio>
    #include <cmath>

    using namespace std;

    int main ()
    {
    cout << "7 ^ 3 = " << pow( 7, 3 ) << '\n';
    cout << "4.73 ^ 12 = " << pow( 4.73, 12 ) << '\n';
    cout << "32.01 ^ 1.54 = " << pow( 32.01, 1.54 ) << '\n';
    }

    --
    To send me email, put "sheltie" in the subject.
     
    Daniel T., Dec 6, 2006
    #3
  4. heng wrote:

    > such as 3^4 ?

    Option 1) write your own function. (but overloading won't work for
    builtins.)
    Option 2) Post to comp.std.c++ and suggest it be added.
    Option 3) Use the morally unsound conversion to and from floating point
    functionality provided with the Standard Library.

    There are two other options, but they amount to impractical academic
    exercises. These are to use Cpp meta-programming or template
    meta-programming.
    --
    NOUN:1. Money or property bequeathed to another by will. 2. Something handed
    down from an ancestor or a predecessor or from the past: a legacy of
    religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
    from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
     
    Steven T. Hatton, Dec 6, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Cannot be converted to 'Interger'

    =?Utf-8?B?VGltOjouLg==?=, Nov 3, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,701
    chanmmn
    Nov 3, 2004
  2. uvbaz
    Replies:
    6
    Views:
    7,721
    uvbaz
    Sep 12, 2006
  3. I_have_nothing

    Any easy to printf an interger in 9,999, 99 format?

    I_have_nothing, May 13, 2005, in forum: C Programming
    Replies:
    4
    Views:
    426
    Chris McDonald
    May 13, 2005
  4. Tim Slattery
    Replies:
    2
    Views:
    435
    Pete C
    Jan 6, 2007
  5. KG

    reverse the bits in an interger?

    KG, Jun 20, 2007, in forum: C Programming
    Replies:
    40
    Views:
    1,335
    Christopher Benson-Manica
    Jun 21, 2007
Loading...

Share This Page