Changeset 5268 in josm for trunk


Ignore:
Timestamp:
2012-06-08T11:20:48+02:00 (7 years ago)
Author:
simon04
Message:

fix #7740 - Fix roundToOsmPrecision() accuracy (patch by mrwojo)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/coor/LatLon.java

    r5235 r5268  
    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;
     
    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
     
    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)
    268     }
    269 
    270     /**
    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.
     266        return Math.round(value * MAX_SERVER_INV_PRECISION) / MAX_SERVER_INV_PRECISION;
     267    }
     268
     269    /**
     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
Note: See TracChangeset for help on using the changeset viewer.