I was watching the World Poker Tournament on tv a few days ago, and after playing a little on yahoo, decided to learn more about how to play the game competently.

This ultimately involves calculating the odds using fractions, which converts into decimal values. While I like to work with percentages when I'm thinking about financial stuff, when it comes to poker, I would prefer to see 1/2 rather than 0.500, or even 15/17 rather than 0.881.

After (I make this mistake too often) I wrote a little script to do this for me, I super-searched "fractions" and found a very good node by tilly.

My script has a couple of features that are decent for the application. It's pretty simple and fits on one page, it produces a text file with relatively small output that's still fairly useful, and if you really want to, its precision can be adjusted.

(By default it returns the first fraction found whose value is within 0.005 of the target.)

Here's the code:

#!/usr/bin/perl -w use strict; #purpose: find simple fractions that are close to percentages... #divisions, tolerance my $div = 1000; my $tol = 0.005; my $high = 25; my $file = 'tolerance.txt'; my $last = ''; #no duplicate fractions in output open FILE, ">$file" or die; DIV:for (1 .. $div) { my $val = $_ / $div; #0.001, for example for ( 1 .. $high) { my $den = $_; #denominator for (1 .. $den) { #numerator my $here = $_/$den; #number #check for within tolerance of value if (($val-$tol <= $here) and ($here <= $val + $tol)) { my $test = "$_/$den"; #string if ($last ne $test) { #unique fractions only $last = $test; print FILE "$val:\t$test\n"; } next DIV; #avoid 1/1, .. 5/5 for 1.000 } } } } close FILE;

The first few lines of output:

0.035: 1/25 0.037: 1/24 0.039: 1/23 0.041: 1/22 0.043: 1/21 0.046: 1/20 0.048: 1/19

In reply to Percentages to Fractions by David Caughell

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.