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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |