Sema: error on ambiguous coercion of comptime float and ints
The following, from the documentation as of the time of writing, illustrates
the problem:
```zig
// Compile time coercion of float to int
test "implicit cast to comptime_int" {
var f: f32 = 54.0 / 5;
_ = f;
}
```
It is not clear how to unify the types of 54.0 and 5 to perform the
division. We can either
- cast 54.0 to comptime_int resulting in @as(comptime_int, 10), which is
casted to @as(f32, 10), or
- cast 5 to comptime_float resulting in @as(comptime_float, 10.8), which
is casted to @as(f32, 10.8)
Since the two resulting values are different, a compiler error is appropriate.
If we know that casting to either type will result in the same value we
don't need to error. For instance, 10.0 / 2 is okay, as is 10 / 2.0.
Fixes: #12364
This commit is contained in:
committed by
Andrew Kelley
parent
070282a96e
commit
07f64a2e13
@@ -194,8 +194,8 @@ fn testSin() !void {
|
||||
const eps = epsForType(ty);
|
||||
try expect(@sin(@as(ty, 0)) == 0);
|
||||
try expect(math.approxEqAbs(ty, @sin(@as(ty, std.math.pi)), 0, eps));
|
||||
try expect(math.approxEqAbs(ty, @sin(@as(ty, std.math.pi / 2)), 1, eps));
|
||||
try expect(math.approxEqAbs(ty, @sin(@as(ty, std.math.pi / 4)), 0.7071067811865475, eps));
|
||||
try expect(math.approxEqAbs(ty, @sin(@as(ty, std.math.pi / 2.0)), 1, eps));
|
||||
try expect(math.approxEqAbs(ty, @sin(@as(ty, std.math.pi / 4.0)), 0.7071067811865475, eps));
|
||||
}
|
||||
|
||||
{
|
||||
@@ -228,8 +228,8 @@ fn testCos() !void {
|
||||
const eps = epsForType(ty);
|
||||
try expect(@cos(@as(ty, 0)) == 1);
|
||||
try expect(math.approxEqAbs(ty, @cos(@as(ty, std.math.pi)), -1, eps));
|
||||
try expect(math.approxEqAbs(ty, @cos(@as(ty, std.math.pi / 2)), 0, eps));
|
||||
try expect(math.approxEqAbs(ty, @cos(@as(ty, std.math.pi / 4)), 0.7071067811865475, eps));
|
||||
try expect(math.approxEqAbs(ty, @cos(@as(ty, std.math.pi / 2.0)), 0, eps));
|
||||
try expect(math.approxEqAbs(ty, @cos(@as(ty, std.math.pi / 4.0)), 0.7071067811865475, eps));
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user