Opened 15 years ago

Closed 15 years ago

#2036 closed defect (fixed)

CPLAtof() - different results than atof()

Reported by: warmerdam Owned by: Mateusz Łoskot
Priority: high Milestone: 1.5.2
Component: OGR_SRS Version: svn-trunk
Severity: major Keywords: CPLAtof
Cc: dron, Mateusz Łoskot, msieczka, Markus Neteler, warmedam


The following precision different between atof() and CPLAtof() is having annoying "knock on" affects on generation of EPSG definition files.

warmerda@amd64[45]% cat test1.cpp 
#include "gdal.h"
#include "cpl_conv.h"

int main()

        double x1, x2;
        const char *str = "6356566.435";

        x1 = atof(str);
        x2 = CPLAtof(str);

        printf( "%.16g, %.16g\n", x1, x2 );
warmerda@amd64[46]% ./test1
6356566.435, 6356566.435000001
(gdb) run
Starting program: /wrk/home/warmerda/test1 
[Thread debugging using libthread_db enabled]
[New Thread 46912564802784 (LWP 354)]
[Switching to Thread 46912564802784 (LWP 354)]

Breakpoint 1, main () at test1.cpp:8
8               const char *str = "6356566.435";
(gdb) n
10              x1 = atof(str);
11              x2 = CPLAtof(str);
13              printf( "%.16g, %.16g\n", x1, x2 );
(gdb) p x1
$1 = 6356566.4349999996
(gdb) p x2
$2 = 6356566.4350000005

Change History (14)

comment:1 by dron, 15 years ago

Owner: changed from warmerdam to dron
Status: newassigned

I have reproduced this problem and also constructed few more cases when CPLAtof() results differs from the libc's atof(): some of my tests started to fail when I reduced the tolerance threshold to 0.0000000000000001. It seems that CPLAtof() typically overestimates the value, but the standard atof() underestimates it.

I will take a look what can be done about that (though, the whole floating point topic is a delicate matter).

By the way, maybe we should not use two different atof() implementations in the same software? It is pretty easy to add the locale aware variant of the CPLAtof() and stop using the standard atof() completely. Otherwise it is possible that we will constantly come to issues like that in the future.

Best regards, Andrey

comment:2 by Mateusz Łoskot, 15 years ago

Cc: Mateusz Łoskot added

comment:3 by msieczka, 15 years ago

Cc: msieczka added
Priority: normalhigh

This also affect PROJ 4.6.0.

Compare the PROJ.4's epsg file in version 4.5.0 and 4.6.0, and see how parameters differ in many SRS definitions, eg. EPSG 26794-26798, 2180, 2255-2262, 2057, 2000-2007 and more. Eg.:

<4009> +proj=longlat +a=6378450.047548896 +b=6356826.621488444 +no_defs <>

<4009> +proj=longlat +a=6378450.047548897 +b=6356826.621488445 +no_defs  <>

<2965> +proj=tmerc +lat_0=37.5 +lon_0=-85.66666666666667 +k=0.999967 +x_0=99999.99989839978 +y_0=249999.9998983998 +ellps=GRS80 +datum=NAD83 +to_meter=0.3048006096012192 +no_defs  <>

<2965> +proj=tmerc +lat_0=37.5 +lon_0=-85.66666666666667 +k=0.999966667 +x_0=99999.99989839978 +y_0=249999.9998983998 +ellps=GRS80 +datum=NAD83 +to_meter=0.3048006096012192 +no_defs  <>

<2180> +proj=tmerc +lat_0=0 +lon_0=19 +k=0.999300 +x_0=500000 +y_0=-5300000 +ellps=GRS80 +units=m +no_defs  <>

<2180> +proj=tmerc +lat_0=0 +lon_0=19 +k=0.9993000000000001 +x_0=500000 +y_0=-5300000 +ellps=GRS80 +units=m +no_defs  <>

comment:4 by Markus Neteler, 15 years ago

Cc: Markus Neteler added

comment:5 by dron, 15 years ago

The problem is in the way how _Ldmul() function works. To fix it that function (or, more generally, _Ldtento()) should be reimplemented. I will think what can I do about that.

comment:6 by dron, 15 years ago

Resolution: fixed
Status: assignedclosed

This is should be fixed now with the r14379.

comment:7 by msieczka, 15 years ago

Resolution: fixed
Severity: normalmajor
Status: closedreopened

Please backport this fix to stable branch, as it's Milestone is 1.5.2.

comment:8 by mloskot, 15 years ago

Owner: changed from dron to Mateusz Łoskot
Status: reopenednew

I'm taking the porting task on myself

comment:9 by Even Rouault, 15 years ago

Cc: warmedam added


I'd suggest you to wait a bit about #2036 as there's a pending issue with the fact that CPLStrtof and CPLStrtofDelim have been removed. See

comment:10 by mloskot, 15 years ago


You are right. I'll wait until this issue is clarified.

comment:11 by dron, 15 years ago

Folks, I have restored these two functions back. See r14464.

comment:12 by msieczka, 15 years ago

Hi Mateusz

Do you think it would be now possible for you to backport the fix into the stable branch?

Cheers, Maciek

comment:13 by mloskot, 15 years ago

Status: newassigned

comment:14 by mloskot, 15 years ago

Resolution: fixed
Status: assignedclosed

Ported recent fixes to branches/1.5 (r14568). Closing.

Note: See TracTickets for help on using tickets.