Wikifunctions:Type proposals/Percentage
Many functions currently use the rational number type, but they actually only accept numbers from 0-1. A type explicitly restricted to this would be helpful for these functions. The most obvious use case for this is probability, but it can also be used for things like completion percent, and for functions where the renderer would like to display a percent.
Structure/Validator
The sign is not necessary, as all numbers greater than or equal to 0 are supported. This means that we only need 2 natural numbers for keys, with en labels "numerator" and "denominator." I think that, before the type is opened, we need a validator to enforce that:
- The denominator must be greater than the numerator or both must be 1
- The greatest common denominator of the numerator and denominator must be equal to 1.
Parser and renderer
Parser
Any value that would be accepted by the Rational number parser would be accepted.
Renderer
Would display the value as a percent, with only as many digits as needed, and only 5 digits beyond the decimal point to be displayed.
Code conversion
Exactly the same as Rational number.
Alternatives
- The existing Rational number type, but, I think that the case where a value from 0-1 is required is too frequent to just have a Rational number.
- We could allow unsimplified fractions.
- We could instead store this as a natural number to store the value, and another one (or a sign??) to represent where the decimal is
- Code conversion could of course be changed to something different from the Rational number type
- We might not want to have a parser, and instead just display/input the raw structure
Comments
Self opposing on preference to validation of inputs. Feeglgeef (talk) 20:04, 2 December 2024 (UTC)Support as proposer
- wait until we know if there is a better way to constrain input ranges than to make a whole new type. --99of9 (talk) 11:44, 20 January 2025 (UTC)