Ticket #2036 (closed defect: fixed)

Opened 6 years ago

Last modified 5 years ago

CPLAtof() - different results than atof()

Reported by: warmerdam Owned by: mloskot
Priority: high Milestone: 1.5.2
Component: OGR_SRS Version: svn-trunk
Severity: major Keywords: CPLAtof
Cc: dron, mloskot, msieczka, neteler, warmedam

Description

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);
(gdb) 
11              x2 = CPLAtof(str);
(gdb) 
13              printf( "%.16g, %.16g\n", x1, x2 );
(gdb) p x1
$1 = 6356566.4349999996
(gdb) p x2
$2 = 6356566.4350000005

Change History

Changed 6 years ago by dron

  • owner changed from warmerdam to dron
  • status changed from new to assigned

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

Changed 6 years ago by mloskot

  • cc mloskot added

Changed 5 years ago by msieczka

  • cc msieczka added
  • priority changed from normal to high

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.:

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

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

4.5.0:
<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  <>

4.6.0:
<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  <>

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

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

Changed 5 years ago by neteler

  • cc neteler added

Changed 5 years ago by dron

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.

Changed 5 years ago by dron

  • status changed from assigned to closed
  • resolution set to fixed

This is should be fixed now with the r14379.

Changed 5 years ago by msieczka

  • status changed from closed to reopened
  • resolution fixed deleted
  • severity changed from normal to major

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

Changed 5 years ago by mloskot

  • owner changed from dron to mloskot
  • status changed from reopened to new

I'm taking the porting task on myself

Changed 5 years ago by rouault

  • cc warmedam added

Mateusz,

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  http://lists.osgeo.org/pipermail/gdal-dev/2008-May/017031.html

Changed 5 years ago by mloskot

Even,

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

Changed 5 years ago by dron

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

Changed 5 years ago by msieczka

Hi Mateusz

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

Cheers, Maciek

Changed 5 years ago by mloskot

  • owner changed from mloskot to mloskot
  • status changed from new to assigned

Changed 5 years ago by mloskot

  • status changed from assigned to closed
  • resolution set to fixed

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

Note: See TracTickets for help on using tickets.