2 to the power 5 (SIMPLE)

This is a discussion on 2 to the power 5 (SIMPLE) within the C Programming forums, part of the General Programming Boards category; Here is some very simple code, which will not compile, can anyone tell me what the problem is and how ...

  1. #1
    Unregistered
    Guest

    Question 2 to the power 5 (SIMPLE)

    Here is some very simple code, which will not compile, can anyone tell me what the problem is and how to fix it?

    #include <stdio.h>

    int main()
    {
    float test;

    test = 2;
    test = test^5; /* This line is the problem */

    printf("2^5 = %f\n", test);

    return 0;
    }

    When I try to compile this using gcc I get the error:
    test.c: In function 'main':
    test.c:8: invalid operands to binary ^

    Thankyou for helping

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    The ^-operator is not the power operator. As far as i know, C doesn't have such an operator. So you'll have to implement a function by your self.

    For example:

    /* z = x ^ y = e ^ (y * ln (x)) */
    z = exp (y * log (x));

    Or:

    int pow (int x, int y)
    {
    if (y == 1)
    return x;
    else
    return (x * pow (x, y - 1));
    }

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    If you want to use "pow();" function you must use the "math.h" library.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    Unregistered
    Guest

    Smile

    Thankyou

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 07:42 AM
  2. No Power, but power being supplied.
    By jrahhali in forum Tech Board
    Replies: 6
    Last Post: 08-11-2005, 02:50 AM
  3. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM
  4. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 12:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21