69 lines
1.8 KiB
C
69 lines
1.8 KiB
C
#include <ieee_float.h>
|
|
#include <math.h>
|
|
#include <stdlib.h>
|
|
|
|
int ieee_float_cmp(const float a, const float b, const float maxDiff){
|
|
float diff = a-b;
|
|
if(fabsf(diff) <= maxDiff){
|
|
return 0;
|
|
}else{
|
|
return (diff > maxDiff)?1:-1;
|
|
}
|
|
}
|
|
|
|
int ieee_double_cmp(const double a, const double b, const double maxDiff){
|
|
double diff = a-b;
|
|
if(fabs(diff) <= maxDiff){
|
|
return 0;
|
|
}else{
|
|
return (diff > maxDiff)?1:-1;
|
|
}
|
|
}
|
|
|
|
int ieee_float_UlpsCmp(const float a, const float b, const float maxFloatDiff, const int maxUlpsDiff){
|
|
const float absDiff = fabsf(a-b);
|
|
if(absDiff <= maxFloatDiff){
|
|
return 0;
|
|
}
|
|
ieee_float_t A;
|
|
ieee_float_t B;
|
|
A.value.f = a;
|
|
B.value.f = b;
|
|
if(A.value.IEEE754.sign != B.value.IEEE754.sign){
|
|
return (int)(B.value.IEEE754.sign - A.value.IEEE754.sign);
|
|
}
|
|
|
|
const int ulpsDiff = abs((int)(A.value.uint - B.value.uint));
|
|
if(ulpsDiff <= maxUlpsDiff){
|
|
return 0;
|
|
}
|
|
return (ulpsDiff > maxUlpsDiff)?1:-1;
|
|
}
|
|
|
|
int ieee_double_UlpsCmp(const double a, const double b, const double maxFloatDiff, const int maxUlpsDiff){
|
|
const double absDiff = fabs(a-b);
|
|
if(absDiff <= maxFloatDiff){
|
|
return 0;
|
|
}
|
|
ieee_double_t A = {.value.f = a};
|
|
ieee_double_t B = {.value.f = b};
|
|
if(A.value.IEEE754.sign != B.value.IEEE754.sign){
|
|
return (int)(B.value.IEEE754.sign - A.value.IEEE754.sign);
|
|
}
|
|
|
|
const int ulpsDiff = abs((int)(A.value.uint - B.value.uint));
|
|
if(ulpsDiff <= maxUlpsDiff){
|
|
return 0;
|
|
}
|
|
return (ulpsDiff > maxUlpsDiff)?1:-1;
|
|
}
|
|
|
|
bool ieee_float_is_zero(const float a, const float maxDiff){
|
|
return (fabsf(a) <= maxDiff)?true:false;
|
|
}
|
|
|
|
bool ieee_double_is_zero(const double a, const double maxDiff){
|
|
return (fabs(a) <= maxDiff)?true:false;
|
|
}
|
|
|