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