|Welcome to the Monastery|
How Perl6 treating decimals as rationals helped me solve a problemby grondilu (Friar)
|on Nov 16, 2012 at 00:04 UTC||Need Help??|
I was about to solve this rosalind.info problem using perl5, but while I was writing I realized there were at least two features in Perl6 that could be usefull if I want to spare a few lines of code.
So I wrote my solution in Perl6, and it passed on first try. Execution took a bit of time (about a minute or so) but it was well in the five minutes delay so that was fine. It was pretty easy. About ten lines of code.
On rosalind, once you solved a problem you can join a discussion thread where you can post your solution and see other people's ones, with comments. So I did that as I do usually.
And then I was intrigued because most people were complaining about how this problem was a pain in the xxx because of the floating point approximations.
At first I did not understand. But then, thinking about it, I realised that this problem indeed requires computing a lot of differences between decimal numbers and then comparing those differences. It sure is the kind of things that creates issues when using floating point numbers.
Yet I had totally failed to notice that this problem could occur. So how was it possible that my code worked?
Well, I then remember that Perl6 treats decimals as rationals, not floating points, and I guessed this was the answer.
I also remember that I once wondered if that was not a bit of a luxury to do that in Perl6. But now I'm totally convinced it is a good idea. A decimal should really be treated as a rational, unless explicitly casted otherwise.
I won't say that's the first time Perl6 impresses me. But hey, kind of.