i have a float value that is hundreds of digits long (like the first 100 digits of pi - 3) and need a way to operate on it. is there any way to store and operate on the float that has a large number of decimals and maintain much precision with built in libraries? is there anything like python's Decimal module in c++?
The other answers all point to high precision integer libraries. There are however a few floating point libraries around:
The High Precision Arithmetic library
The GNU Multiple Precision Arithmetic Library (GMP) "Arithmetic without limitations"
The GNU multiple-precision floating-point computations with correct rounding (the GNU MPFR library). There's also a C++ wrapper.
NTL: A Library for doing Number Theory. Together with NTL::RR you can use this even within boost.
The LBNL double-double precision, quad-double precision and arbitrary precision software.
... and don't forget the possibility that you can always implement your own solution. (Might not be the most effective or fastest solution, but it's "the" solution if you want to learn something.
No built-in library, but you can do that using Bignum arithmetics :) http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic.
What a Bignum is: an array (vector) of digits. You can easily implement sum/difference....
I've actually asked something simillar here: STL big int class implementation
Unless it is some extra exotic platform, where a float is 100+ bytes long, you will find it hard to archive what you want without a library for big numbers.
Related
In my project, I should get two numbers that are bigger than long long or etc. So I'm looking for a good solution.
C++ itself is not able to work with variables that have more then 64 bits. You either may implement it yourself, which I would strongly discourage you, or you use something well tested.
One of the best and well known libraries provides an solution for you: boost.
The Multiprecision Library provides integer, rational and floating-point types in C++ that have more range and precision than C++'s ordinary built-in types. The big number types in Multiprecision can be used with a wide selection of basic mathematical operations, elementary transcendental functions as well as the functions in Boost.Math. The Multiprecision types can also interoperate with the built-in types in C++ using clearly defined conversion rules. This allows Boost.Multiprecision to be used for all kinds of mathematical calculations involving integer, rational and floating-point types requiring extended range and precision.
Find all the information you need behind this Boost.Multiprecision
I wish to declare a floating point variable that can store more significant digits than the more common doubles and long doubles, preferably something like an octuple (256 bits), that (I believe) might give about 70 significant digits.
How do I declare such a variable? And will cross-platform compability be an issue (as opposed to fixed-width integers)?
Any help is much appreciated.
The C++ standard mandates precision up to and including double; and the finer details of that floating point scheme are left to the implementation.
An IEEE754 quadruple precision long double will only give you 36 significant figures. I've never come across a system, at the time of writing, that implements octuple precision.
Your best bet is to use something like the GNU Multiple Precision Arithmetic Library, or, if you really want binary floating point, The GNU Multiple Precision Floating Point Reliable Library.
While I don't know of any C++ libraries that fully implement a proper IEEE754 octuple precision, I've found a library by the name ttmath which implements a multi-word system, allowing it to deal with much larger numbers.
In my trading application I have to use decimal to represent prices. I need lowest possible latency so the only acceptable solution would be to use int64 to represent decimal. I can configure globally that I do not need for example more then 5 digits after dot, then everywhere
0.0000001 is not supported
0.000001 is not supported
1 should be used instead of 0.00001
10 should be used instead of 0.0001
100 should be used instead of 0.001
1000 should be used instead of 0.01
10000 should be used instead of 0.1
100000 should be used instead of 1
and so on
Are there any libraries that help to do such kind of work? I don't understand completely if I need any library, probably I should just work with int64 and that's it? Any hints and suggestions are welcome.
upd I now realized that devide and multiply are not obvios at all. So i'm looking for some header only library that add some macros of function to devide/multiply fixed point stored in INT64.
What you're suggesting is basically fixed point arithmetic. It's a way of achieving decimal fraction calculations using only integer operations. It can have some speed advantages (on some systems), and if it's done correctly can avoid some of the errors introduced through floating point.
There will be libraries which can help, although the maths involved is quite simple. You might find it's easy enough to read up on the subject and implement it yourself.
For example, how can I use the result of 1000^1000 for arithmetic? I don't think there's a library that can accommodate that, all I see at most is 100 number digits.
Use an arbitrary-precision arithmetic library like GMP or Boost.Multiprecision.
What you are looking for is a library like GMP or Boost-Multiprecision or TTmath.
Or, you might challenge yourself to write a low level representation that handles longer than standard bit representations, and do arithmetic with it.
Stick with the first option though, if it does the job you have in mind.
I was wondering what kind of method was used to multiply numbers in C++. Is it the traditional schoolbook long multiplication? Fürer's algorithm? Toom-Cook?
I was wondering because I am going to need to be multiplying extremely large numbers and need a high degree of efficiency. Therefore the traditional schoolbook long multiplication O(n^2) might be too inefficient, and I would need to resort to another method of multiplication.
So what kind of multiplication does C++ use?
You seem to be missing several crucial things here:
There's a difference between native arithmetic and bignum arithmetic.
You seem to be interested in bignum arithmetic.
C++ doesn't support bignum arithmetic. The primitive datatypes are generally native arithmetic to the processor.
To get bignum (arbitrary precision) arithmetic, you need to implement it yourself or use a library. (such as GMP) Unlike Java, and C# (among others), C++ does not have a library for arbitrary precision arithmetic.
All of those fancy algorithms:
Karatsuba: O(n^1.585)
Toom-Cook: < O(n^1.465)
FFT-based: ~ O(n log(n))
are applicable only to bignum arithmetic which are implemented in bignum libraries. What the processor uses for its native arithmetic operations is somewhat irrelevant as it's
usually constant time.
In any case, I don't recommend that you try to implement a bignum library. I've done it before and it's quite demanding (especially the math). So you're better off using a library.
What do you mean by "extremely large numbers"?
C++, like most other programming languages, uses the multiplication hardware that is built-in in the processor. Exactly how that works is not specified by the C++ language. But for normal integers and floating-point numbers, you will not be able to write something faster in software.
The largest numbers that can be represented by the various data types can vary between different implementations, but some typical values are 2147483647 for int, 9223372036854775807 for long, and 1.79769e+308 for double.
In C++ integer multiplication is handled by the chip. There is no equivalent of Perl's BigNum in the standard language, although I'm certain such libraries do exist.
That all depends on the library and compiler used.
It is performed in hardware. for the same reason huge numbers won't work. The largest number c++ can represent in 64 bit hardware is 18446744073709551616. if you need larger numbers you need an arbitrary precision library.
If you work with large numbers the standard integer multiplication in c++ will no longer work and you should use a library providing arbitrary precision multiplication, like GMP http://gmplib.org/
Also, you should not worry about performance prior to writing your application (=premature optimization). These multiplications will be fast, and most likely many other components in your software will cause much more slowdown.
plain c++ uses CPU mult instructions (or schoolbook multiplication using bitshifts and additions if your CPU does not have such an instruction. )
if you need fast multiplication for large numbers, I would suggest looking at gmp ( http://gmplib.org ) and use the c++ interface from gmpxx.h
Just how big are these numbers going to be? Even languages like python can do 1e100*1e100 with arbitrary precision integers over 3 million times a second on a standard processor. That's multiplication to 100 significant places taking less than one millionth of second. To put that into context there are only about 10^80 atoms in the observable universe.
Write what you want to achieve first, and optimise later if necessary.