ulp.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "common.h"
00039
00040 #include <float.h>
00041
00042
00043 double
00044 bn_epsilon()
00045 {
00046 #if defined(DBL_EPSILON)
00047 return DBL_EPSILON;
00048 #elif defined(HAVE_IEEE754)
00049 static const double val = 1.0;
00050 register long long next = *(long long*)&val + 1;
00051 return val - *(double *)&next;
00052 #else
00053
00054 volatile double tol = 1.0;
00055 while (1.0 + (tol * 0.5) != 1.0) {
00056 tol *= 0.5;
00057 }
00058 #endif
00059 }
00060
00061
00062 float
00063 bn_epsilonf()
00064 {
00065 #if defined(FLT_EPSILON)
00066 return FLT_EPSILON;
00067 #elif defined(HAVE_IEEE754)
00068 static const float val = 1.0;
00069 register long next = *(long*)&val + 1;
00070 return val - *(float *)&next;
00071 #else
00072
00073 volatile float tol = 1.0f;
00074 while (1.0f + (tol * 0.5f) != 1.0f) {
00075 tol *= 0.5f;
00076 }
00077 #endif
00078 }
00079
00080
00081 double
00082 bn_dbl_min()
00083 {
00084 register long long val = (1LL<<52);
00085 return *(double *)&val;
00086 }
00087
00088
00089 double
00090 bn_dbl_max()
00091 {
00092 static const double val = INFINITY;
00093 register long long next = *(long long*)&val - 1;
00094 return *(double *)&next;
00095 }
00096
00097
00098 double
00099 bn_flt_min()
00100 {
00101 register long val = (1LL<<23);
00102 return *(float *)&val;
00103 }
00104
00105
00106 double
00107 bn_flt_max()
00108 {
00109 static const float val = INFINITY;
00110 register long next = *(long*)&val - 1;
00111 return *(float *)&next;
00112 }
00113
00114
00115 double
00116 bn_ulp(double val)
00117 {
00118 double next;
00119 register long long up, dn, idx;
00120
00121 if (isnan(val) || !isfinite(val))
00122 return val;
00123
00124 if (val >=0) {
00125 up = *(long long*)&val + 1;
00126 return *(double *)&up - val;
00127 }
00128 dn = *(long long*)&val - 1;
00129 return *(double *)&dn - val;
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141