1 /***************************************************************************** 2 * * 3 * UNURAN -- Universal Non-Uniform Random number generator * 4 * * 5 ***************************************************************************** 6 * * 7 * FILE: unur_fp_source.h * 8 * * 9 * PURPOSE: * 10 * declares macros for floating point arithmetic. * 11 * * 12 ***************************************************************************** 13 * * 14 * Copyright (c) 2000-2006 Wolfgang Hoermann and Josef Leydold * 15 * Department of Statistics and Mathematics, WU Wien, Austria * 16 * * 17 * This program is free software; you can redistribute it and/or modify * 18 * it under the terms of the GNU General Public License as published by * 19 * the Free Software Foundation; either version 2 of the License, or * 20 * (at your option) any later version. * 21 * * 22 * This program is distributed in the hope that it will be useful, * 23 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 25 * GNU General Public License for more details. * 26 * * 27 * You should have received a copy of the GNU General Public License * 28 * along with this program; if not, write to the * 29 * Free Software Foundation, Inc., * 30 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA * 31 * * 32 *****************************************************************************/ 33 34 /*---------------------------------------------------------------------------*/ 35 #ifndef UNUR_FP_SOURCE_H_SEEN 36 #define UNUR_FP_SOURCE_H_SEEN 37 /*---------------------------------------------------------------------------*/ 38 39 /*---------------------------------------------------------------------------*/ 40 /* Comparisons */ 41 42 int _unur_FP_cmp( double x1, double x2, double eps); 43 /* compare two floats: */ 44 /* x1 eq x2 iff |x1-x2| <= min(|x1|,|x2) * eps */ 45 /* return: */ 46 /* -1 if x1 < x2 */ 47 /* 0 if x1 eq x2 */ 48 /* +1 if x1 > x2 */ 49 50 /* macros for different levels of accuracy */ 51 #define _unur_FP_cmp_same(a,b) (_unur_FP_cmp((a),(b),DBL_EPSILON)) 52 #define _unur_FP_cmp_equal(a,b) (_unur_FP_cmp((a),(b),UNUR_EPSILON)) 53 #define _unur_FP_cmp_approx(a,b) (_unur_FP_cmp((a),(b),UNUR_SQRT_DBL_EPSILON)) 54 55 /* a == b (except precision bit) */ 56 #define _unur_FP_same(a,b) (_unur_FP_cmp((a),(b),DBL_EPSILON)==0) 57 58 /* a == b */ 59 #define _unur_FP_equal(a,b) (_unur_FP_cmp((a),(b),UNUR_EPSILON)==0) 60 61 /* a is approximately equal to b */ 62 #define _unur_FP_approx(a,b) (_unur_FP_cmp((a),(b),UNUR_SQRT_DBL_EPSILON)==0) 63 64 /* a < b */ 65 #define _unur_FP_less(a,b) ((_unur_FP_cmp((a),(b),UNUR_EPSILON)<0) ? TRUE : FALSE) 66 67 /* a > b */ 68 #define _unur_FP_greater(a,b) ((_unur_FP_cmp((a),(b),UNUR_EPSILON)>0) ? TRUE : FALSE) 69 70 /*---------------------------------------------------------------------------*/ 71 /* Comparing floating point with == or != is unsafe. */ 72 /* However, we assume that comparing with 0.0 and powers of 2.0 is safe. */ 73 /* Thus we use the followig functions to mark these "safe" comparisons in */ 74 /* the code and thus we can use the GCC to detect all other comparisons. */ 75 /* For the latter _unur_FP_cmp_same() must be used. */ 76 77 /* defined as macros */ 78 #define _unur_iszero(x) ((x)==0.0) 79 #define _unur_isone(x) ((x)==1.0) 80 #define _unur_isfsame(x,y) ((x)==(y)) 81 82 /* defined as functions: only used for debugging. */ 83 /* (it switches off GCC warnings) */ 84 #ifndef _unur_iszero 85 int _unur_iszero (const double x); 86 #endif 87 88 #ifndef _unur_isone 89 int _unur_isone (const double x); 90 #endif 91 92 #ifndef _unur_isfsame 93 int _unur_isfsame (const double x, const double y); 94 #endif 95 96 /*---------------------------------------------------------------------------*/ 97 /* Infinity and NaN (Not a Number) */ 98 99 /* wrapper / replacement for corresponding C99 functions */ 100 int _unur_isfinite (const double x); 101 int _unur_isnan (const double x); 102 int _unur_isinf (const double x); 103 104 /*---------------------------------------------------------------------------*/ 105 /* Other checks for infinity */ 106 107 /* +oo */ 108 #define _unur_FP_is_infinity(a) ((a) >= INFINITY) 109 110 /* -oo */ 111 #define _unur_FP_is_minus_infinity(a) ((a) <= -INFINITY) 112 113 /*---------------------------------------------------------------------------*/ 114 #endif /* UNUR_FP_SOURCE_H_SEEN */ 115 /*---------------------------------------------------------------------------*/ 116 117 118 119 120 121 122