Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
task-jp opened this issue Dec 18, 2024 · 4 comments
Labels
diagnostics Suggestions for better diagnostics

Comments

@task-jp
Copy link
Contributor

task-jp commented Dec 18, 2024

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 task-jp added bug Something isn't working need triaging Issue that the owner of the area still need to triage labels Dec 18, 2024
@task-jp
Copy link
Contributor Author

task-jp commented Dec 18, 2024

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

@ogoffart
Copy link
Member

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.

@tronical
Copy link
Member

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.

@ogoffart ogoffart changed the title Math.abs postfix style doesn't work for angle Add a warning when taking the negative value of .abs() on a literal. (eg -1.abs()) Dec 18, 2024
@ogoffart ogoffart added diagnostics Suggestions for better diagnostics priority:low Lowest priority. The issue is kept open for tracking purpose, but noone is actively working on this and removed bug Something isn't working need triaging Issue that the owner of the area still need to triage priority:low Lowest priority. The issue is kept open for tracking purpose, but noone is actively working on this labels Dec 18, 2024
@task-jp
Copy link
Contributor Author

task-jp commented Dec 18, 2024

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 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().

ogoffart added a commit that referenced this issue Dec 19, 2024
ogoffart added a commit that referenced this issue Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
diagnostics Suggestions for better diagnostics
Projects
None yet
Development

No branches or pull requests

3 participants