@@ -1317,26 +1317,28 @@ test "lossyCast" {
1317
1317
///
1318
1318
/// This does not guarantee returning *b* if *t* is 1 due to floating-point errors.
1319
1319
/// This is monotonic.
1320
- pub fn lerp (comptime T : type , a : T , b : T , t : T ) T {
1321
- if (@typeInfo (T ) != .Float and @typeInfo (T ) != .ComptimeFloat )
1320
+ pub fn lerp (a : anytype , b : anytype , t : anytype ) @TypeOf (a , b , t ) {
1321
+ const T = @TypeOf (a , b , t );
1322
+
1323
+ comptime if (! std .meta .trait .isFloat (T ))
1322
1324
@compileError ("T must be a float type" );
1323
1325
1324
1326
assert (t >= 0 and t <= 1 );
1325
1327
return @mulAdd (T , b - a , t , a );
1326
1328
}
1327
1329
1328
1330
test "lerp" {
1329
- try testing .expectEqual (@as (f64 , 75 ), lerp (f64 , 50 , 100 , 0.5 ));
1330
- try testing .expectEqual (@as (f32 , 43.75 ), lerp (f32 , 50 , 25 , 0.25 ));
1331
- try testing .expectEqual (@as (f64 , -31.25 ), lerp (f64 , -50 , 25 , 0.25 ));
1331
+ try testing .expectEqual (@as (f64 , 75 ), lerp (50 , 100 , 0.5 ));
1332
+ try testing .expectEqual (@as (f32 , 43.75 ), lerp (50 , 25 , 0.25 ));
1333
+ try testing .expectEqual (@as (f64 , -31.25 ), lerp (-50 , 25 , 0.25 ));
1332
1334
1333
- try testing .expectApproxEqRel (@as (f32 , -7.16067345e+03 ), lerp (f32 , -10000.12345 , -5000.12345 , 0.56789 ), 1e-19 );
1334
- try testing .expectApproxEqRel (@as (f64 , 7.010987590521e+62 ), lerp (f64 , 0.123456789e-64 , 0.123456789e64 , 0.56789 ), 1e-33 );
1335
+ try testing .expectApproxEqRel (@as (f32 , -7.16067345e+03 ), lerp (-10000.12345 , -5000.12345 , 0.56789 ), 1e-19 );
1336
+ try testing .expectApproxEqRel (@as (f64 , 7.010987590521e+62 ), lerp (0.123456789e-64 , 0.123456789e64 , 0.56789 ), 1e-33 );
1335
1337
1336
- try testing .expectEqual (@as (f32 , 0.0 ), lerp (f32 , 1.0e8 , 1.0 , 1.0 ));
1337
- try testing .expectEqual (@as (f64 , 0.0 ), lerp (f64 , 1.0e16 , 1.0 , 1.0 ));
1338
- try testing .expectEqual (@as (f32 , 1.0 ), lerp (f32 , 1.0e7 , 1.0 , 1.0 ));
1339
- try testing .expectEqual (@as (f64 , 1.0 ), lerp (f64 , 1.0e15 , 1.0 , 1.0 ));
1338
+ try testing .expectEqual (@as (f32 , 0.0 ), lerp (@as ( f32 , 1.0e8 ) , 1.0 , 1.0 ));
1339
+ try testing .expectEqual (@as (f64 , 0.0 ), lerp (@as ( f64 , 1.0e16 ) , 1.0 , 1.0 ));
1340
+ try testing .expectEqual (@as (f32 , 1.0 ), lerp (@as ( f32 , 1.0e7 ) , 1.0 , 1.0 ));
1341
+ try testing .expectEqual (@as (f64 , 1.0 ), lerp (@as ( f64 , 1.0e15 ) , 1.0 , 1.0 ));
1340
1342
}
1341
1343
1342
1344
/// Returns the maximum value of integer type T.
0 commit comments