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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



ch_tang
New User

Sep 11, 2009, 6:05 PM

Post #1 of 3 (720 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 (717 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 (698 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.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives