Skip to content

Commit

Permalink
Prevent precision loss when converting integer vectors to doubles
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
zml2008 committed Aug 15, 2021
1 parent 8912ded commit 379366c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public final class Vector2{{ E }} implements Vector{{ E }}, Comparable<Vector2{{

@Override
public Vector2d toDouble() {
return new Vector2d(this.x, this.y);
return new Vector2d((double) this.x, (double) this.y);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public final class Vector3{{ E }} implements Vector{{ E }}, Comparable<Vector3{{

@Override
public Vector3d toDouble() {
return new Vector3d(this.x, this.y, this.z);
return new Vector3d((double) this.x, (double) this.y, (double) this.z);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public final class Vector4{{ E }} implements Vector{{ E }}, Comparable<Vector4{{

@Override
public Vector4d toDouble() {
return new Vector4d(this.x, this.y, this.z, this.w);
return new Vector4d((double) this.x, (double) this.y, (double) this.z, (double) this.w);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.spongepowered.math.test.{{ EDecEquiTest }};
import org.spongepowered.math.test.TestUtili;
import org.spongepowered.math.vector.Vector2{{ E }};
import org.spongepowered.math.vector.Vector3{{ E }};
import org.spongepowered.math.vector.Vector3d;
import org.spongepowered.math.vector.Vector4{{ E }};
import org.spongepowered.math.vector.VectorN{{ E }};

Expand Down Expand Up @@ -396,4 +397,9 @@ public class Vector3{{ E }}Test {
Assertions.assertFalse(new Vector3{{ E }}(122, 43, 96).equals(new Vector3{{ E }}(378, 95, 96)));
}

@Test
void testPrecisionKept() {
Assertions.assertEquals(new Vector3{{ E }}(29999999, 0, 0).toDouble(), new Vector3d(29999999d, 0, 0));
}

}

0 comments on commit 379366c

Please sign in to comment.