CGI/Perl Guide | Learning Center | Forums | Advertise | Login Site Search: in Perl Guide PerlGuru Forums Learning Ctr

Home: Perl Programming Help: Intermediate:
Subtraction Error using Perl

ch_tang
New User

Sep 11, 2009, 6:05 PM

Post #1 of 3 (890 views)
 Subtraction Error using Perl Can't Post
Hi All,

I observed the following abnormality while running my PERL program calculating student t test test statistics value. Hope you can help. Thanks in advance.

\$t0 = (\$mean1 - \$mean2)/(sqrt(\$varp*(1/\$n1+1/\$n2)));

\$mean_diff = \$mean1 - \$mean2;

I printed the value of the variable in the equation:

print "(\$mean1 - \$mean2) \$mean_diff /sqrt(\$varp*(1/\$n1+1/\$n2)) = \$t0\n";

The results was as below:

\$mean1-\$mean2 \$mean_diff /sqrt(\$varp*(1/\$n1+1/\$n2)) = \$t0

(0.61 - 0.61) -1.11022302462516e-16 /sqrt(8.40561424617007e-32*(1/39+1/35)) = -1.64465938636703

Which was puzzling. Shouldnt 0.61 0.61 result to 0? This screwed up my test statistics and wonder if any of you can help work around this issue? Do I need to load any additional perl mathematics module to calculate this properly?

I expected \$t0 to be 0 since the numerator should be a 0.

toolic
User

Sep 11, 2009, 6:26 PM

Post #2 of 3 (887 views)
 Re: [ch_tang] Subtraction Error using Perl [In reply to] Can't Post
You do not show how you calculated \$mean1 and
\$mean2. They are probably the result of floating point
arithmetic operations. See the discussion at:

http://www.perlmonks.org/?node_id=293402

One way to solve this issue is to use sprintf:

 Code
`\$mean_diff = sprintf '%f', \$mean1 - \$mean2;`

ch_tang
New User

Sep 12, 2009, 6:02 PM

Post #3 of 3 (868 views)
 Re: [toolic] Subtraction Error using Perl [In reply to] Can't Post
thanks toolic.

i think i got the idea now from the link you posted.

 Announcements     PerlGuru Announcements Perl Programming Help     Frequently Asked Questions     Beginner     Intermediate     Advanced     Regular Expressions     mod_perl     DBI     Win32 Programming Help Fun With Perl     Perl Quizzes - Learn Perl the Fun Way     Perl Golf     Perl Poetry Need a Custom or Prewritten Perl Program?     I need a program that...     I Need a Programmer for Freelance Work     Throw Down The Gauntlet General Discussions     General Questions     Feedback     Tutorial/Article Suggestions for The Learning Cent     Internet Security Other Programming Languages     Javascript     PHP

 Search this forum this category all forums for All words Any words Whole Phrase (options) Powered by Gossamer Forum v.1.2.0