fix double to float conversion bug

This commit is contained in:
Marty Kraimer
2004-04-05 13:17:30 +00:00
parent 610eb4ea87
commit ff08561ba0
3 changed files with 22 additions and 58 deletions

View File

@@ -22,6 +22,7 @@
#include <math.h>
#include <float.h>
#include "epicsConvert.h"
#include "dbDefs.h"
#include "errlog.h"
#include "cvtFast.h"
@@ -37,20 +38,6 @@
#include "recGbl.h"
#include "dbConvert.h"
/*safe double to float conversion*/
static void safeDoubleToFloat(double *pd,float *pf)
{
double abs = fabs(*pd);
if(*pd==0.0) {
*pf = 0.0;
} else if(abs>=FLT_MAX) {
if(*pd>0.0) *pf = FLT_MAX; else *pf = -FLT_MAX;
} else if(abs<=FLT_MIN) {
if(*pd>0.0) *pf = FLT_MIN; else *pf = -FLT_MIN;
} else {
*pf = *pd;
}
}
/* DATABASE ACCESS GET CONVERSION SUPPORT */
static long getStringString (
@@ -1900,12 +1887,12 @@ static long getDoubleFloat(
double *psrc=(double *)(paddr->pfield);
if(nRequest==1 && offset==0) {
safeDoubleToFloat(psrc,pbuffer);
*pbuffer = epicsConvertDoubleToFloat(*psrc);
return(0);
}
psrc += offset;
while (nRequest) {
safeDoubleToFloat(psrc,pbuffer);
*pbuffer = epicsConvertDoubleToFloat(*psrc);
++psrc; ++pbuffer;
if(++offset==no_elements) psrc=(double *)paddr->pfield;
nRequest--;
@@ -4110,12 +4097,12 @@ static long putDoubleFloat(
float *pdest=(float *)(paddr->pfield);
if(nRequest==1 && offset==0) {
safeDoubleToFloat(pbuffer,pdest);
*pdest = epicsConvertDoubleToFloat(*pbuffer);
return(0);
}
pdest += offset;
while (nRequest) {
safeDoubleToFloat(pbuffer,pdest);
*pdest = epicsConvertDoubleToFloat(*pbuffer);
++pbuffer; ++pdest;
if(++offset==no_elements) pdest=(float *)paddr->pfield;
nRequest--;

View File

@@ -20,6 +20,7 @@
#include <math.h>
#include <float.h>
#include "epicsConvert.h"
#include "dbDefs.h"
#include "errlog.h"
#include "cvtFast.h"
@@ -919,19 +920,7 @@ static long cvt_d_f(
double *from,
float *to,
struct dbAddr *paddr)
{
double abs = fabs(*from);
if(*from==0.0) {
*to = 0.0;
} else if(abs>=FLT_MAX) {
if(*from>0.0) *to = FLT_MAX; else *to = -FLT_MAX;
} else if(abs<=FLT_MIN) {
if(*from>0.0) *to = FLT_MIN; else *to = -FLT_MIN;
} else {
*to = *from;
}
return(0);
}
{ *to = epicsConvertDoubleToFloat(*from); return 0;}
/* Convert Double to Double */
static long cvt_d_d(

View File

@@ -29,6 +29,7 @@
#include <math.h>
#include <float.h>
#include "epicsConvert.h"
#include "dbDefs.h"
#include "errlog.h"
#include "ellLib.h"
@@ -117,19 +118,6 @@ extern unsigned short dbDBRnewToDBRold[DBR_ENUM+1];
#ifndef MAX_STRING_SIZE
#define MAX_STRING_SIZE 40
#endif
/*safe double to float conversion*/
static void safeDoubleToFloat(double *pd,float *pf)
{
double abs = fabs(*pd);
if(abs>=FLT_MAX) {
if(*pd>0.0) *pf = FLT_MAX; else *pf = -FLT_MAX;
} else if(abs<=FLT_MIN) {
if(*pd>0.0) *pf = FLT_MIN; else *pf = -FLT_MIN;
} else {
*pf = *pd;
}
}
/* From $cs/dblib/src/dbiocsubs.c
* subroutines
@@ -686,12 +674,12 @@ void *pfl
pold->precision = new.precision;
strncpy(pold->units,new.units,MAX_UNITS_SIZE);
pold->units[MAX_UNITS_SIZE-1] = '\0';
safeDoubleToFloat(&new.upper_disp_limit,&pold->upper_disp_limit);
safeDoubleToFloat(&new.lower_disp_limit,&pold->lower_disp_limit);
safeDoubleToFloat(&new.upper_alarm_limit,&pold->upper_alarm_limit);
safeDoubleToFloat(&new.upper_warning_limit,&pold->upper_warning_limit);
safeDoubleToFloat(&new.lower_warning_limit,&pold->lower_warning_limit);
safeDoubleToFloat(&new.lower_alarm_limit,&pold->lower_alarm_limit);
pold->upper_disp_limit = epicsConvertDoubleToFloat(new.upper_disp_limit);
pold->lower_disp_limit = epicsConvertDoubleToFloat(new.lower_disp_limit);
pold->upper_alarm_limit = epicsConvertDoubleToFloat(new.upper_alarm_limit);
pold->lower_alarm_limit = epicsConvertDoubleToFloat(new.lower_alarm_limit);
pold->upper_warning_limit = epicsConvertDoubleToFloat(new.upper_warning_limit);
pold->lower_warning_limit = epicsConvertDoubleToFloat(new.lower_warning_limit);
options=0;
nRequest=no_elements;
status = dbGetField(paddr,DBR_FLOAT,&(pold->value),&options,
@@ -859,14 +847,14 @@ void *pfl
pold->precision = new.precision;
strncpy(pold->units,new.units,MAX_UNITS_SIZE);
pold->units[MAX_UNITS_SIZE-1] = '\0';
safeDoubleToFloat(&new.upper_disp_limit,&pold->upper_disp_limit);
safeDoubleToFloat(&new.lower_disp_limit,&pold->lower_disp_limit);
safeDoubleToFloat(&new.upper_alarm_limit,&pold->upper_alarm_limit);
safeDoubleToFloat(&new.upper_warning_limit,&pold->upper_warning_limit);
safeDoubleToFloat(&new.lower_warning_limit,&pold->lower_warning_limit);
safeDoubleToFloat(&new.lower_alarm_limit,&pold->lower_alarm_limit);
safeDoubleToFloat(&new.upper_ctrl_limit,&pold->upper_ctrl_limit);
safeDoubleToFloat(&new.lower_ctrl_limit,&pold->lower_ctrl_limit);
pold->upper_disp_limit = epicsConvertDoubleToFloat(new.upper_disp_limit);
pold->lower_disp_limit = epicsConvertDoubleToFloat(new.lower_disp_limit);
pold->upper_alarm_limit = epicsConvertDoubleToFloat(new.upper_alarm_limit);
pold->lower_alarm_limit = epicsConvertDoubleToFloat(new.lower_alarm_limit);
pold->upper_warning_limit = epicsConvertDoubleToFloat(new.upper_warning_limit);
pold->lower_warning_limit = epicsConvertDoubleToFloat(new.lower_warning_limit);
pold->upper_ctrl_limit = epicsConvertDoubleToFloat(new.upper_ctrl_limit);
pold->lower_ctrl_limit = epicsConvertDoubleToFloat(new.lower_ctrl_limit);
options=0;
nRequest=no_elements;
status = dbGetField(paddr,DBR_FLOAT,&(pold->value),&options,