DEV Community

Discussion on: Write a function that shows off something unique or interesting about the language you're using

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

Leaf stores literals (constants) as rational numbers and is type safe.

good is assigned the value 1 -- there is no precision loss on the division.

err produces a compiler error since 1/3*2 is not an integer.

Collapse
 
dubyabrian profile image
W. Brian Gourlie

Woah. How do rational numbers interact with irrational numbers in this case? Are they approximated prior to any sort of arithmetic or are they considered incompatible types?

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

π and 1/2 were my two motivating reasons for making this feature.

For irrationals, and other fractions, the system will switch to a high-precision floating point mode instead of a rational -- I honestly don't remember what the trigger criteria actually is, I think size of the rational.

What this allows however is that π is a single constant you can define to 100 decimal places. This is high enough precision that several constant operations, basic math, can work on this value without losing precision when converting to the actual system type. It doesn't matter if you convert to a 32/64/128-bit float, it'll have the full precision for that type. No need for constants per type, or stuff like M_PI2.

Note this precision only applies to constants. Runtime variables are limited by the standard system types. Sometime later I'll add these numbers are runtime, but they're fairly special purpose at that point. The literal folding now is enough to cover the current intended use-cases.