Math woes

This is a discussion on Math woes within the C++ Programming forums, part of the General Programming Boards category; I need a function that calculates x to the xth power, but that prints out the whole answer. i.e. 12^12=8916100448256. ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    3

    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 08:26 PM.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    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
    zMan

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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 :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    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
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    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 -

    Code:
    #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++)
        {
            i*=power;
        }
    
        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
    Posts
    2,686
    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.

    Code:
    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);
    
        if(negative)
            cout << "-";
        cout << result;
    }

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 06: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, 07:39 AM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM

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