# Behaviour of 'expectApproxEqAbs()'

**URL:** <https://ziggit.dev/t/behaviour-of-expectapproxeqabs/2769>\
**Category:** Help\
**Tags:** standard-library\
**Created:** [January 7, 2024, 9:35pm UTC](https://ziggit.dev/t/behaviour-of-expectapproxeqabs/2769 "2024-01-07T21:35:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johnathan](https://ziggit.dev/letter_avatar_proxy/v4/letter/j/67e7ee/32.png) [@Johnathan](https://ziggit.dev/u/Johnathan)\
**Post date:** [January 7, 2024, 9:35pm UTC](https://ziggit.dev/t/behaviour-of-expectapproxeqabs/2769/1 "2024-01-07T21:35:27Z")

</div>

To test that a function returns roughly what is expected, the standard library’s

```zig
inline fn expectApproxEqAbs(expected: anytype, actual: anytype, tolerance: anytype) !void

```

looks promising. In master (0.12) there is a problem in my understanding for the following simple case (`test.zig`):

```zig
const std = @import("std");

fn thrice(number: f32) f32 {
    return 3.0 * number;
}
    
test "Expecting this to compile and to pass" {
     try std.testing.expectApproxEqAbs(3.0, thrice(1.000001), 0.01);
}

```

I.e., `$ zig test test.zig`

```zig
test.zig:8:50: error: unable to resolve comptime value
    try std.testing.expectApproxEqAbs(3.0, thrice(1.000001), 0.01);
                                           ~~~~~~^~~~~~~~~~
test.zig:8:50: note: value being casted to 'comptime_float' must be comptime-known

```

Wouldn’t the function’s return type (`f32`) make second argument’s type comptime known? Why must its value also be comptime known?

Switch the first two arguments and the test passes:

```zig
const std = @import("std");

fn thrice(number: f32) f32 {
    return 3.0 * number;
}
    
test "Expecting this to compile and to pass" {
     try std.testing.expectApproxEqAbs(thrice(1.000001), 3.0, 0.01);
}

```

`$ zig test testworks.zig`

```zig
All 1 tests passed.

```

Although the value 3.0 would be the expected result within the specified tolerance, why does it have to be passed as `actual` to make the test work?

---

<div class="post-metadata">

**Author:** ![castholm](https://ziggit.dev/user_avatar/ziggit.dev/castholm/32/1330_2.png) [@castholm](https://ziggit.dev/u/castholm)\
**Post date:** [January 7, 2024, 9:50pm UTC](https://ziggit.dev/t/behaviour-of-expectapproxeqabs/2769/2 "2024-01-07T21:50:11Z")

</div>

> [@Johnathan](#):
>
> looks promising. In master (0.12) there is a problem in my understanding for the following simple case (`test.zig`):
> 
> ```zig
> const std = @import("std");
> 
> fn thrice(number: f32) f32 {
> return 3.0 * number;
> }
>     
> test "Expecting this to compile and to pass" {
> try std.testing.expectApproxEqAbs(3.0, thrice(1.000001), 0.01);
> }
> 
> ```

Your first example compiles and runs just fine for me using a very recent master (`0.12.0-dev.2046+d3a163f86`). What version of master are you using? The new signatures for `expectEqual` and related functions were only merged into master three days ago, so if your version is older than that you still have the old

```zig
fn expectApproxEqAbs(expected: anytype, actual: @TypeOf(expected), tolerance: @TypeOf(expected)) !void

```

version of `expectApproxEqAbs`.

---

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [January 7, 2024, 10:31pm UTC](https://ziggit.dev/t/behaviour-of-expectapproxeqabs/2769/3 "2024-01-07T22:31:54Z")

</div>

> [@Johnathan](#):
>
> `try std.testing.expectApproxEqAbs(3.0, thrice(1.000001), 0.01);`

Maybe:

```zig
try std.testing.expectApproxEqAbs(@as(f32, 3.0), thrice(1.000001), 0.01);

```

A float literal like `3.0` is a comptime\_float so the test expects a comptime\_float from `thrice`. Basically the first argument dictates what type the test expects to compare.

---

<div class="post-metadata">

**Author:** ![Johnathan](https://ziggit.dev/letter_avatar_proxy/v4/letter/j/67e7ee/32.png) [@Johnathan](https://ziggit.dev/u/Johnathan)\
**Post date:** [January 8, 2024, 12:04am UTC](https://ziggit.dev/t/behaviour-of-expectapproxeqabs/2769/4 "2024-01-08T00:04:14Z")

</div>

Confirmed! It was a slightly older version (`0.12.0-dev.1856+94c63f31f`), and the expected bahaviour is obtained for `0.12.0-dev.2063+804cee3b9`.
