Ticket #7740: roundToOsmPrecision.patch

File roundToOsmPrecision.patch, 2.1 KB (added by mrwojo, 12 years ago)

also made roundToOsmPrecisionStrict() call roundToOsmPrecision()

  • src/org/openstreetmap/josm/data/coor/LatLon.java

    ### Eclipse Workspace Patch 1.0
    #P JOSM
     
    1010import static java.lang.Math.toRadians;
    1111import static org.openstreetmap.josm.tools.I18n.trc;
    1212
    13 import java.math.BigDecimal;
    14 import java.math.MathContext;
    1513import java.text.DecimalFormat;
    1614import java.text.NumberFormat;
    1715import java.util.Locale;
     
    3432     * The API returns 7 decimals.
    3533     */
    3634    public static final double MAX_SERVER_PRECISION = 1e-7;
     35    public static final double MAX_SERVER_INV_PRECISION = 1e7;
    3736    public static final int    MAX_SERVER_DIGITS = 7;
    3837
    3938    private static DecimalFormat cDmsMinuteFormatter = new DecimalFormat("00");
     
    264263     * @return rounded value
    265264     */
    266265    public static double roundToOsmPrecision(double value) {
    267         return Math.round(value / MAX_SERVER_PRECISION) * MAX_SERVER_PRECISION; // causes tiny rounding errors (see LatLonTest)
     266        return Math.round(value * MAX_SERVER_INV_PRECISION) / MAX_SERVER_INV_PRECISION;
    268267    }
    269268
    270269    /**
    271      * Returns the value rounded to OSM precisions, i.e. to
    272      * LatLon.MAX_SERVER_PRECISION. The result is guaranteed to be exact, but at a great cost.
    273      * This function is about 1000 times slower than roundToOsmPrecision(), use it with caution.
     270     * Returns the value rounded to OSM precision. This function is now the same as
     271     * {@link #roundToOsmPrecision(double)}, since the rounding error has been fixed.
    274272     *
    275273     * @return rounded value
    276274     */
    277275    public static double roundToOsmPrecisionStrict(double value) {
    278         double absV = Math.abs(value);
    279         int numOfDigits = MAX_SERVER_DIGITS + (absV < 1 ? 0 : (absV < 10 ? 1 : (absV < 100 ? 2 : 3)));
    280         return BigDecimal.valueOf(value).round(new MathContext(numOfDigits)).doubleValue();
     276        return roundToOsmPrecision(value);
    281277    }
    282278
    283279    /**