rhaiscript / rhai

Rhai - An embedded scripting language for Rust.
https://crates.io/crates/rhai
Apache License 2.0
3.81k stars 179 forks source link

More functions in `BasicMathPackage` like `min(x, y)`, `max(x, y)`, ... #700

Closed LetsMelon closed 1 year ago

LetsMelon commented 1 year ago

It would be nice if the BasicMathPackage implements more basic functions, like some functions processing implements by default.

from processings docs:

other functions from the docs: (but I'm not quit sure if they would be really necessary and maybe they should go into a separate package for vectors)

(Sorry if I missed a already implemented function from the packages and I'm in love with this crate, so easy to use)

schungx commented 1 year ago

There is this: https://github.com/rhaiscript/rhai-sci

Perhaps it is better to expand rhai-sci?

LetsMelon commented 1 year ago

I think these functions would better fit in the BasicMathPackage because of the simplicity of the functions and the comprehensive area of ​​application.

e.g.: pixel mapping from a float value with the range 0.0 - 1.0 to an 0.0 - 255.0 range -> map(1.0, 0.0, 1.0, 0.0, 255.0) [1]


[1] maybe a better implementation of map can cast between float and integer so that you can do this: map(1.0, 0.0, 1.0, 0, 255)) an store the result in rust as u8

schungx commented 1 year ago

It probably won't be Basic... if we put it in the standard library, we probably would need to create something like MoreMathPackage etc.

Actually I see many of your proposed functions already in rhai-sci so perhaps it is not a good idea to duplicate.

And the remaining ones are really not very standard and have specific uses in specific cases.

LetsMelon commented 1 year ago

Yeah duplicates with rhai-sci would be a terrible solution in every point. But I think it's also not a great solution to add another crate to a project to have a function like min(x, y) and max(x, y).

And where is the line if a function should go into a package distributed with rhai (and enabled by default) or with a seperate crate (like rhai-sci)?

LetsMelon commented 1 year ago

But nevertheless a official and tested distribution of the missing (or more, or less, ...) in rhai-sci would be better than none.

LetsMelon commented 1 year ago

But nevertheless a official and tested distribution of the missing (or more, or less, ...) in rhai-sci would be better than none.

And then maybe it would also be a great idea to mention rhai-sci in the docs page for Numeric Functions.

schungx commented 1 year ago

There are more things to consider. For example, the max function takes two numbers.

Normally, in the standard library, numbers can interoperate, and there are INT, FLOAT, Decimal.

So we're looking at nine combinations and so nine versions of the function. Seven if excluding float+decimal.

This falls into the gray area where we need to ponder whethrr to add max and min. Because they can easily be replaced by an if expression.

schungx commented 1 year ago

Alright, come to think of it, I agree that max and min probably should belong to the standard library, considering that operators like > and < are defined for numbers.

I'll add them. Like all the other operators, floating-point can also inter-operate with integers for max and min.