Thread: Exponents??

  1. #1
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79

    Exponents??

    Pardon the "n00b" question, but if there's a sqrt() function in <math> then why the heck doesn't
    Code:
    std::cout<<i^2;
    give me i squared? (I know, ^ is some kind of logical operator.)

    Where are the dang exponents?
    Code:
    #include <iostream>
    
    int main ()
    {
    int i = 1;
    while (i <= 10)
    
        {
        std::cout<<i*i<<std::endl;
        ++i;
    
    
        }
        
    system("PAUSE");
    return 0;
    }
    I've googled and searched the site but I'm sure you guys can answer this in far less time that it will take me to flounder around.

    Thanks!

    -JM

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    ^ is the bitwise "xor" operator. try using the pow( ) function from <cmath>.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    Perfect! Thanks!

    Code:
    #include <iostream>
    #include <cmath>
    
    int main ()
    {
    int i = 0;
    while (i <= 16)
    
        {
        std::cout<<pow(2,i)<<std::endl;
        ++i;
        }
        
    system("PAUSE");
    return 0;
    }

  4. #4
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    if you want to make a power function yourself:
    Code:
    double power(double base, double exp)
    {
        if(exp == 1)
            return base;
        if(exp == 0)
            return 0;
        else
           return base * power(base, exp - 1);
    }

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Except if exp is zero you should return 1...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yeah, and the thing will overflow the stack for any non-integral or negative exponent.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exponents are LAAAME
    By Sm33zy in forum C Programming
    Replies: 8
    Last Post: 11-21-2008, 10:10 PM
  2. Fraction Exponents
    By ninjaturtle[k9] in forum C++ Programming
    Replies: 4
    Last Post: 10-18-2004, 10:46 AM
  3. God, i feel like a n00b again. How do i do exponents?
    By Inquirer in forum C++ Programming
    Replies: 13
    Last Post: 09-01-2003, 08:41 PM
  4. how to use exponents
    By guitargatler in forum C++ Programming
    Replies: 11
    Last Post: 02-01-2003, 09:09 PM
  5. For loop and exponents
    By TrazPFloyd in forum C++ Programming
    Replies: 5
    Last Post: 10-15-2002, 05:19 AM