Jump to content

Wikifunctions:Type proposals/Percentage

From Wikifunctions

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

  1. 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.
  2. We could allow unsimplified fractions.
  3. 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
  4. Code conversion could of course be changed to something different from the Rational number type
  5. We might not want to have a parser, and instead just display/input the raw structure

Comments