How to handle big data element in c++? - c++

I want to divide the return value of pow(2.0,(n-8)) by 86399.
The problem is 10 <= n <= 100000000.
How can I handle such a large return value?
I'm on Ubuntu 11.10 64 bits, using C++ 4.0.0-8

You can't unless you use a big numbers library. 64 bits can't hold a number that big. And even then, it will probably take a while. 2^(86392) has about 26000 digits in it.
If you want to get just a modulus, there are some nice algorithms for that. See http://en.wikipedia.org/wiki/Modular_exponentiation.
If you want to try bignums still, check out http://gmplib.org/.

One very easy way would be to use GMP -- http://gmplib.org/

This discussion should answer your question Modular Exponentiation for high numbers in C++
For numbers that large, you'll have to do something clever. There's no way you can represent that full number naively in any reasonable way without bigint libraries, and even then it's really too big for brute force. The number itself would take up tens of megabytes.

Related

Boost's multiprecision to emulate low-accuracy fast floats

I have an stm32-F1 processor that is very slow with float operations and I have some libraries from an F7 processor that uses a lot of floats. I would like to use this libraries on my poor F1 so I was thinking of a way to make as less tweaks as I can on the code and emulate floats with a same interface but with an underling integer type.It's important to note that I only need 7 digits of accuracy (numbers between 0.001 and 4094.999 that's why I guess something like typedef number<cpp_dec_float<7> > fixed7; would work in my case faster than floats.
Is boost's multiprescision good enough for that ? Do you have any other suggestions? should I make my own arithmetic type?
I found a solution after all. Τhis numeric system is excellent for my purpose and a little simpler to use as multiprescision is based on having higher accuracy than normal c++ but I just want less precision. Here is John's MC Farlane numeric types

division of two very large numbers

I want to calculate (a+b)/pow(2,s).
-10^10 ≤ s ≤ 10^10
1 ≤ a, b ≤ 10^9
But even if I store the result in long long it gives 0. How can I calculate the result given that the answer lies in the range of long?
You don't need division for this... dividing by a power of two is just a right-shift.
You probably need a good bignum library, e.g. GMPlib
Bignum operations are a difficult algorithmic issue (there are several books on the subject, and you can still earn a PhD to improve them). So don't try to reinvent them yourself (if you want them to be more efficient than the slow naive algorithms everyone learned at elementary school, you'll need many years of hard work) but use an existing bignum library.
Perhaps you have some XY problem, and you don't need to compute that in fact. (You did not motivate your question enough).
You can use std::bitset<log2(10^9)> manually calculate the sum and then call operator>>=( s ) on result and obtain long by std:bitset::to_ulong()
You can use boost cpp_int, it is easiest way.

How to make large calculations program faster

I'm implementing a compression algorithm. Thing is, it is taking a second for a 20 Kib files, so that's not acceptable. I think it's slow because of the calculations.
I need suggestions on how to make it faster. I have some tips already, like shifting bits instead of multiplying, but I really want to be sure of which changes actually help because of the complexity of the program. I also accept suggestions concerning compiler options, I've heard there is a way to make the program do faster mathematical calculations.
Common operations are:
pow(...) function of math library
large number % 2
large number multiplying
Edit: the program has no floating point numbers
The question of how to make things faster should not be asked here to other people, but rather in your environment to a profiler. Use the profiler to determine where most of the time is spent, and that will hint you into which operations need to be improved, then if you don't know how to do it, ask about specific operations. It is almost impossible to say what you need to change without knowing what your original code is, and the question does not provide enough information: pow(...) function: what are the arguments to the function, is the exponent fixed? how much precision do you need? can you change the function for something that will yield a similar result? large number: how large is large in large number? what is number in this context? integers? floating point?
Your question is very broad, without enough informaiton to give you concrete advise, we have to do with a general roadmap.
What platform, what compiler? What is "large number"? What have you done already, what do you know about optimization?
Test a release build with optimization (/Ox /LTCG in Visual C++, -O3 IIRC for gcc)
Measure where time is spent - disk access, or your actual compression routine?
Is there a better algorithm, and code flow? The fastest operation is the one not executed.
for 20K files, memory working set should not be an issue (unless your copmpression requries large data structures), so so code optimization are the next step indeed
a modern compiler implements a lot of optimizations already, e.g replacing a division by a power-of-two constant with a bit shift.
pow is very slow for native integers
if your code is well written, you may try to post it, maybe someone's up to the challenge.
Hints :-
1) modulo 2 works only on the last bit.
2) power functions can be implemented in logn time, where n is the power. (Math library should be fast enough though). Also for fast power you may check this out
If nothing works, just check if there exists some fast algorithm.

Fast way to compute n times 10 raised to the power of minus m

I want to compute 10 raised to the power minus m. In addition to use the math function pow(10, -m), is there any fast and efficient way to do that?
What I ask such a simple question to the c++ gurus from SO is that, as you know, just like base 2, 10 is also a special base. If some value n times the 10's power minus m, it is equivalent to move n's decimal point to the left m times. I think it must be a fast and efficient way to cope with.
For floating point m, so long as your standard library implementation is well written, then pow will be efficient.
If m is an integer, and you hinted that it is, then you could use an array of pre calculated values.
You should only be worrying about this kind of thing if that routine is a bottleneck in your code. That is if the calls to that routine take a significant proportion of the total running time.
Ten is not a special value on a binary machine, only two is. Use pow or exponentiation by squaring.
Unfortunately there is no fast and efficient way to calculate it using IEEE 754 floating point representation. The fastest way to get the result is to build a table for every value of m that you care about, and then just perform a lookup.
If there's a fast and efficient way to do it then I'm sure your CPU supports it, unless you're running on an embedded system in which case I'd hope that the pow(...) implementation is well written.
10 is special to us as most of us have ten fingers. Computers only have two digits, so 2 is special to them. :)
Use lookup table there cant be more than 1000 floats and especially if m is integer.
If you could operate with log n instead of n for a significant time, you could save time because instead of
n = pow(10*n,-m)
you now have to calculate (using the definition l = log10(n))
l = -m*(l+1)
Just some more ideas which may lead you to further solutions...
If you are interested in
optimization on algorithm level you
might look for a parallelized
approach.
You may speed up on
system/archtectural level on using Ipp
(for Intel Processors), or e.g. AMD
Core Math Library (ACML) for AMD
To use the power of your graphics
card may be another way (e.g. CUDA for NVIDEA cards)
I think it's also worth to look at
OpenCL
IEEE 754 specifies a bunch of floating-point formats. Those that are in widespread use are binary, which means that base 10 isn't in any way special. This is contrary to your assumption that "10 is also a special base".
Interestingly, IEEE 754-2008 does add decimal floating-point formats (decimal32 and friends). However, I'm yet to come across hardware implementations of those.
In any case, you shouldn't be micro-optimizing your code before you've profiled it and established that this is indeed the bottleneck.

how to store 1000000 digit integers in C++

in my problem i have to save big big integers like upto 1000000 digits and to do some operation. how can i do that.i know that a long int in c++ can store upto 10 digits
You can use GMP, the GNU arbitrary precision library. Just be aware that it's not a very good library if you run out of memory.
By that, I mean it will just exit out from underneath you if it cannot allocate memory. I find this an ... interesting ... architectural decision for a general purpose library but it's popular for this sort of stuff so, provided you're willing to wear that restriction, it's probably a good choice.
Another good one is MPIR, a fork of GMP which, despite the name "Multiple Precision Integers and Rationals", handles floating point quite well. I've found these guys far more helpful than the GMP developers when requesting help or suggesting improvements (but, just be aware, that's my experience, your mileage may vary).