1

Our program manipulates extensively real numbers that happen to be very small or big. while we don't need a very high precision. We are strongly concerned about performance (CPU usage).

Such numbers could be 2.5687e-45785 , for instance.

Remark : as the program does a lot of additions, it is not an efficient possibility to work with the logarithms.

Fortunately, we don't need arbitrary small or high numbers neither (it would require to code the numbers with a structure with variable size). Let say 1.0e100000 is a reasonable limit for us.

I know that C offers several types for floating point numbers : float, double, long double. I have taken a look at the way numbers are encoded and what that implies in term of limits (IEE 754). The bits used to represents the numbers are separated in 3 groups : a sign bit, an exponent group (the powers of 2 that conditions how big or small values numbers can reach) and a fraction part that gives the precision. The more bits in the exponent, the smaller and higher the numbers.

long doubles on my X86-64 family computer have 15 bits of exponent and 63 bits for fraction.( https://en.wikipedia.org/wiki/Extended_precision). They can represent numbers in the range 3.65×10^−4951 to 1.18×10^4932.

But for our need, it has too much precision but cannot represent numbers as small or big as I would like. Ideally, it would be fine if I could transfer some fraction bits to exponent bits. Of course, I am aware that this type is not flexible because it is the actual type that the hardware manipulates (Intel 8087 math coprocessor for long double).

Before devising our own representation of numbers (probably with a double and an additional integer seen as an extra exponent) and understanding that it won't be easy to make it efficient(in term of CPU usage) because of the renormalisations that would occur, I would like to know whether libraries exists that already provides number representations tha-64t match our needs and are efficient.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 3
    such libraries do exist, though asking for library recommendations is offtopic. Moreover, you should pick only one language not two – 463035818_is_not_an_ai Jun 05 '23 at 08:49
  • Different big number libraries have different benefits and disadvantages. You should ask for opinions in discussion groups, does not fit to format of stack overflow. – Öö Tiib Jun 05 '23 at 08:54
  • 2
    You can't have written this post and done this amount of research without stumbling over libgmp and friends. How do they not fit your requirements? – Botje Jun 05 '23 at 08:55
  • You dismissed working with logarithms for efficiency concerns, but using a library or your own floating-point representation may end up beeing quite slower. – Bob__ Jun 05 '23 at 08:58
  • @Botje: Does libgmp have floating-point types? My first thought for a library was https://www.mpfr.org/ – Peter Cordes Jun 05 '23 at 08:59
  • @PeterCordes https://www.mpfr.org/faq.html – Jabberwocky Jun 05 '23 at 09:04
  • 1
    Just came here to say the same. The GMP website states "High-level floating-point arithmetic functions (mpf). This is the GMP function category to use if the C type `double' doesn't give enough precision for an application. There are about 70 functions in this category. **New projects should strongly consider using the much more complete GMP extension library mpfr instead of mpf.**" – Botje Jun 05 '23 at 09:05
  • 1
    @Bob__: I think he means that it wouldn't work to keep the data in logarithm format all the time, like they could if he only wanted to multiply and divide these numbers. (Then they could just map their bignums to floats representing their logs, and use hardware FP add/sub.) Using an exponent field (log) as part of a software big-float format requires comparing exponents when adding, and if they're close enough for significant digits to overlap, shifting. Unless you know a trick for doing log(x+y) in terms of log(x) and log(y)? – Peter Cordes Jun 05 '23 at 09:06
  • 3
    What does "our program" do? On the whole, it is the range of magnitudes that matter, not the absolute size of any element. If it is a physical problem involving a number like 2.5687e-45785 then you are probably working in the wrong units. – lastchance Jun 05 '23 at 09:17
  • 4
    @lastchance Which physical system can have such numbers, even if you work in the wrong units? Or more generally asking the OP: What application use such enormous floating point numbers? – gerum Jun 05 '23 at 09:24
  • @lastchance it is not a physical system but a mathematical one. The numbers are used in counting enumerations or probabilities. – Arnaud Mégret Jun 05 '23 at 09:48
  • But @Arnaud Megret, regardless of floating-point representation, do you know how long it will take you to count to a number as large as 1.0e100000? Conversely, if you want a probability as small as 2.5687e-45785, apart from the fact that that is in the realm of "effectively never happens", maybe you should consider continuous probability distributions not discrete ones. – lastchance Jun 05 '23 at 09:58
  • 1
    I'm not sure 1.0e100000 and "reasonable" belong in the same sentence. – n. m. could be an AI Jun 05 '23 at 09:58
  • @lastchance 100000 multiplications by 10. We are doing both multiplications and additions. – Arnaud Mégret Jun 05 '23 at 10:08
  • @lastchance suppose you want to represent the number of different possibilities to give values for a set of 100 000 variables having 10 states each. The calculus is trivial in that simple case. But I can add logical constraints between the values of variables, the number of valid possibilities is harder to obtain and require a structure called a clique-tree. – Arnaud Mégret Jun 05 '23 at 10:15
  • Yes, @Arnaud Megret, but that is why statistical thermodynamics provides a mathematical framework, whilst classical thermodynamics is what engineers use to analyse heat engines. – lastchance Jun 05 '23 at 10:21

0 Answers0