Opened 14 years ago

Last modified 6 years ago

#312 new defect

Incorrect formatting with negative value of decimal places — at Initial Version

Reported by: mloskot Owned by: strk
Priority: major Milestone: GEOS Fund Me
Component: Core Version: main
Severity: Significant Keywords: precision model
Cc:

Description

Testing GEOS PrecisionModel and WKTWriter with assumption taken from JTS implementation of jts/geom/PrecisionMode.java (version 1.7)

 /* For example, to specify 3 decimal places of precision, use a scale factor
  * of 1000. To specify -3 decimal places of precision (i.e. rounding to
  * the nearest 1000), use a scale factor of 0.001.
  */

Using scale value 0.001 leads to garbage formatter %.-1f which behaves differently with various C implementations. Here is test program that mimics formatting performed by GEOS:

#include <cmath>
#include <cstdio>
#include <string>
#include <iostream>

int main()
{
    double scale = 0.001; // -3 decimal places

    // PrecisionModel::getMaximumSignificantDigits
    const int digits = 1 + (int)std::ceil(std::log(scale) / std::log(double(10.0)));
    std::cout << digits << std::endl;

    // WKTWriter::createFormatter
    std::string fmt("%.");
    {
        char buffer[255] = { 0 };
        sprintf(buffer, "%i", digits);
        fmt.append(buffer);
        fmt.append("f");
        std::cout << fmt << std::endl;
    }

    // WKTWriter::writeNumber
    {
        const double n = 12345.54321;
        char buffer[255] = { 0 };
        sprintf(buffer, fmt.c_str(), n);  // writes n as -1f
        std::cout << buffer << std::endl;
    }

    return 0;
}

Change History (0)

Note: See TracTickets for help on using tickets.