How to find the log of a number? - c++

I have a number (num1) which is 18 digits long. I am storing it in an integer array.
I have another number (num2) which is also 18 digits long. This is also being stored in an integer array.
I have to find the log of the first number to the base of the second number (log num1 to base num2).
How to do this in C++? I can't use the log function as the numbers are being stored in arrays.

They key term to google for is bigint. There are various C++ libraries which support bigints (that is, number which can be as long as your memory permits).
The only bigint library I've used myself is GMP. However, if you just need a single function on bigints (log, in your case), then maybe taking some smaller library (is more practical).
Just checked the GMP page and incidentally, it calls them bignums all over. So that may be another useful term to use when searching for a solution. ;-)

Related

How to parse the division of two very large numbers into a double?

I have a geometric algorithm which takes as input a polygon. However, the files I am supposed to use as input files store the coordinates of the polygons in a rather peculiar way. Each file consists of one line, a counterclockwise sequence of the vertices. Each vertex is represented by its x and y coordinates each of which is written as the quotient of two integers int/int. However, these integers are incredibly large. I wrote a program that parses them from a string into long long using the function std::stoll. However, it appears that some of the numbers in the input file are larger than 2^64.
The output coordinates are usually quite small, in the range 0-1000. How do I go about parsing these numbers and then dividing them, obtaining doubles? Is there any standard library way of doing this, or should I use something like the boost library?
If you are after a ratio of two large numbers as string, you can shorten the strings:
"194725681173571753193674" divided by "635482929374729202" is the same as
"1947256811735717" divided by "6354829293" to at least 9 digits (I just removed the same amount of digits on both sides). Depending on the needed precision, this might be the simplest solution. Just remove digits before converting to long long.
You can parse the inputs directly into a long double I believe. However, that approach will introduce precision errors. If precision is important, then avoid this.
A general solution for precise results is to represent the large integer with an array of integers where one integer represents the lower order bytes, next integer represents the larger bytes etc. This is generally called arbitrary precision arithmetic.
Is there any standard library way of doing this
No, other than basic building blocks such as vector for storing the array.
or should I use something like the boost library?
That's often a good place to start. Boost happens to have a library for this.

How to handle a integer value bigger than the max int(64) can store? [duplicate]

I am trying to write a program for finding Mersenne prime numbers. Using the unsigned long long type I was able to determine the value of the 9th Mersenne prime, which is (2^61)-1. For larger values I would need a data type that could store integer values greater than 2^64.
I should be able to use operators like *, *=, > ,< and % with this data type.
You can not do what you want with C natives types, however there are libraries that let handle arbitrarily large numbers, like the GNU Multiple Precision Arithmetic Library.
To store large numbers, there are many choices, which are given below in order of decreasing preferences:
1) Use third-party libraries developed by others on github, codeflex etc for your mentioned language, that is, C.
2) Switch to other languages like Python which has in-built large number processing capabilities, Java, which supports BigNum, or C++.
3) Develop your own data structures, may be in terms of strings (where 100 char length could refer to 100 decimal digits) with its custom operations like addition, subtraction, multiplication etc, just like complex number library in C++ were developed in this way. This choice could be meant for your research and educational purpose.
What all these people are basically saying is that the 64bit CPU will not be capable of adding those huge numbers with just an instruction but you rather need an algorithm that will be able to add those numbers. Such an algorithm would have to treat the 2 numbers in pieces.
And the libraries they listed will allow you to do that, a good exercise would be to develop one yourself (just the algorithm/function to learn how it's done).
There is no standard way for having data type greater than 64 bits. You should check the documentation of your systems, some of them define 128 bits integers. However, to really have flexible size integers, you should use an other representation, using an array for instance. Then, it's up to you to define the operators =, <, >, etc.
Fortunately, libraries such as GMP permits you to use arbitrary length integers.
Take a look at the GNU MP Bignum Library.
Use double :)
it will solve your problem!

How to Print number which is over ULLONG_MAX at Console ?

I want to print "845100400152152934331135470251" or "1071292029505993517027974728227441735014801995855195223534251"
but in C++ the max value of "Unsigned long long " is "18446744073709551615"
this is much less than which I want to print
please help me...
First of all, your problem is not about printing big numbers but storing them in variables (and maybe calculating on them).
On some compilers (GCC for example), you have variable types like int128 that can handle numbers up to 10^38 (more less).
If this doesn't solve the problem, you'll have to write your own arithmetic. For example, store numbers in strings and write functions that will calculate on them (addition and subtraction is rather easy, multiplying medium (as long as numbers aren't really huge), dividing by big integers hard). Alternatively you can look for already made big integer libraries (on the Internet, c++ doesn't have built-in one).

How does Microsoft Calculator, calculate such large numbers?

I am up to about 8E10000 so how is it calculating such large number, there is no variable that can hold such large numbers.
Normal types in C can usually only store up to 64 bits, instead of a single variable, you can use an array of characters to store digits of your number and write functions for each operation (sum, minus and so on) in your program.
You may look at this: GNU Multiple Precision Arithmetic Library
In a nut shell they aren't using one variable to hold the operands but data structures than can probably hold arbitrary long numbers (like an array) and they evaluate operations by considering the number to be in a large radix system.
When you actually do a math operation the operands aren't variables but array (or any other data structure that is suitable) and you do it by doing the operation (where available) component wise.
When you want to add 2 array you choose a radix and then loop the arrays and add op1[i] to op2[i] then you take that value and check to see if it its bigger than your radix and compute a carriage that you add to next addition.
car = (op1[i] + op2[i])%radix
You need to be careful in choosing the radix and the underlaying data so an addition doesn't cause an overflow.
This how you also do when you add numbers in the base 10 by hand but without taking into account the radix.
You can also look over this for a bigint package.

How to store extremely large numbers?

For example I have a factorial program that needs to save really huge integers that can be 50+ digits long. The absolute maximum primitive data type in C++ is unsigned long long int with a maximum value 18446744073709551615 which is only 20 digits long. Here's the link to the limits of C++: http://www.cplusplus.com/reference/climits/
How do I store numbers that are larger than that in a variable of some sort?
If you already have a boost dependency (which many people these days do), you can use the boost multi-precision library. In fact, it already has an example of a factorial program that can support output up to 128 bits, though extending it further is pretty trivial.
You'll have to use a bigint or bignum implementation. There are some libraries out there like this: http://gmplib.org/
Some more info and a list of libraries: http://en.wikipedia.org/wiki/Bignum
You can use array. First you should copy that giant number array,then use comma after 19 digits:
unsigned long long int num[]= {
7316717653133062491,9225119674426574742,3553491949349698352,0,312774506326239578,3180169848018694788,
5184385861560789112,9494954595017379583,3195285320880551112,5406987471585238630,5071569329096329522,
7443043557668966489,5044524452316173185,6403098711121722383,1136222989342338030,8135336276614282806,
4444866452387493035,8907296290491560440,7723907138105158593,0,7960866701724271218,8399879790879227492,
1901699720888093776,6572733300105336788,1220235421809751254,5405947522435258490,7711670556013604839,
5864467063244157221,5539753697817977846,1740649551492908625,6932197846862248283,9722413756570560574,
9026140797296865241,4535100474821663704,8440319989000889524,3450658541227588666,8811642717147992444,
2928230863465674813,9191231628245861786,6458359124566529476,5456828489128831426,0,7690042242190226710,
5562632111110937054,4217506941658960408,0,7198403850962455444,3629812309878799272,4428490918884580156,
1660979191338754992,0,0,5240636899125607176,0,6058861164671094050,7754100225698315520,0,0,
5593572972571636269,5618826704282524836,0,0,8232575304207529634,50};
There are many ways to store very big number which are below:
string
File
Link list
Vector/Dynamic array
Note: Please don't use array to avoid memory problem issues.