Stream: ideas

Topic: Num function for getting the difference between two numbers


view this post on Zulip Richard Feldman (Apr 06 2023 at 00:46):

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

view this post on Zulip Richard Feldman (Apr 06 2023 at 00:46):

does that seem like a reasonable addition? if so, what should it be called?

view this post on Zulip Richard Feldman (Apr 06 2023 at 00:47):

Num.diff x y was my first thought, but in math "difference" means normal subtraction without absolute value

view this post on Zulip Richard Feldman (Apr 06 2023 at 00:47):

Num.delta maybe?

view this post on Zulip Ajai Nelson (Apr 06 2023 at 00:49):

Maybe Num.distance?

view this post on Zulip Ajai Nelson (Apr 06 2023 at 00:53):

Or maybe Num.absoluteDifference (https://en.wikipedia.org/wiki/Absolute_difference)

view this post on Zulip Richard Feldman (Apr 06 2023 at 01:00):

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:

view this post on Zulip Folkert de Vries (Apr 06 2023 at 07:52):

see also https://doc.rust-lang.org/std/primitive.usize.html#method.abs_diff

view this post on Zulip Kiryl Dziamura (Apr 11 2023 at 10:59):

Here's PR
https://github.com/roc-lang/roc/pull/5275

It's my first contribution, so I have a couple of questions:

  1. I'm not sure regarding test exhaustiveness - how far should I go with testing different numeric types and their combinations?
  2. Should I update solve/tests/solve_expr.rs in scope of this task? How the inference test will look like for this function then?
  3. LLVM optimization: in the rust codebase, they check the input size, if it's 1 Byte - they make a trick
    a. Is it possible to do such thing using Roc only (looks like it has everything for that)?
    b. How to test if the optimization is applied?
    c. And in general, should it be done in Roc? Maybe Zig is a better choice here?

view this post on Zulip Folkert de Vries (Apr 11 2023 at 11:01):

I think it's usefult to test for 128-bit values as well, they are a bit special in the backends

view this post on Zulip Folkert de Vries (Apr 11 2023 at 11:02):

but otherwise, i64 and f64 are sufficient

view this post on Zulip Folkert de Vries (Apr 11 2023 at 11:02):

solve tests are not needed here, type checking is also tested by the gen tests

view this post on Zulip Folkert de Vries (Apr 11 2023 at 11:02):

and the llvm optimization does not really seem worth it

view this post on Zulip Folkert de Vries (Apr 11 2023 at 11:03):

I think implementing in roc is fine here. There is not much to gain by using zig

view this post on Zulip Kiryl Dziamura (Apr 11 2023 at 11:06):

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

view this post on Zulip Folkert de Vries (Apr 11 2023 at 11:07):

this is a tradeoff. we want to test, but tests take time

view this post on Zulip Folkert de Vries (Apr 11 2023 at 11:07):

I think it makes sense to just run the tests for all backends here

view this post on Zulip Kiryl Dziamura (Apr 11 2023 at 11:41):

and when to use the indoc! macro?
seems I figured it out. can be ignored for one-liners

view this post on Zulip Kiryl Dziamura (Apr 12 2023 at 14:14):

mono IR tests are failed. I guess I should commit the generated txts, right?

view this post on Zulip Folkert de Vries (Apr 12 2023 at 14:15):

yes, exactly

view this post on Zulip Kiryl Dziamura (Apr 12 2023 at 14:44):

ok, updated the branch. all tests now should be green. it's time to read other readmes than the CONTRIBUTING.md :)

view this post on Zulip Kiryl Dziamura (Apr 18 2023 at 21:06):

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.

view this post on Zulip Folkert de Vries (Apr 18 2023 at 21:54):

those are dev backend tests, which you can run with cargo test-gen-dev gen_num::test_name

view this post on Zulip Folkert de Vries (Apr 18 2023 at 21:54):

in the dev backend, arithmetic does not panic yet, because we have not implemented that

view this post on Zulip Folkert de Vries (Apr 18 2023 at 21:56):

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