r/ProgrammerTIL • u/C4Oc • Mar 24 '21
C# [.NET 4.0] TIL that there are BigIntegers, which can store numbers that are extremely large
It's more like 2 days ago, but I figured out that essentially you can use BigIntegers to calculate numbers up to 1×1050000 with relative ease. This type exists in other languages too and now I finally know how the Google Calculator on my phone manages to reach 6.3×1075257.
Although above 1×1070000 it begins to get quite slow and it's almost freezing the program to a halt at 1×10100000. If you really have to get that far, then 1×10150000 is really the limit before it gets way too slow. In certain scenarios BigIntegers can be useful, for example a calculator.
28
u/xonjas Mar 25 '21
All good programing languages have some sort of big int/big decimal implementation.
26
u/[deleted] Mar 24 '21
The only limit on integer size in python is the amount of memory available.
11
u/BenjaminGeiger Mar 24 '21
I've been using BigInteger in a few of my Advent of Code solutions in F#. It's significantly slower than Int32/Int64 but worth it not to have to worry about overflow.
6
u/iiiinthecomputer Mar 25 '21 edited Mar 25 '21
These are generally implemented as binary-coded decimal (BCD) or densely-packed decimal (DPD) and are very useful. PostgreSQL's
NUMERICis one example of a BCD decimal data type. They're extremely useful but they do have some big disadvantages too.There are a variety of related fixed precision decimal floating point (DFP) data types available in many languages. These have major advantages over infinite precision BCD and you should consider using them when they are available. Advantages to these "decfloat" types include native hardware support for operations with them on some platforms, fixed-width representations for more efficient storage and passing, and the availability of multiple existing libraries that implement them.
gccsupports decfloat types natively and will use hardware instructions for them where available.All these types have some disadvantages too.
Mainly, they're slow and they use more memory.
BCD in particular is very slow to perform arithmetic on. There's no widespread hardware support for DFP (really only IBM POWER6 and zSeries) so everything must be done in software, with limited assistance from SIMD instructions for some operations. Many implementations don't support a full range of operators on BCD types at all, so you may find you can't do things like logarithms or exponentiation without lossy conversion to binary floating point. Also, because BCD types almost always use variable width binary representations more memory copying and dynamic memory allocation is required when working with them than with basic integral types.
BCD types usually require more memory/storage than a basic type representation of the same value too. The main exception is when most of your values are small but you must support very large values occasionally, in which case sometimes their variable width storage will benefit you. In most cases those small benefits are lost due to padding and alignment anyway.
Fixed width DFP is often a safer bet. If hardware and tool support was more wide spread I'd use 64 bit DFP for a whole lot of things. The saner rounding and the ability to avoid most of the issues with binary floating point vs base10 representation are great. But right now support just isn't universal enough.
BCD and DFP types are excellent tools but should not be applied blindly to every problem. They're often a worthwhile simplification though, and the performance impact may not matter for many situations. I prefer to use them over fixed point decimal maths with binary integers because they're much simpler to work with and that reduces the risk of bugs. But if I expect to store 100 million integers between 0 and 200 you better bet I'm using a fixed binary integral type.
Look at the performance difference that Python's
numpyachieves.1
u/hadoopken Mar 25 '21
But you can't use that for a Facebook interview for a large number multiplication in strings :-)
2
u/ColdFerrin Mar 25 '21 edited Mar 25 '21
Java has them as well. In Java the size limit is memory, however the larger the get the slower math becomes. In Java there is also bigDecimal, which is just bigInteger that remembers where the decimal point is.
They are both just strings underneath.Edit: I was wrong. It is stored as twos compliment binary, but with the sign stored separately.
1
u/iiiinthecomputer Mar 25 '21
gccsupports decfloat types per TR24732 which specifies support for IEE-754-2008 decimal floating point.IEE-754-2008 decimal floating point was previously part of the separate IEE-854-1987 standard.
This isn't a BCD type like the Java or .NET
BigInteger, but it's an extremely useful close relative you should know about. It's more similar toBigDecimalin languages with that.0
u/[deleted] Mar 25 '21 edited Mar 30 '21
Yeah, most languages have them. These are called arbitrary precision integers/numbers. In Java they are called java.math.BigInteger and java.math.BigDecimal, the latter is for fractions.
edit: someone got salty
-12
u/Campes Mar 24 '21
Chances are they are probably using floating point numbers internally for calculations, and not integers. Floats can also store very large numbers and more importantly, you need to be able to support decimals which integers cannot.