slint icon indicating copy to clipboard operation
slint copied to clipboard

Add a warning when taking the negative value of `.abs()` on a literal. (eg `-1.abs()`)

Open task-jp opened this issue 1 year ago • 4 comments

Bug Description

-1deg.abs() returns -1deg whereas Math.abs(-1deg) returns 1deg.

I expect -1deg.abs() equals 1deg.

Reproducible Code (if applicable)

export component AbsAngle inherits VerticalLayout {
    Text {
        text: -1deg.abs() / 1deg;  // => -1
    }
    Text {
        text: Math.abs(-1deg) / 1deg; // => 1
    }
}

Environment Details

  • Slint Version:
  • Platform/OS: Linux/X11
  • Programming Language: .slint
  • Backend/Renderer:

Product Impact

No response

task-jp avatar Dec 18 '24 06:12 task-jp

Is - applied to the result of 1deg.abs()?

task-jp avatar Dec 18 '24 06:12 task-jp

Right, -1deg.abs() is the same as -(1deg.abs()). This is the same precedence rules as in Rust, C++, and so on.

We can't change the precedence rules because then then things like -self.foo wouldn't work anymore because that would become (-self).foo

If anything we could detect this specific case and have a warning.

ogoffart avatar Dec 18 '24 08:12 ogoffart

If anything we could detect this specific case and have a warning.

That might be an option, I agree. Otherwise I'd say that this isn't a bug.

tronical avatar Dec 18 '24 08:12 tronical

We may improve the documentation for that.

https://github.com/slint-ui/slint/blob/ee6f78fc8df32fc1cb07585792eb703782f76f49/docs/astro/src/content/docs/reference/global-functions/math.mdx says

T type
Many of the math functions can be used with any <Link type="NumericTypes" label="numeric type"/> such as angle, duration, float, int, length, and percent. These are represented on this page as T.

General Math Functions

abs(T) -> T

Return the absolute value, where T is a numeric type.

Math.abs(-10); // returns 10
abs(-10); // returns 10
-10.abs(); // returns 10

This looks like I can write -10deg.abs().

task-jp avatar Dec 18 '24 10:12 task-jp