Math Functions in Perl
am 05.11.2007 14:00:35 von coolchick
Hi All,
I am trying to compute few equations using perl.
This is the equation that I am trying to solve:
MI=171-5.2*ln(aveV)-0.23*aveV(g)-16.2*ln(aveLOC)
+50sin(sqrt(2.46*perCM))
I have all the variables: aveV, aveV(g), aveLOC and perCM.
So the equation is:
$MI=171 - (5.2*(2.303*log($aveV)))-(0.23*$ecc)-
(16.2*(2.303*log($aveLOC)))+(50*(sin(sqrt(2.46*$perCM))));
where:
$aveV= 239.530014548124
$ecc=9
$aveLOC=47
$perCM=0.297872340425532
I am not getting the correct answer. I get MI=-2.56217048794884, which
is not correct.
I tried to break it down and the problem is that I don't think I have
the right libraries for math.
It is not computing sqrt, log, and sin correctly.
I am using:
use Math::Complex;
use Math::Trig;
Any help would be great!
Thanks,
naureen
Re: Math Functions in Perl
am 05.11.2007 14:33:38 von jurgenex
coolchick wrote:
> MI=171-5.2*ln(aveV)-0.23*aveV(g)-16.2*ln(aveLOC)
> +50sin(sqrt(2.46*perCM))
>
> I have all the variables: aveV, aveV(g), aveLOC and perCM.
> So the equation is:
>
> $MI=171 - (5.2*(2.303*log($aveV)))-(0.23*$ecc)-
> (16.2*(2.303*log($aveLOC)))+(50*(sin(sqrt(2.46*$perCM))));
>
> where:
> $aveV= 239.530014548124
> $ecc=9
> $aveLOC=47
> $perCM=0.297872340425532
>
> I am not getting the correct answer. I get MI=-2.56217048794884, which
> is not correct.
This is a long shot, but is any of the iterim values exceedingly large or
small? If so then you fell victim to a variation of 'perldoc -q 999'.
The easiest remedy would be to rewrite the equation to avoid very
large/small iterim values. This is not trivial, but there is a reason why
Computer Numerics is a class of its own at university.
jue
Re: Math Functions in Perl
am 05.11.2007 15:54:13 von Glenn Jackman
At 2007-11-05 08:00AM, "coolchick" wrote:
> Hi All,
>
> I am trying to compute few equations using perl.
>
> This is the equation that I am trying to solve:
>
> MI=171-5.2*ln(aveV)-0.23*aveV(g)-16.2*ln(aveLOC)
> +50sin(sqrt(2.46*perCM))
>
> I have all the variables: aveV, aveV(g), aveLOC and perCM.
> So the equation is:
>
> $MI=171 - (5.2*(2.303*log($aveV)))-(0.23*$ecc)-
> (16.2*(2.303*log($aveLOC)))+(50*(sin(sqrt(2.46*$perCM))));
Is that log() function base e or base 2 or ...?
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Re: Math Functions in Perl
am 05.11.2007 16:07:53 von smallpond
On Nov 5, 8:00 am, coolchick wrote:
> Hi All,
>
> I am trying to compute few equations using perl.
>
> This is the equation that I am trying to solve:
>
> MI=171-5.2*ln(aveV)-0.23*aveV(g)-16.2*ln(aveLOC)
> +50sin(sqrt(2.46*perCM))
>
> I have all the variables: aveV, aveV(g), aveLOC and perCM.
> So the equation is:
>
> $MI=171 - (5.2*(2.303*log($aveV)))-(0.23*$ecc)-
> (16.2*(2.303*log($aveLOC)))+(50*(sin(sqrt(2.46*$perCM))));
>
> where:
> $aveV= 239.530014548124
> $ecc=9
> $aveLOC=47
> $perCM=0.297872340425532
>
> I am not getting the correct answer. I get MI=-2.56217048794884, which
> is not correct.
> I tried to break it down and the problem is that I don't think I have
> the right libraries for math.
> It is not computing sqrt, log, and sin correctly.
>
> I am using:
> use Math::Complex;
> use Math::Trig;
>
> Any help would be great!
> Thanks,
> naureen
Rearranging your equation to show intermediate terms:
$aveV = 239.530014548124;
$ecc = 9.0;
$aveLOC = 47.0;
$perCM = 0.297872340425532;
$i1= (5.2 * (2.303*log($aveV)));
$i2= (0.23 * $ecc);
$i3= (16.2 * (2.303*log($aveLOC)));
$i4= (50 * (sin(sqrt(2.46*$perCM))));
$MI = 171.0 - $i1 - $i2 - $i3 + $i4;
print "171.0 - ",$i1," - ",$i2," - ",$i3," + ",$i4," = ",$MI,"\n";
171.0 - 65.610465007406 - 2.07 - 143.64361681316 + 37.761911332617 =
-2.56217048794883
I don't think you have math errors. All of the terms are the
same order of magnitude. If the result is wrong, then it is
something in your formula. You do want log base e and angle
in radians, right?
--S
Re: Math Functions in Perl
am 05.11.2007 17:49:56 von coolchick
Hi, thanks for the help but the problem is that I am trying to solve
the Coleman-Oman's model for computing a code Maintainability Index.
The equation has 'ln' which I equated to 2.303*log(). So how do I
compute that? If I do the same thing using a scientific calculator, I
get a different answer.
For the angle, shouldn't that be in degrees.
Here is the equation:
http://www.stsc.hill.af.mil/consulting/show_ct_article.asp?u ri=2001/08/liso.html&uri2=sw_measurement
Re: Math Functions in Perl
am 05.11.2007 19:03:18 von Jim Gibson
In article <1194281396.204469.234530@t8g2000prg.googlegroups.com>,
coolchick wrote:
> Hi, thanks for the help but the problem is that I am trying to solve
> the Coleman-Oman's model for computing a code Maintainability Index.
> The equation has 'ln' which I equated to 2.303*log(). So how do I
> compute that? If I do the same thing using a scientific calculator, I
> get a different answer.
> For the angle, shouldn't that be in degrees.
perldoc -f log:
log EXPR
log Returns the natural logarithm (base e) of EXPR.
perldoc -f sin:
sin EXPR
sin Returns the sine of EXPR (expressed in radians).
>
> Here is the equation:
>
> http://www.stsc.hill.af.mil/consulting/show_ct_article.asp?u ri=2001/08/liso.ht
> ml&uri2=sw_measurement
>
% perl -e '
$aveV=239.53001;$ecc=9;$aveLOC=47;$perCM=0.297872;$mi=
171-(5.2*(log($aveV)))-(0.23*$ecc)-(16.2*log($aveLOC))+(50*( sin(sqrt(2.4
6*$perCM))));print "mi=$mi\n";'
mi=115.830374853214
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Re: Math Functions in Perl
am 05.11.2007 19:48:58 von coolchick
How did you get 'mi=115.830374853214 ' as your your answer?
How can I do this inside my perl code ?
Re: Math Functions in Perl
am 05.11.2007 21:09:45 von glex_no-spam
coolchick wrote:
> How did you get 'mi=115.830374853214 ' as your your answer?
>
> How can I do this inside my perl code ?
At this point, most people would simply copy and paste the
relevant parts of the code into a file and experiment with
it on their own.
Re: Math Functions in Perl
am 05.11.2007 21:25:05 von Michele Dondi
On Mon, 05 Nov 2007 05:00:35 -0800, coolchick
wrote:
>Hi All,
>
>I am trying to compute few equations using perl.
Perl is not oriented at solving equations. You can solve them
symbolically and throw in values to compute the value of some function
given the arguments to it.
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: Math Functions in Perl
am 05.11.2007 21:49:38 von Michele Dondi
On Mon, 05 Nov 2007 10:03:18 -0800, Jim Gibson
wrote:
>> For the angle, shouldn't that be in degrees.
>
>perldoc -f log:
>
> log EXPR
> log Returns the natural logarithm (base e) of EXPR.
For some reason, pocket calcultators have this habit of calling log_10
log and log_e ln. In my mathematical writings I just stick with log to
mean "their" ln. logarithms in any other base (unless explicitly
shown) are of little mathematical relevance, IMHO.
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: Math Functions in Perl
am 05.11.2007 21:51:07 von Jim Gibson
In article <1194288538.022091.25250@z24g2000prh.googlegroups.com>,
coolchick wrote:
> How did you get 'mi=115.830374853214 ' as your your answer?
By executing the code shown, which was cut-and-pasted from a shell
session on my computer.
>
> How can I do this inside my perl code ?
By copying the code shown into a file and turning it into a complete
Perl script (I left out 'use strict;', for example, which you should
add).
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Re: Math Functions in Perl
am 05.11.2007 23:42:03 von Martijn Lievaart
On Mon, 05 Nov 2007 21:49:38 +0100, Michele Dondi wrote:
> For some reason, pocket calcultators have this habit of calling log_10
> log and log_e ln. In my mathematical writings I just stick with log to
> mean "their" ln. logarithms in any other base (unless explicitly shown)
> are of little mathematical relevance, IMHO.
I use log2 on a nearly daily basis. But then, I'm in computers while I
could have learned a trade.
M4
Re: Math Functions in Perl
am 06.11.2007 00:33:19 von Michele Dondi
On Mon, 5 Nov 2007 23:42:03 +0100, Martijn Lievaart
wrote:
>> For some reason, pocket calcultators have this habit of calling log_10
>> log and log_e ln. In my mathematical writings I just stick with log to
>> mean "their" ln. logarithms in any other base (unless explicitly shown)
>> are of little mathematical relevance, IMHO.
>
>I use log2 on a nearly daily basis. But then, I'm in computers while I
>could have learned a trade.
Hehe, in fact I was about to say that, probably along the lines of "a
second, but distant competitor being log_2". In fact information
theory is a respectable mathematical one also in its own respect i.e.
abstractly and regardless of any physical computer...
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,