this code works fine if x and y are signed numbers:
Num.abs (x - y)
however, if x and y are unsigned, this might crash if y happens to be greater than x.
What I'm really trying to express here is "I want to get the magnitude of difference between these two numbers" - and it seems reasonable to have a Num builtin function to do this which Just Works regardless of whether the numbers are signed or not
does that seem like a reasonable addition? if so, what should it be called?
Num.diff x y was my first thought, but in math "difference" means normal subtraction without absolute value
Num.delta maybe?
Maybe Num.distance?
Or maybe Num.absoluteDifference (https://en.wikipedia.org/wiki/Absolute_difference)
or Num.absDiff for short? Might make it more discoverable in editor autocomplete, because when you go to write Num.abs it's the next suggestion :thinking:
see also https://doc.rust-lang.org/std/primitive.usize.html#method.abs_diff
Here's PR
https://github.com/roc-lang/roc/pull/5275
It's my first contribution, so I have a couple of questions:
solve/tests/solve_expr.rs in scope of this task? How the inference test will look like for this function then?I think it's usefult to test for 128-bit values as well, they are a bit special in the backends
but otherwise, i64 and f64 are sufficient
solve tests are not needed here, type checking is also tested by the gen tests
and the llvm optimization does not really seem worth it
I think implementing in roc is fine here. There is not much to gain by using zig
Ah, another question: is there a rule of thumb for features to activate in tests?
#[cfg(any(feature = "gen-llvm", feature = "gen-dev", feature = "gen-wasm"))]
I used all of them but I see there are tests that uses only a couple
this is a tradeoff. we want to test, but tests take time
I think it makes sense to just run the tests for all backends here
and when to use the indoc! macro?
seems I figured it out. can be ignored for one-liners
mono IR tests are failed. I guess I should commit the generated txts, right?
yes, exactly
ok, updated the branch. all tests now should be green. it's time to read other readmes than the CONTRIBUTING.md :)
I found tests are failed anyway in CI.
cargo test is passed on my machine, and if I try the command that fails in CI - I have a completely different result because of the arm arch of my laptop (almost all tests are failed in that case).
What would be the strategy for fixing the broken tests? Blind handpicking and rerunning CI sounds not good.
those are dev backend tests, which you can run with cargo test-gen-dev gen_num::test_name
in the dev backend, arithmetic does not panic yet, because we have not implemented that
so you should make the tests ignore the dev backend, by changing lines like this
#[cfg(any(feature = "gen-llvm", feature = "gen-dev", feature = "gen-wasm"))]
into
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
Last updated: Jun 16 2026 at 16:19 UTC