-
Notifications
You must be signed in to change notification settings - Fork 945
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
Math functions Min/Max #84
Conversation
Nice 👍 |
I'm always happy to see a function added 10 years late. Better than never! 🍆 |
Was it really necessary to add a Transform subcommand? |
No, I only did this for the sake of completeness. |
You could have made if for BIF_DECL(BIF_MinMax)
{
// ...
} will be usable for |
I would be more inclined to merge this if the Transform sub-commands were removed. There is no sense in adding code and documentation to extend a deprecated command. |
I've removed the commit for adding the Transform sub-commands. |
Should 1 parameter be allowed, and return the same value? |
Why not? 👍 |
This is why not: to assist error detection. |
I have reduced the parameter minimum to 1. Besides the fact that I don't know how to use the current code to provide a different minimum parameter handling between the variadic and explicit variants (for example whether the first parameter is variadic or an array), I think the minimum number of parameters for both variants should be the same to avoid confusion. Since |
That is acceptable. Variadic parameters are expanded before the function is called. It is not feasible to differentiate between the two cases. |
The typecast to double causes loss of precision (and counter-intuitive results) when dealing with integers. The functions are incapable of differentiating between certain integers, with the result instead depending on the order of parameters:
Given the current behaviour of math operators with mixed types, it would make sense to treat all inputs as floating-point and produce a floating-point result if any one input is floating-point. (By contrast, I'd guess that simply checking the type on each iteration might produce inconsistent results.) The check
|
Thanks for the suggestions. They helped me to improve my understanding of the code. To avoid inconsistent results due different number types, I've reworked the code. Integers and floats are now compared separately to determine the lowest/highest value respectively. Only then, the found integer and the found float are compared with each other. |
These functions
and their Transform counterpartsare used to determine the lowest or highest value of several numeric input values.While Transform only supports two input values,the functions acceptmore than twoone or more input values (up to 10000 if all parameters are explicitly specified, or quasi-infinite if an array is passed as a variadic parameter). If one the input values is non-numeric, a blank value is returned. The return value inherits the number type (integer or float) of the highest/lowest input value.To the best of my knowledge, I have tried to keep the code performant and small. Perhaps it can still be improved. There should be no breaking changes, since already existing functions of the same name overwrite the built-in ones.
I think it is a great convenience for the user to use these functions instead of using the ternary operator (or a loop for more than two values).
If you are interested in merging, I will write the corresponding documentation.
Examples: