Thread: Math woes

  1. #1
    Registered User
    Join Date
    Aug 2001

    Question Math woes(please help)

    I need a function that calculates x to the xth power, but that prints out the whole answer. i.e. 12^12=8916100448256. Using pow() only allows for so many digits to be shown, and then it kicks in using scientific notation. Hopefully, there is a way to get every digit in the answer to be output.

    Any insights?
    Last edited by TheUrbanChico; 08-31-2001 at 07:26 PM.

  2. #2
    Registered User
    Join Date
    Aug 2001

    exponential calculation

    You can store the values into an array to prevent overflow. Which requires some fun logic. but for shorter versions this should do (+ or - the off by one margin)

    cin >> nBase; //lets say nBase is 10
    cin >> nPower; //lets say nPower is 3 .... 10 ^ 3 = 1000

    nResult = 1;

    for(i =1;i<=nPower;i++)
    nResult *= nBase;//1st loop nResult = 1 * 10
    //2nd loop nResult = 10 * 10
    //3rd loop nResult = 100 * 10


    if(nPower == 0)
    nResult = 1; //any number to the 0 power is 1

    cout << nResult;

    this is only pseudo code ... I have not executed this code but with a few fixes the logic will work

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    just use pow() from math.h or cmath. Its there provided for you.... why write another???
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :-

  4. #4
    Join Date
    Aug 2001
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    You can either use a 'double' or else a 'float' with pow(). Use the double if you want more decimal places. It this is not enough than you need to get an I64 microprocessor, WinXP professional for 64-bit computers, and an appropriate motherboard. The I64 technology will allow you to work with much larger built in types.
    I compile code with:
    Visual Studio.NET beta2

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    If you've got MSVC then you can use __int64 to obtain more precision. As it's non-standard you'll have to write your own functions to work with it and I had to convert all values to a char before I could print them accurately -

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    int main()
        unsigned __int64 i=12;
        char f[64];
        int power = 12;
        for (int j=1;j<power;j++)
        cout << _i64toa(i,f,10)<<endl;
        return 0;
    other compilers may offer their own 64 bit int.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Borland C++ (and C++Builder) also have __int64's (if that is the compiler you use). Also, I would recommend using unsigned __int64's to get a little more precision.

    int main()
        bool negative = false;
        __int64 a;
        int b;
        unsigned __int64 result;
        // Get input for a and b;
        if(a < 0 && b % 2 != 0)
            negative = true;
        result = abs(a); /* I don't think abs() is actually defined for __int64's, but it isn't hard to write one. */
        for(int j = 1; j < b; j++) result *= abs(a);
            cout << "-";
        cout << result;

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > calculates x to the xth power, but that prints out the whole answer. i.e. 12^12=8916100448256
    Depends how big your number can get. floats / doubles / int64 will only take you so far (actually floats are worse than int) before you run out of bits to accurately represent the number (and hence accurately print the number), before you have to resort to using something like this
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of
    Replies: 16
    Last Post: 04-01-2009, 05:36 PM
  2. Help with C++ Math
    By aonic in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2005, 04:40 AM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  4. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 06:39 AM
  5. toughest math course
    By axon in forum A Brief History of
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM