Calculation results with very long numbers

This is a discussion on Calculation results with very long numbers within the C++ Programming forums, part of the General Programming Boards category; Hi. I am new to C++. Learning from tutorials. One of tutorials shows how to write a simple program that ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    12

    Calculation results with very long numbers

    Hi. I am new to C++.
    Learning from tutorials.
    One of tutorials shows how to write a simple program that accepts a number and then calculates it to power of another user specified number. Simple numbers work fine (2^5=25, etc) But I want to try very long numbers (1234567^34854322).
    The program either quits after accepting the two numbers, or shows something like 1.#INF.
    I realize the result may take a hundred lines to display, that is fine.
    Question. Is this a failure to calculate or failure to display a result that has been calculated?
    How do I format the result so it can be seen?
    What are the limitations, if any?
    Here is the program:

    Code:
    // Compute power of a number
    #include <iostream>
    #include <cmath> 
    #include <iomanip>
    
    
    int main()
    {
        using namespace std;
        double power1;
        double num1;
        cout << "Enter number" << endl;
        cin >> num1;
        cout << "Enter power" << endl;
        cin >> power1;
        num1 = pow(power1,num1);
        cout << setprecision(130)  << num1 << endl;
        
        system("pause");
            return 0;
    }

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    I am certain that 1234567^34854322" is way too big for any standard data type. You could write one, storing digits in a vector. Then compute the result based on that old way you learned to count. Some digit * the base ^ postion

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    12

    What is the best source to look up these custom datatypes?

    Syscal, thank you for the reply.
    What is the best source to look up these custom vector data types? Is this the right term, first of all?
    Thanks again.

  4. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    It's a failure to store. double datatype just doesn't have enough bits to represent such a huge number.

    See here for how to determine various such limits on your system.

  5. #5
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    This might be enlightening: Arbitrary-precision arithmetic - Wikipedia, the free encyclopedia

    I must be going crazy because I can remember there being a Boost bigint library... but it doesn't exist.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    Hmm, as fate would have it, I've been working on my simple and easy to understand bigint and megafloat classes over the last few days.
    Just finishing ensuring they pass all tests before uploading.
    Probably best to pick one of those existing ones if you're more interested in results, than how it works though.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    12
    BMJ (thank you BTW) pointed to a wikipedia article which has this link to an online calculator: ttmath.org / Big online calculator . So, it can compute but what code do I write to generate these large numbers? examples will be appreciated!

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    12

    How to connect these two codes?

    I found an example of how to use vectors, but this example below uses a constraint. How do I connect the two so it stores long numbers as shown above:

    Code:
    #include <cstring> // memcpy
    #include <vector> 
    #include <cstdio> // printf
    int main()
    {
      using namespace std;
      const char arr[] = "12345678902333333333323323333333345345345345345345435";
      // construct a vector with 11 zero-initialized chars.
      vector<char> vec(sizeof arr);  
      // copy 11 chars from arr into the vector
      memcpy(&vec[0], arr, sizeof arr); 
      // prints "1234567890"
      printf("%s", &vec[0]); 
      
        system("pause");
        return 0;
    }

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    30
    Quote Originally Posted by grigorianvlad View Post
    I found an example of how to use vectors, but this example below uses a constraint. How do I connect the two so it stores long numbers as shown above:
    what do you mean by constraint?
    btw, google is your friend, here's one big integer library
    https://mattmccutchen.net/bigint/

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Your problem here is pow(). You need to re-implement the function.

    Basically, you want
    Code:
    vector<char>* long_pow(double num, double pow);
    Now how to implement that. I don't think it is that easy. Your alternative would be to find a ready library.

    You could of course go with
    Code:
    vector<char>* long_pow(vector<char>& num, vector<char>& pow);
    and basically do it as you would do it with a pen and paper speed not being good but still it would work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 05:20 PM
  2. Knight's Tour Recursion Problem
    By thephreak6 in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 09:18 AM
  3. Peculiar Problem with Printf
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-02-2002, 12:34 AM
  4. Peculiar Problem with Printf
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 07-02-2002, 12:03 AM
  5. how to pack 8 x 4 bit binary numbers into a long int?
    By n00bcodezor in forum C Programming
    Replies: 11
    Last Post: 11-19-2001, 04:46 PM

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