slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
17.55k stars 601 forks source link

[slint-lang] Missing `log()` math function #4435

Closed abique closed 9 months ago

abique commented 9 months ago

Missing log() math function in slint expression. You have pow(), so you also need log() to calculate the inverse function. You may consider adding exp() too.

What prevent you from adding most of the standard math lib?

abique commented 9 months ago

For some context I want a slider to operate in the log domain, and I'd want those math functions to be present in slint.

ogoffart commented 9 months ago

I believe there is already a log function: https://slint.dev/snapshots/master/docs/slint/src/language/builtins/namespaces#log-float-float-float

abique commented 9 months ago

Ah ! indeed. Though, this one isn't standard. This is the first time I see this signature. I expect log(exp(1)) == 1.

abique commented 9 months ago

I can work with log(a, b), that isn't a problem. I you support overloading, or having a default value for the second argument, I'd encourage it.

Feel free to close this issue.

ogoffart commented 9 months ago

There is no obvious value for the default. 10 or 2 or 2.71828. You said that you expect it to be e, but that is usually called ln not log. The rust stdlib also

What prevent you from adding most of the standard math lib?

Nothing. We could add exp and ln. Every function grow the API surface though. So we don't want to do it just for the sake of it. Slint is not a programming language.

abique commented 9 months ago

There is no obvious value for the default. 10 or 2 or 2.71828. You said that you expect it to be e, but that is usually called ln not log. The rust stdlib also

Yeah you did it like rust did, :ok_hand:.

Though, I consider the standard to be like C++, java, javascript, python and others. I say it is the standard, not because of a technical merit, but just because it is widely known and used.

I predict that I won't be the only one to miss it, and at some point it'll be a balance between the amount of time spent in support vs amount of time of dev work.

About adding more math functions, it is your call. As soon as you want to do some unit conversions, geometry, etc... you don't want to add a callback for everything. Also those math functions are "pure" and will just be a binding to the rust one. I can imagine that their accuracy may vary from one OS/CPU to another but if you use the double version, I think it'll be fine.

Feel free to close this issue, and thank you for answering my question.

ogoffart commented 9 months ago

Thanks for your understanding. Because rust and other language have different default value, this seems to be a valid reason not to have a default value. We could still add that later if lots of people want it. For your Slider in the log domain, you can also use log(_, 10), base 10 is usually more intuitive for UI, but it is true it depends on your specific domain.