Skip to content

Commit 7cc4dce

Browse files
authored
Switch to precise lerp. (#548)
Reviving #278, fixes #275.
1 parent 9a8729d commit 7cc4dce

File tree

19 files changed

+37
-16
lines changed

19 files changed

+37
-16
lines changed

codegen/templates/vec.rs.tera

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1867,7 +1867,7 @@ impl {{ self_t }} {
18671867
#[inline]
18681868
#[must_use]
18691869
pub fn lerp(self, rhs: Self, s: {{ scalar_t }}) -> Self {
1870-
self + ((rhs - self) * s)
1870+
self * (1.0 - s) + rhs * s
18711871
}
18721872

18731873
/// Moves towards `rhs` based on the value `d`.

src/f32/coresimd/vec3a.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ impl Vec3A {
726726
#[inline]
727727
#[must_use]
728728
pub fn lerp(self, rhs: Self, s: f32) -> Self {
729-
self + ((rhs - self) * s)
729+
self * (1.0 - s) + rhs * s
730730
}
731731

732732
/// Moves towards `rhs` based on the value `d`.

src/f32/coresimd/vec4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ impl Vec4 {
713713
#[inline]
714714
#[must_use]
715715
pub fn lerp(self, rhs: Self, s: f32) -> Self {
716-
self + ((rhs - self) * s)
716+
self * (1.0 - s) + rhs * s
717717
}
718718

719719
/// Moves towards `rhs` based on the value `d`.

src/f32/neon/vec3a.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ impl Vec3A {
770770
#[inline]
771771
#[must_use]
772772
pub fn lerp(self, rhs: Self, s: f32) -> Self {
773-
self + ((rhs - self) * s)
773+
self * (1.0 - s) + rhs * s
774774
}
775775

776776
/// Moves towards `rhs` based on the value `d`.

src/f32/neon/vec4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ impl Vec4 {
747747
#[inline]
748748
#[must_use]
749749
pub fn lerp(self, rhs: Self, s: f32) -> Self {
750-
self + ((rhs - self) * s)
750+
self * (1.0 - s) + rhs * s
751751
}
752752

753753
/// Moves towards `rhs` based on the value `d`.

src/f32/scalar/vec3a.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ impl Vec3A {
769769
#[inline]
770770
#[must_use]
771771
pub fn lerp(self, rhs: Self, s: f32) -> Self {
772-
self + ((rhs - self) * s)
772+
self * (1.0 - s) + rhs * s
773773
}
774774

775775
/// Moves towards `rhs` based on the value `d`.

src/f32/scalar/vec4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ impl Vec4 {
827827
#[inline]
828828
#[must_use]
829829
pub fn lerp(self, rhs: Self, s: f32) -> Self {
830-
self + ((rhs - self) * s)
830+
self * (1.0 - s) + rhs * s
831831
}
832832

833833
/// Moves towards `rhs` based on the value `d`.

src/f32/sse2/vec3a.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ impl Vec3A {
769769
#[inline]
770770
#[must_use]
771771
pub fn lerp(self, rhs: Self, s: f32) -> Self {
772-
self + ((rhs - self) * s)
772+
self * (1.0 - s) + rhs * s
773773
}
774774

775775
/// Moves towards `rhs` based on the value `d`.

src/f32/sse2/vec4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ impl Vec4 {
755755
#[inline]
756756
#[must_use]
757757
pub fn lerp(self, rhs: Self, s: f32) -> Self {
758-
self + ((rhs - self) * s)
758+
self * (1.0 - s) + rhs * s
759759
}
760760

761761
/// Moves towards `rhs` based on the value `d`.

src/f32/vec2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ impl Vec2 {
692692
#[inline]
693693
#[must_use]
694694
pub fn lerp(self, rhs: Self, s: f32) -> Self {
695-
self + ((rhs - self) * s)
695+
self * (1.0 - s) + rhs * s
696696
}
697697

698698
/// Moves towards `rhs` based on the value `d`.

src/f32/vec3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ impl Vec3 {
759759
#[inline]
760760
#[must_use]
761761
pub fn lerp(self, rhs: Self, s: f32) -> Self {
762-
self + ((rhs - self) * s)
762+
self * (1.0 - s) + rhs * s
763763
}
764764

765765
/// Moves towards `rhs` based on the value `d`.

src/f32/wasm32/vec3a.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ impl Vec3A {
737737
#[inline]
738738
#[must_use]
739739
pub fn lerp(self, rhs: Self, s: f32) -> Self {
740-
self + ((rhs - self) * s)
740+
self * (1.0 - s) + rhs * s
741741
}
742742

743743
/// Moves towards `rhs` based on the value `d`.

src/f32/wasm32/vec4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ impl Vec4 {
730730
#[inline]
731731
#[must_use]
732732
pub fn lerp(self, rhs: Self, s: f32) -> Self {
733-
self + ((rhs - self) * s)
733+
self * (1.0 - s) + rhs * s
734734
}
735735

736736
/// Moves towards `rhs` based on the value `d`.

src/f64/dvec2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ impl DVec2 {
692692
#[inline]
693693
#[must_use]
694694
pub fn lerp(self, rhs: Self, s: f64) -> Self {
695-
self + ((rhs - self) * s)
695+
self * (1.0 - s) + rhs * s
696696
}
697697

698698
/// Moves towards `rhs` based on the value `d`.

src/f64/dvec3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ impl DVec3 {
759759
#[inline]
760760
#[must_use]
761761
pub fn lerp(self, rhs: Self, s: f64) -> Self {
762-
self + ((rhs - self) * s)
762+
self * (1.0 - s) + rhs * s
763763
}
764764

765765
/// Moves towards `rhs` based on the value `d`.

src/f64/dvec4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ impl DVec4 {
816816
#[inline]
817817
#[must_use]
818818
pub fn lerp(self, rhs: Self, s: f64) -> Self {
819-
self + ((rhs - self) * s)
819+
self * (1.0 - s) + rhs * s
820820
}
821821

822822
/// Moves towards `rhs` based on the value `d`.

tests/vec2.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,13 @@ macro_rules! impl_vec2_float_tests {
843843
assert_approx_eq!($vec2::ZERO, v0.lerp(v1, 0.5));
844844
});
845845

846+
glam_test!(test_lerp_big_difference, {
847+
let v0 = $vec2::new(-1e30, -1e30);
848+
let v1 = $vec2::new(16.0, 16.0);
849+
assert_approx_eq!(v0, v0.lerp(v1, 0.0));
850+
assert_approx_eq!(v1, v0.lerp(v1, 1.0));
851+
});
852+
846853
glam_test!(test_move_towards, {
847854
let v0 = $vec2::new(-1.0, -1.0);
848855
let v1 = $vec2::new(1.0, 1.0);

tests/vec3.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,13 @@ macro_rules! impl_vec3_float_tests {
987987
assert_approx_eq!($vec3::ZERO, v0.lerp(v1, 0.5));
988988
});
989989

990+
glam_test!(test_lerp_big_difference, {
991+
let v0 = $vec3::new(-1e30, -1e30, -1e30);
992+
let v1 = $vec3::new(16.0, 16.0, 16.0);
993+
assert_approx_eq!(v0, v0.lerp(v1, 0.0));
994+
assert_approx_eq!(v1, v0.lerp(v1, 1.0));
995+
});
996+
990997
glam_test!(test_move_towards, {
991998
let v0 = $vec3::new(-1.0, -1.0, -1.0);
992999
let v1 = $vec3::new(1.0, 1.0, 1.0);

tests/vec4.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,13 @@ macro_rules! impl_vec4_float_tests {
11241124
assert_approx_eq!($vec4::ZERO, v0.lerp(v1, 0.5));
11251125
});
11261126

1127+
glam_test!(test_lerp_big_difference, {
1128+
let v0 = $vec4::new(-1e30, -1e30, -1e30, -1e30);
1129+
let v1 = $vec4::new(16.0, 16.0, 16.0, 16.0);
1130+
assert_approx_eq!(v0, v0.lerp(v1, 0.0));
1131+
assert_approx_eq!(v1, v0.lerp(v1, 1.0));
1132+
});
1133+
11271134
glam_test!(test_move_towards, {
11281135
let v0 = $vec4::new(-1.0, -1.0, -1.0, -1.0);
11291136
let v1 = $vec4::new(1.0, 1.0, 1.0, 1.0);

0 commit comments

Comments
 (0)