Skip to content

Commit

Permalink
Double precision in Distance (#213)
Browse files Browse the repository at this point in the history
* Use double for Distance precision

* Use double difference in PackedAtlas
  • Loading branch information
matthieun authored and MikeGost committed Sep 12, 2018
1 parent 2a799f3 commit 5ca51b5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1181,12 +1181,12 @@ protected Long nodeIdentifierForEnlargedLocation(final Location location,
{
final Distance distance1 = location.distanceTo(node1.getLocation());
final Distance distance2 = location.distanceTo(node2.getLocation());
final long difference = distance2.asMillimeters() - distance1.asMillimeters();
if (difference > 0)
final double difference = distance2.asMillimeters() - distance1.asMillimeters();
if (difference > 0.0)
{
return 1;
}
else if (difference < 0)
else if (difference < 0.0)
{
return -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class Distance implements Serializable

private static final int DISTANCE_PRINTING_METERS_THRESHOLD = 1000;

private final long millimeters;
private final double millimeters;

public static Distance feet(final double feet)
{
Expand All @@ -58,15 +58,15 @@ public static Distance kilometers(final double kilometers)

public static Distance meters(final double meters)
{
return Distance.millimeters((long) (meters * MILLIMETERS_PER_METER));
return Distance.millimeters(meters * MILLIMETERS_PER_METER);
}

public static Distance miles(final double miles)
{
return Distance.feet(miles * FEET_PER_MILE);
}

public static Distance millimeters(final long millimeters)
public static Distance millimeters(final double millimeters)
{
return new Distance(millimeters);
}
Expand All @@ -76,7 +76,7 @@ public static Distance nauticalMiles(final double nauticalMiles)
return Distance.meters(nauticalMiles * METERS_PER_NAUTICAL_MILE);
}

private Distance(final long millimeters)
private Distance(final double millimeters)
{
if (millimeters < 0)
{
Expand All @@ -102,15 +102,15 @@ public double asKilometers()

public double asMeters()
{
return (double) asMillimeters() / MILLIMETERS_PER_METER;
return asMillimeters() / MILLIMETERS_PER_METER;
}

public double asMiles()
{
return asFeet() / FEET_PER_MILE;
}

public long asMillimeters()
public double asMillimeters()
{
return this.millimeters;
}
Expand Down Expand Up @@ -138,7 +138,7 @@ public boolean equals(final Object obj)
@Override
public int hashCode()
{
return Long.hashCode(this.millimeters);
return Double.hashCode(this.millimeters);
}

public boolean isGreaterThan(final Distance that)
Expand Down Expand Up @@ -178,7 +178,7 @@ public Distance scaleBy(final Ratio ratio)

public Distance substract(final Distance that)
{
final long delta = this.asMillimeters() - that.asMillimeters();
final double delta = this.asMillimeters() - that.asMillimeters();
return Distance.millimeters(Math.max(delta, 0));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void testAdd()
public void testConversion()
{
final Distance tenMiles = Distance.miles(10);
Assert.assertEquals(16093440, tenMiles.asMillimeters());
Assert.assertEquals(16093440.0, tenMiles.asMillimeters(), 5);
Assert.assertEquals(52800.0, tenMiles.asFeet(), 0);
Assert.assertEquals(16.09344, tenMiles.asKilometers(), 0);
Assert.assertEquals(8.6897624, tenMiles.asNauticalMiles(), 1e-7);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void testCompare()
// initialization methods you choose
Assert.assertFalse(
Speed.kilometersPerHour(1.000009).isSlowerThan(Speed.kilometersPerHour(1.000008)));
Assert.assertTrue(Speed.kilometersPerHour(1.000009)
Assert.assertFalse(Speed.kilometersPerHour(1.000009)
.isSlowerThanOrEqualTo(Speed.kilometersPerHour(1.000008)));
}

Expand All @@ -42,12 +42,21 @@ public void testConversion()
Assert.assertTrue(Speed.milesPerHour(1).asKilometersPerHour() - 1.6093440 < 0.0001);
}

@Test
public void testDuration()
{
final Speed speed = Speed.metersPerSecond(3.0);
final Distance distance = Distance.meters(6.0);
Assert.assertEquals(Duration.seconds(2.0), speed.asDuration(distance));
}

@Test
public void testEquals()
{
Assert.assertEquals(Speed.kilometersPerHour(9.999999), Speed.kilometersPerHour(9.999999));
Assert.assertNotEquals(Speed.kilometersPerHour(9.99999), Speed.kilometersPerHour(9.99998));
Assert.assertEquals(Speed.kilometersPerHour(9.9999999), Speed.kilometersPerHour(9.9999998));
Assert.assertNotEquals(Speed.kilometersPerHour(9.9999999),
Speed.kilometersPerHour(9.9999998));
}

@Test
Expand All @@ -62,12 +71,4 @@ public void testOperations()
Assert.assertEquals(Speed.kilometersPerHour(50).subtract(Speed.kilometersPerHour(100)),
Speed.kilometersPerHour(0));
}

@Test
public void testDuration()
{
final Speed speed = Speed.metersPerSecond(3.0);
final Distance distance = Distance.meters(6.0);
Assert.assertEquals(Duration.seconds(2.0), speed.asDuration(distance));
}
}

0 comments on commit 5ca51b5

Please sign in to comment.