Perl: the Markov chain saw PerlMonks

How Perl6 treating decimals as rationals helped me solve a problem

by grondilu (Friar)
 on Nov 16, 2012 at 00:04 UTC Need Help??

Hi monks,

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.

• Comment on How Perl6 treating decimals as rationals helped me solve a problem

Replies are listed 'Best First'.
Re: How Perl6 treating decimals as rationals helped me solve a problem
by choroba (Bishop) on Nov 16, 2012 at 01:57 UTC
Grr, do I really have to solve all the simple problems first? :-)
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1004081]
Approved by davido
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2018-03-17 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (225 votes). Check out past polls.

Notices?