Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

How Perl6 treating decimals as rationals helped me solve a problem

by grondilu (Friar)
on Nov 16, 2012 at 00:04 UTC ( #1004081=perlmeditation: print w/replies, xml ) Need Help??

Hi monks,

I was about to solve this 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 (Chancellor) on Nov 16, 2012 at 01:57 UTC
    Grr, do I really have to solve all the simple problems first? :-)
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1004081]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2017-08-18 00:20 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (294 votes). Check out past polls.