Changeset 4574 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/coor/LatLon.java
r4541 r4574 244 244 */ 245 245 public static double roundToOsmPrecision(double value) { 246 return Math.round(value / MAX_SERVER_PRECISION) * MAX_SERVER_PRECISION; // causes tiny rounding errors (see LatLonTest) 247 } 248 249 /** 250 * Returns the value rounded to OSM precisions, i.e. to 251 * LatLon.MAX_SERVER_PRECISION. The result is guaranteed to be exact, but at a great cost. 252 * This function is about 1000 times slower than roundToOsmPrecision(), use it with caution. 253 * 254 * @return rounded value 255 */ 256 public static double roundToOsmPrecisionStrict(double value) { 246 257 double absV = Math.abs(value); 247 258 int numOfDigits = MAX_SERVER_DIGITS + (absV < 1 ? 0 : (absV < 10 ? 1 : (absV < 100 ? 2 : 3))); 248 259 return BigDecimal.valueOf(value).round(new MathContext(numOfDigits)).doubleValue(); 249 //return Math.round(value / MAX_SERVER_PRECISION) * MAX_SERVER_PRECISION; // Old method, causes rounding errors (see LatLonTest) ! 250 } 251 260 } 261 252 262 /** 253 263 * Replies a clone of this lat LatLon, rounded to OSM precisions, i.e. to … … 260 270 roundToOsmPrecision(lat()), 261 271 roundToOsmPrecision(lon()) 272 ); 273 } 274 275 /** 276 * Replies a clone of this lat LatLon, rounded to OSM precisions, i.e. to 277 * MAX_SERVER_PRECISION 278 * 279 * @return a clone of this lat LatLon 280 */ 281 public LatLon getRoundedToOsmPrecisionStrict() { 282 return new LatLon( 283 roundToOsmPrecisionStrict(lat()), 284 roundToOsmPrecisionStrict(lon()) 262 285 ); 263 286 } -
trunk/test/unit/org/openstreetmap/josm/data/coor/LatLonTest.java
r4573 r4574 25 25 26 26 for (double value : sampleValues) { 27 assertEquals(LatLon.roundToOsmPrecision (value), value, 0);27 assertEquals(LatLon.roundToOsmPrecisionStrict(value), value, 0); 28 28 } 29 29 30 assertEquals(LatLon.roundToOsmPrecision (0.0), 0.0, 0);31 assertEquals(LatLon.roundToOsmPrecision (-0.0), 0.0, 0);30 assertEquals(LatLon.roundToOsmPrecisionStrict(0.0), 0.0, 0); 31 assertEquals(LatLon.roundToOsmPrecisionStrict(-0.0), 0.0, 0); 32 32 33 assertEquals(LatLon.roundToOsmPrecision (0.12345678), 0.1234568, 0);34 assertEquals(LatLon.roundToOsmPrecision (0.123456789), 0.1234568, 0);33 assertEquals(LatLon.roundToOsmPrecisionStrict(0.12345678), 0.1234568, 0); 34 assertEquals(LatLon.roundToOsmPrecisionStrict(0.123456789), 0.1234568, 0); 35 35 36 assertEquals(LatLon.roundToOsmPrecision (1.12345678), 1.1234568, 0);37 assertEquals(LatLon.roundToOsmPrecision (1.123456789), 1.1234568, 0);36 assertEquals(LatLon.roundToOsmPrecisionStrict(1.12345678), 1.1234568, 0); 37 assertEquals(LatLon.roundToOsmPrecisionStrict(1.123456789), 1.1234568, 0); 38 38 39 assertEquals(LatLon.roundToOsmPrecision (10.12345678), 10.1234568, 0);40 assertEquals(LatLon.roundToOsmPrecision (10.123456789), 10.1234568, 0);39 assertEquals(LatLon.roundToOsmPrecisionStrict(10.12345678), 10.1234568, 0); 40 assertEquals(LatLon.roundToOsmPrecisionStrict(10.123456789), 10.1234568, 0); 41 41 42 assertEquals(LatLon.roundToOsmPrecision (100.12345678), 100.1234568, 0);43 assertEquals(LatLon.roundToOsmPrecision (100.123456789), 100.1234568, 0);42 assertEquals(LatLon.roundToOsmPrecisionStrict(100.12345678), 100.1234568, 0); 43 assertEquals(LatLon.roundToOsmPrecisionStrict(100.123456789), 100.1234568, 0); 44 44 45 assertEquals(LatLon.roundToOsmPrecision (100.00000001), 100.0000000, 0);46 assertEquals(LatLon.roundToOsmPrecision (100.000000001), 100.0000000, 0);47 assertEquals(LatLon.roundToOsmPrecision (100.0000000001), 100.0000000, 0);48 assertEquals(LatLon.roundToOsmPrecision (100.00000000001), 100.0000000, 0);49 assertEquals(LatLon.roundToOsmPrecision (100.000000000001), 100.0000000, 0);50 assertEquals(LatLon.roundToOsmPrecision (100.0000000000001), 100.0000000, 0);51 assertEquals(LatLon.roundToOsmPrecision (100.00000000000001), 100.0000000, 0);52 assertEquals(LatLon.roundToOsmPrecision (100.000000000000001), 100.0000000, 0);53 assertEquals(LatLon.roundToOsmPrecision (100.0000000000000001), 100.0000000, 0);54 assertEquals(LatLon.roundToOsmPrecision (100.00000000000000001), 100.0000000, 0);55 assertEquals(LatLon.roundToOsmPrecision (100.000000000000000001), 100.0000000, 0);56 assertEquals(LatLon.roundToOsmPrecision (100.0000000000000000001), 100.0000000, 0);57 assertEquals(LatLon.roundToOsmPrecision (100.00000000000000000001), 100.0000000, 0);45 assertEquals(LatLon.roundToOsmPrecisionStrict(100.00000001), 100.0000000, 0); 46 assertEquals(LatLon.roundToOsmPrecisionStrict(100.000000001), 100.0000000, 0); 47 assertEquals(LatLon.roundToOsmPrecisionStrict(100.0000000001), 100.0000000, 0); 48 assertEquals(LatLon.roundToOsmPrecisionStrict(100.00000000001), 100.0000000, 0); 49 assertEquals(LatLon.roundToOsmPrecisionStrict(100.000000000001), 100.0000000, 0); 50 assertEquals(LatLon.roundToOsmPrecisionStrict(100.0000000000001), 100.0000000, 0); 51 assertEquals(LatLon.roundToOsmPrecisionStrict(100.00000000000001), 100.0000000, 0); 52 assertEquals(LatLon.roundToOsmPrecisionStrict(100.000000000000001), 100.0000000, 0); 53 assertEquals(LatLon.roundToOsmPrecisionStrict(100.0000000000000001), 100.0000000, 0); 54 assertEquals(LatLon.roundToOsmPrecisionStrict(100.00000000000000001), 100.0000000, 0); 55 assertEquals(LatLon.roundToOsmPrecisionStrict(100.000000000000000001), 100.0000000, 0); 56 assertEquals(LatLon.roundToOsmPrecisionStrict(100.0000000000000000001), 100.0000000, 0); 57 assertEquals(LatLon.roundToOsmPrecisionStrict(100.00000000000000000001), 100.0000000, 0); 58 58 59 assertEquals(LatLon.roundToOsmPrecision (99.999999999999999999999), 100.0000000, 0);60 assertEquals(LatLon.roundToOsmPrecision (99.99999999999999999999), 100.0000000, 0);61 assertEquals(LatLon.roundToOsmPrecision (99.9999999999999999999), 100.0000000, 0);62 assertEquals(LatLon.roundToOsmPrecision (99.999999999999999999), 100.0000000, 0);63 assertEquals(LatLon.roundToOsmPrecision (99.99999999999999999), 100.0000000, 0);64 assertEquals(LatLon.roundToOsmPrecision (99.9999999999999999), 100.0000000, 0);65 assertEquals(LatLon.roundToOsmPrecision (99.999999999999999), 100.0000000, 0);66 assertEquals(LatLon.roundToOsmPrecision (99.99999999999999), 100.0000000, 0);67 assertEquals(LatLon.roundToOsmPrecision (99.9999999999999), 100.0000000, 0);68 assertEquals(LatLon.roundToOsmPrecision (99.999999999999), 100.0000000, 0);69 assertEquals(LatLon.roundToOsmPrecision (99.99999999999), 100.0000000, 0);70 assertEquals(LatLon.roundToOsmPrecision (99.9999999999), 100.0000000, 0);71 assertEquals(LatLon.roundToOsmPrecision (99.999999999), 100.0000000, 0);72 assertEquals(LatLon.roundToOsmPrecision (99.99999999), 100.0000000, 0);73 assertEquals(LatLon.roundToOsmPrecision (99.9999999), 99.9999999, 0);59 assertEquals(LatLon.roundToOsmPrecisionStrict(99.999999999999999999999), 100.0000000, 0); 60 assertEquals(LatLon.roundToOsmPrecisionStrict(99.99999999999999999999), 100.0000000, 0); 61 assertEquals(LatLon.roundToOsmPrecisionStrict(99.9999999999999999999), 100.0000000, 0); 62 assertEquals(LatLon.roundToOsmPrecisionStrict(99.999999999999999999), 100.0000000, 0); 63 assertEquals(LatLon.roundToOsmPrecisionStrict(99.99999999999999999), 100.0000000, 0); 64 assertEquals(LatLon.roundToOsmPrecisionStrict(99.9999999999999999), 100.0000000, 0); 65 assertEquals(LatLon.roundToOsmPrecisionStrict(99.999999999999999), 100.0000000, 0); 66 assertEquals(LatLon.roundToOsmPrecisionStrict(99.99999999999999), 100.0000000, 0); 67 assertEquals(LatLon.roundToOsmPrecisionStrict(99.9999999999999), 100.0000000, 0); 68 assertEquals(LatLon.roundToOsmPrecisionStrict(99.999999999999), 100.0000000, 0); 69 assertEquals(LatLon.roundToOsmPrecisionStrict(99.99999999999), 100.0000000, 0); 70 assertEquals(LatLon.roundToOsmPrecisionStrict(99.9999999999), 100.0000000, 0); 71 assertEquals(LatLon.roundToOsmPrecisionStrict(99.999999999), 100.0000000, 0); 72 assertEquals(LatLon.roundToOsmPrecisionStrict(99.99999999), 100.0000000, 0); 73 assertEquals(LatLon.roundToOsmPrecisionStrict(99.9999999), 99.9999999, 0); 74 74 } 75 75 }
Note:
See TracChangeset
for help on using the changeset viewer.