C++ : largest and smallest float

Carlis

Senior member
May 19, 2006
237
0
76
I am writing some software to compute partition functions from some monte carlo simulations. The issue is that I have to make sure that I dont obtain nonsens due to the limitations of the size of a float.

I am worried about the following piece of code:


----------------------
arg = (BETA[k]-BETA[j])*Eis + Z[k]-Z[j]
den+=exp(arg)
---------------------------
Now, arg can be rather large. So how can I find out what is the largest/smallest argument that exp can operate on? Also, I suppose that I can declare 'den' as a long double, but that will not help, since exp returns only a double, right?

I am doing this with gcc (g++) on a 64 bit intel mac.

Best
Carlis
 

Schmide

Diamond Member
Mar 7, 2002
5,590
724
126
They should all be declared in <values.h> <limits.h> or <float.h>

maxfloat
maxdouble
etc

You'll probably run into computational pitfalls before you overflow the numbers.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
I agree with Schmide, floating point rounding errors are likely going to bite you pretty quickly.

If you are looking for something that has a high precision and is fairly quick, I recommend using the GMP. It can do arbitrary floating point precision while being pretty darn nippy.
 

iCyborg

Golden Member
Aug 8, 2008
1,327
52
91
I did some work with MC involved, and I normally operated with probabilities. And there's a standard way of treating them mathematically not by looking at them directly, but by operating with log probabilities. This has several benefits: usually you would have products of exp(x_i) if the variables are IID, so besides the fact that you deal with a smaller range of values and thus avoid overflows and other numerical issues, due to the property of log-exp, the product is replaced by a sum of logs which improves performance too.

Now, I don't know the problem domain enough from your description, it's a bit weird to see summing exps, so it may well be that the above doesn't apply, but you may still try to see if it makes sense to translate stuff into logs of stuff. If not, you'll have lots of problems if arg can vary a lot. You'll have to be careful not just about overflows, but how you sum them up because summing a small and a large double is something that doesn't produce good results, and with exps even reordering the operations may not help much. Perhaps looking at something like the abovementioned GMP would be worthwhile in that case.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |