List Info

Thread: precision and perl




precision and perl
user name
2006-03-30 21:22:47
Wayne Simmons wrote:
> From: Wayne Simmons
> > 
> > my $x = .0006;
> > my $y = $x * 10000;
> > print $y , "\n";
> > print sprintf("%04d\n",$y);
> > print int($y), "\n";
> > __END__
> 
> From: Christopher Hahn [mailto:christopher.hahnperegrine.com]
> > 
> > Int returns the integer portion --> it
truncates!
> 
> 
> Great, thanks for filling me in! So 6 truncates to 5 in
perl?!? :-0

No, 5.9999999999999991118215803 truncates to 5 in most any
language.

The problem is that floating point calculations are always
approximate in computers.

This may make more sense:

    my $x = .0006;
    my $y = $x * 10000;
    print "Number looks like this: $y\n";
    printf "But it is really this: %0.25f\n",
$y;
    printf "And it truncates to this: %d\n",$y;

Output:

    Number looks like this: 6
    But it is really this: 5.9999999999999991118215803
    And it truncates to this: 5

-- 
Bowie
_______________________________________________
ActivePerl mailing list
ActivePerllistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
Testing for module
user name
2006-03-31 00:51:33
I want to test for the existence of a module, and do
alternative
calculations based on the result. So I have written the
following, based
on http://www.unix.org.ua/orelly/perl/cookbook/ch12_03.htm

BEGIN {
	$flag_hires=0;
	unless (eval "use Time::HiRes qw(gettimeofday
tv_interval)" ) {
		$flag_hires=1;
		$timefirst=$timelast = [gettimeofday];print "Hello2
";
	}else{$timefirst=$timelast=time();print "Hello3
";}
}

This code ALWAYS returns flag_hires=1
even if I introduce an error, e.g., "use
Time::HHiRes...."
Why? Also, it appears I can't use [gettimeofday] in the
BEGIN block.
Thanks for your help as I delve into new areas.

 --Nelson R. Pardee, Support Analyst, Information Technology
& Services--
 --Syracuse University, 211 Machinery Hall, Syracuse, NY
13244-1260    --
 --(315) 443-1079         NRPARDEESYR.EDU                    
        --
_______________________________________________
ActivePerl mailing list
ActivePerllistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
Testing for module
user name
2006-03-31 03:17:53
Nelson R. Pardee wrote:

> I want to test for the existence of a module, and do
alternative
> calculations based on the result. So I have written the
following, based
> on http://www.unix.org.ua/orelly/perl/cookbook/ch12_03.htm
> 
> BEGIN {
> 	$flag_hires=0;
> 	unless (eval "use Time::HiRes qw(gettimeofday
tv_interval)" ) {
> 		$flag_hires=1;
> 		$timefirst=$timelast = [gettimeofday];print
"Hello2 ";
> 	}else{$timefirst=$timelast=time();print "Hello3
";}
> }
> 
> This code ALWAYS returns flag_hires=1
> even if I introduce an error, e.g., "use
Time::HHiRes...."
> Why? Also, it appears I can't use [gettimeofday] in
the BEGIN block.
> Thanks for your help as I delve into new areas.

Try this:

use strict;
use warnings;

my $use_hires;
my $start_time;

BEGIN {
	eval "use Time::HiRes qw(gettimeofday
tv_interval)";
	if ($) {
		$use_hires = 0;
		$start_time = time;
		print "No HiRes available\n";
	} else {
		$use_hires = 1;
		$start_time = [&gettimeofday ()];
		print "HiRes available\n";
	}
}

sleep 2;	# something to time

if ($use_hires) {
	printf "hires: %.3f seconds\n", tv_interval
($start_time);
} else {
	printf "basic: %u seconds\n", time -
$start_time;
}

__END__
_______________________________________________
ActivePerl mailing list
ActivePerllistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )