From 58cbb9fe4f89b9febd780f7bdcfa4c2bb74a723e Mon Sep 17 00:00:00 2001 From: Kristian Evers Date: Sun, 6 May 2018 17:43:26 +0300 Subject: Replace int typecasts with calls to lround to avoid bad conversions on NaN input. Added test to check for those cases. --- src/PJ_unitconvert.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/PJ_unitconvert.c') diff --git a/src/PJ_unitconvert.c b/src/PJ_unitconvert.c index a076b76c..53bf5e81 100644 --- a/src/PJ_unitconvert.c +++ b/src/PJ_unitconvert.c @@ -66,6 +66,8 @@ Last update: 2017-05-16 #define PJ_LIB__ #include #include + +#include "proj_math.h" #include "proj_internal.h" #include "projects.h" @@ -152,14 +154,14 @@ static double decimalyear_to_mjd(double decimalyear) { /*********************************************************************** Epoch of modified julian date is 1858-11-16 00:00 ************************************************************************/ - int year; + long year; double fractional_year; double mjd; if( decimalyear < -10000 || decimalyear > 10000 ) return 0; - year = (int)floor(decimalyear); + year = lround(floor(decimalyear)); fractional_year = decimalyear - year; mjd = (year - 1859)*365 + 14 + 31; mjd += fractional_year*days_in_year(year); @@ -228,9 +230,9 @@ static double yyyymmdd_to_mjd(double yyyymmdd) { Date given in YYYY-MM-DD format. ************************************************************************/ - int year = (int)floor( yyyymmdd / 10000 ); - int month = (int)floor((yyyymmdd - year*10000) / 100); - int day = (int)floor( yyyymmdd - year*10000 - month*100); + long year = lround(floor( yyyymmdd / 10000 )); + long month = lround(floor((yyyymmdd - year*10000) / 100)); + long day = lround(floor( yyyymmdd - year*10000 - month*100)); double mjd = daynumber_in_year(year, month, day); for (year -= 1; year > 1858; year--) -- cgit v1.2.3