/* logs.h -- define macros for working with logs of unsigned numbers * * logsum(a, b) is log(exp(a) + exp(b)), computed in such a way as to * prevent overflow. logdif is log(exp(a) - exp(b)). a must be > b. * -1.0 is treated as log(0.0). logmul(a, b) is log(exp(a) * exp(b)), * which is just the sum of a and b unless either is -1.0. */ #define LOGMAXL (log((double) MAXLONG)) /* log of max long integer */ #define LOG0 (-1.0) /* clean up the code a little */ #define LOG1 (0.0) #define LOG2 (log(2.0)) #define lsum1(a, b) ((b < LOG1) ? a : (a + log1p(exp(b - a)))) #define logsum(a, b) (((a) > (b)) ? lsum1((a), (b)) : lsum1((b), (a))) #define logdif(a, b) (((b) < LOG1) ? (a) : ((a) + log1p(-exp((b) - (a))))) #define logmul(a, b) ((((a) < LOG1) || ((b) < LOG1)) ? LOG0 : ((a) + (b))) #define logint(a) (((a) == 0) ? (LOG0) : log((double) (a)))