@@ -1285,26 +1285,28 @@ test "lossyCast" {
1285
1285
///
1286
1286
/// This does not guarantee returning *b* if *t* is 1 due to floating-point errors.
1287
1287
/// This is monotonic.
1288
- pub fn lerp (comptime T : type , a : T , b : T , t : T ) T {
1289
- if (@typeInfo (T ) != .Float and @typeInfo (T ) != .ComptimeFloat )
1288
+ pub fn lerp (a : anytype , b : anytype , t : anytype ) @TypeOf (a , b , t ) {
1289
+ const T = @TypeOf (a , b , t );
1290
+
1291
+ comptime if (! std .meta .trait .isFloat (T ))
1290
1292
@compileError ("T must be a float type" );
1291
1293
1292
1294
assert (t >= 0 and t <= 1 );
1293
1295
return @mulAdd (T , b - a , t , a );
1294
1296
}
1295
1297
1296
1298
test "lerp" {
1297
- try testing .expectEqual (@as (f64 , 75 ), lerp (f64 , 50 , 100 , 0.5 ));
1298
- try testing .expectEqual (@as (f32 , 43.75 ), lerp (f32 , 50 , 25 , 0.25 ));
1299
- try testing .expectEqual (@as (f64 , -31.25 ), lerp (f64 , -50 , 25 , 0.25 ));
1299
+ try testing .expectEqual (@as (f64 , 75 ), lerp (50 , 100 , 0.5 ));
1300
+ try testing .expectEqual (@as (f32 , 43.75 ), lerp (50 , 25 , 0.25 ));
1301
+ try testing .expectEqual (@as (f64 , -31.25 ), lerp (-50 , 25 , 0.25 ));
1300
1302
1301
- try testing .expectApproxEqRel (@as (f32 , -7.16067345e+03 ), lerp (f32 , -10000.12345 , -5000.12345 , 0.56789 ), 1e-19 );
1302
- try testing .expectApproxEqRel (@as (f64 , 7.010987590521e+62 ), lerp (f64 , 0.123456789e-64 , 0.123456789e64 , 0.56789 ), 1e-33 );
1303
+ try testing .expectApproxEqRel (@as (f32 , -7.16067345e+03 ), lerp (-10000.12345 , -5000.12345 , 0.56789 ), 1e-19 );
1304
+ try testing .expectApproxEqRel (@as (f64 , 7.010987590521e+62 ), lerp (0.123456789e-64 , 0.123456789e64 , 0.56789 ), 1e-33 );
1303
1305
1304
- try testing .expectEqual (@as (f32 , 0.0 ), lerp (f32 , 1.0e8 , 1.0 , 1.0 ));
1305
- try testing .expectEqual (@as (f64 , 0.0 ), lerp (f64 , 1.0e16 , 1.0 , 1.0 ));
1306
- try testing .expectEqual (@as (f32 , 1.0 ), lerp (f32 , 1.0e7 , 1.0 , 1.0 ));
1307
- try testing .expectEqual (@as (f64 , 1.0 ), lerp (f64 , 1.0e15 , 1.0 , 1.0 ));
1306
+ try testing .expectEqual (@as (f32 , 0.0 ), lerp (@as ( f32 , 1.0e8 ) , 1.0 , 1.0 ));
1307
+ try testing .expectEqual (@as (f64 , 0.0 ), lerp (@as ( f64 , 1.0e16 ) , 1.0 , 1.0 ));
1308
+ try testing .expectEqual (@as (f32 , 1.0 ), lerp (@as ( f32 , 1.0e7 ) , 1.0 , 1.0 ));
1309
+ try testing .expectEqual (@as (f64 , 1.0 ), lerp (@as ( f64 , 1.0e15 ) , 1.0 , 1.0 ));
1308
1310
}
1309
1311
1310
1312
/// Returns the maximum value of integer type T.
0 commit comments