Problems? Is your data what you think it is? PerlMonks

### Re: decimal to fraction

by vr (Friar)
 on Sep 12, 2017 at 12:49 UTC ( #1199196=note: print w/replies, xml ) Need Help??

in reply to decimal to fraction

In practice, you probably want to stop the loop early

Just a note, for slow learners like me, that otherwise one always gets amazingly correct and on the same scale amazingly useless result :-):

```>perl rat.pl 0.49420098210293
0: 0 / 1 = 0 (-0.5)
2: 1 / 2 = 0.5 (0.006)
42: 42 / 85 = 0.4941176470588235 (-8e-005)
1: 43 / 87 = 0.4942528735632184 (5e-005)
1: 85 / 172 = 0.4941860465116279 (-1e-005)
1: 128 / 259 = 0.4942084942084942 (8e-006)
1: 213 / 431 = 0.494199535962877 (-1e-006)
3: 767 / 1552 = 0.494201030927835 (5e-008)
8: 6349 / 12847 = 0.4942009807737215 (-1e-009)
4: 26163 / 52940 = 0.4942009822440498 (1e-010)
2: 58675 / 118727 = 0.4942009820849512 (-2e-011)
3: 202188 / 409121 = 0.4942009821055385 (3e-012)
2: 463051 / 936969 = 0.4942009821029298 (-2e-016)
4869: 2254797507 / 4562511182 = 0.49420098210293 (8e-021)
5: 11274450586 / 22813492879 = 0.49420098210293 (-2e-021)
1: 13529248093 / 27376004061 = 0.49420098210293 (5e-023)
27: 376564149097 / 761965602526 = 0.49420098210293 (-2e-024)
1: 390093397190 / 789341606587 = 0.49420098210293 (1e-025)
13: 5447778312567 / 11023406488157 = 0.49420098210293 (-9e-028)
9: 49420098210293 / 100000000000000 = 0.49420098210293 (0)

Another surprising note, this little program produces more precise and more 'beautiful' (shorter) approximation, than a built-in verb for same purpose in my latest toy:

```   2 x: 0.49420098210293
2993521666234 6057296069093

(cf. 463051/936969), and changing comparison tolerance (with 9!:19) doesn't help

Replies are listed 'Best First'.
Re^2: decimal to fraction
by huck (Vicar) on Sep 12, 2017 at 13:07 UTC

Heh, While technically correct, IMHO this is kinda cheating

```9: 49420098210293 / 100000000000000 = 0.49420098210293 (0)
and on the same scale amazingly useless result :-):

The numerator and denominator are mutually prime (no common factors), and thus the fraction is fully reduced. How else would you represent it as a fraction?

```#!/usr/bin/env perl -l
use warnings;
use strict;

#####
# [id://56906] for quick gcf
sub gcf {
my (\$x, \$y) = @_;
(\$x, \$y) = (\$y, \$x % \$y) while \$y;
return \$x;
}

######

print gcf(49420098210293,100000000000000);  # prints 1

#####

use Math::Prime::Util qw/is_prime factor/;
print is_prime(49420098210293);             # not prime (I wasn't sure
+ when I started my answer)
local \$, = 'x';
print factor(49420098210293);               # 113x4729x92481709
print factor(100000000000000);              # 2x2x2x2x2x2x2x2x2x2x2x2x
+2x2x5x5x5x5x5x5x5x5x5x5x5x5x5x5
# no common factors

Create A New User
Node Status?
node history
Node Type: note [id://1199196]
help
Chatterbox?
 LanX questions the integerity of this approach. .. LanX doesn't sound natural [LanX]: welcome to a new episode of Big Perl Theory...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2018-03-20 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (250 votes). Check out past polls.

Notices?