Reputation: 186
I having written a perl code for a function e^x = 1 + x/1! + x^2/2! + x^3/3!+.. + x^n/n!
consider values for x = 1 and n =10. My problem is I am getting correct values for the numerator and denominator part but while dividing them into $factor. I am not getting the decimal values. Could you please correct me where I am making mistake.
($x, $n) = @ARGV; # for x=1 , n =10
say "\n Your entered values are ", $x, " ", $n;
for my $i (1..$n)
{
$numerator = $x**$i;
$denominator = Math::BigInt->new($i)->bfac();
$factor = int($numerator / $denominator); # tried it without typecasting then too noluck
$exp = $exp + $factor; #[$numerator/$denominator];
say $i, "\n Numerator :", $numerator, "Denominator :", $denominator, " Factor :", $factor, " EXP :", $exp;
$i++;
}
Upvotes: 0
Views: 326
Reputation: 6573
This worked for me using bignum
.
#!/usr/bin/perl
use strict;
use warnings;
use 5.014;
use bignum;
my ($x, $n) = @ARGV; # for x=1 , n =10
say "\n Your entered values are ", $x, " ", $n;
my $exp;
for my $i (1..$n)
{
my $numerator = $x**$i;
my $denominator = Math::BigInt->new($i)->bfac();
my $factor = $numerator / $denominator; # tried it without typecasting then too noluck
$exp += $factor; #[$numerator/$denominator];
say $i, "\n Numerator : ", $numerator, " Denominator :", $denominator, " Factor :", $factor, " EXP :", $exp;
}
Output was:
C:\Old_Data\perlp>perl t7.pl 1 10
Your entered values are 1 10
1
Numerator : 1 Denominator :1 Factor :1 EXP :1
2
Numerator : 1 Denominator :2 Factor :0.5 EXP :1.5
3
Numerator : 1 Denominator :6 Factor :0.1666666666666666666666666666666666666667 EXP :1.6666666666666666666666666666666666666667
4
Numerator : 1 Denominator :24 Factor :0.04166666666666666666666666666666666666667 EXP :1.70833333333333333333333333333333333333337
5
Numerator : 1 Denominator :120 Factor :0.008333333333333333333333333333333333333333 EXP :1.716666666666666666666666666666666666666703
6
Numerator : 1 Denominator :720 Factor :0.001388888888888888888888888888888888888889 EXP :1.718055555555555555555555555555555555555592
7
Numerator : 1 Denominator :5040 Factor :0.0001984126984126984126984126984126984126984 EXP :1.7182539682539682539682539682539682539682904
8
Numerator : 1 Denominator :40320 Factor :0.0000248015873015873015873015873015873015873 EXP :1.7182787698412698412698412698412698412698777
9
Numerator : 1 Denominator :362880 Factor :0.000002755731922398589065255731922398589065256 EXP :1.718281525573192239858906525573192239858942956
10
Numerator : 1 Denominator :3628800 Factor :0.0000002755731922398589065255731922398589065256 EXP :1.7182818011463844797178130511463844797178494816
Upvotes: 0
Reputation: 8492
Well, you're deliberately casting to int()
. That's throwing away anything after the decimal place. Take away the int()
and you should be fine.
$numerator = 1;
$denominator = 2;
$factor = $numerator / $denominator;
print $factor;
prints 0.5
for me.
EDIT: This is a bit of a hack, but I found the real problem -- Math::BigInt
always does integer division, no matter what you do with the other operator. You can fix this by doing:
use Math::BigFloat;
[...]
$denominator = Math::BigFloat->new(Math::BigInt->new($i)->bfac());
Upvotes: 4
Reputation: 95242
The problem is that your denominator is a Math::BigInt
. Dividing an int by one of those always yields an integer result. If you use Math::BigFloat instead, it will work. Or, if you want exact rational numbers, use Math::BigRat.
Upvotes: 1