On Mon, Mar 15, 2010 at 05:59:08PM -0400, DAVECROSS via RT wrote:
Show quoted text> Michael,
>
> This evening I thought I'd try to add some tests for exponentiation to
> the distribution, just to get a feel for how much work might be involved
> to implement this feature. You can see the test program at:
>
>
http://github.com/davorg/number-fraction/blob/master/t/13_exp.t
>
> I'm very happy to report that the tests all passed without me having to
> change the code at all. So it looks like the module already supports the
> operation you want. I suppose Perl is faking it from the multiplication
> support.
>
> Please try the existing CPAN version and see what you think. If it's not
> working for you in some way, then please send some failing tests so that
> I can investigate further.
>
> Cheers,
Hi Dave ...
I saw your test. I think I did not make myself clear in
what I am requesting. I want the result appear as fractional.
For example, I want:
1/2 ** 3 => 1/8 not 0.125
The following is "failed" test:
==> cut here <==
use Number::Fraction ':constants';
my $f = '1/2';
my $power = '3';
my $answer = $f ** $power;
print "$answer\n";
==> cut here <==
$ perl frac2.pl
0.125
I understand the power is fractional, for example,
2**(1/2) can not be expressed by a fractional.
I only want fractional answer when power is an integer.
I think the answer can be simply expressed as
(a/b)**c = a**c/b**c
that would be fractional format.
The other thing I noticed is the final fraction is expressed
in "non-reduced" form, like, 9/8. I wrote a trivial subroutine
to convert it to "reduced" format. Example: 9/8 => 1 1/8.
I think it is best to add an option: some people wants
reduced format, some don't.
sub reduce {
my $input = shift;
return $input unless $input =~ m{ ^ (-|\+)? (\d+) / (\d+) $}x;
my $sign = $1 || "";
my $a = $2;
my $b = $3;
my $c = 0;
while ( $a > $b ) {
$a = $a - $b;
$c++;
}
if ( $c == 0 ) {
return "${sign}$a/$b";
}
else {
return "${sign}$c $a/$b";
}
}
--
Michael Wang