Support for rational numbers #54
Loading…
x
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Created by: radrow
Rational numbers are essential to clearly represent ratios and proportions, which appear commonly in lots of models and computations. Sophia does not support any kind of numbers other than integrals and I think it could be changed.
How does it look now
While developing some kind of basic bonding curve contract I had to specify
reserve_ratio
. This is the very first place I really faced lack of fractions – I was forced to definemax_reserve_ratio
, and set reserve ratio as "the value that divided bymax_reserve_ratio
will produce actual reserve ratio". This leads to quite ridiculous consequences, because then in Bancor formula I need to take something to the power of that ratio, which requires defining more and more operations on these implicit rationals and pushes all this work to the user. Take a look at this solidity code taken from https://github.com/relevant-community/contracts/blob/bondingCurves/contracts/BancorFormula.sol :Note all these workarounds. The programmer needs to pass nominators and denominators separately to define actually very principal operations. This leads to a lot of unneeded bloat of code and creates a lot of opportunities to introduce bugs.
How do I see it
For a start, handling rationals may be done by a library. Rational number is literally a pair of integers. We may call it either
Frac
,Ratio
,Fractional
orRational
. It is important to point to the user that they are not system floats. However, the language may actually help us here – taking an idea from OCaml we may include new operators:(+.) : frac -> frac -> frac
,(-.)
,(/.)
,(*.)
, because just like in OCaml we have only Hindley-Milner typesystem that won't allow more fancy polymorphism that would allow use of traditional+
-like operators. Sample usage:Some operations will preserve rational body and there is nothing to bother here. There is however some problem with functions that may exit rationals, like
sqrt
, or exponentiation in general as their results can have infinite numerical representation and they will need to be approximated. On the other hand we are not introducing new issue – Sophia programmers still need to deal with it. There is no way to bypass it, but we may make this a bit nicer.From my point of view it would be very handy to have approximation tools in some library that would perform it for user, but that is a different talk.
What would I expect / naming propositions
Contruction:
Rounding:
Operations:
Beside that we can later provide some functions that approximate operations on real numbers, like
Created by: UlfNorell
If you define
you can implement everything except the infix operators as a library.
Created by: radrow
Of course, but my idea is to have an official and unified implementation for this that would be shipped with the language
Created by: UlfNorell
Right, this is something that could go in a standard library of some kind.
Reading this
it sounded like you were saying this is how you have to do it in Sophia at the moment, and I wanted to make it clear that this is not at all true.
Created by: thepiwo
This would be working well as standard library, now that they are integrated overall
Created by: radrow
Right @thepiwo , actually behind your backs I was working on this. I will finish it after I'm back