[DGD] Arithmetic with objects and int/float

nikoroleva natasha.i.koroleva at gmail.com
Thu May 2 12:59:50 CEST 2019


Before I could set out to learn cloud-server, I needed a problem that
interested me and something that wasn't obviously beyond my ability to
solve programmatically. I fumbled around with several ideas before
settling on a question that comes up from time to time while playing
role-playing games (at least, among the folks I play with): dice
probabilities. I started with that specific question and then started
generalising.

I arrived at the point where I wanted to express probabilities in
rational form. I wrote an object type with two integer attributes:
numerator and denominator. Give it a probability (a float), and it
gives a rational representation of that number. It does so using a
continued fraction, the limits of which I arrived at experimentally
rather than through some understanding of floats.

Continuing to generalise, I started implementing rational arithmetic.
Here several questions arise, some of them having to do with floats
and compounding errors. I think the best thing to do is to store the
original float in the rational object type and use it directly in any
arithmetic operations.

Another question has more to do with the arithmetic. With the +
operator implemented in the rational object type, I can do r + i where
r is the rational object and i is an int. Mathematically, I should
just as well be able to do i + r. Is it worthwhile writing code to
enable such an expression? How would you do it?

I started to explore this idea using a number object type that can be
an int, float, castable string, or rational object. Personally and so
far, I don't find it worth much more than scratching a curiosity itch.
But I'm happy to hear your thoughts on these things.

Regards


More information about the DGD mailing list