开发者

Simple: How do doubles work in Perl?

I am very confused about how decimal numbers work in perl. I'm having trouble multiplying an in开发者_开发知识库t with a double. Here's what I have:

sub timeEstimate(){
$number = shift;
print "Number: $number\n";
$stuff = sprintf("%d", $number * $number * $number) * .2045;
print "stuff: $stuff\n";
$totalDownloads = $stuff + ($number * $number) + $number;
print "totalDownloads: $totalDownloads\n";
$secondPerFile = .4464;
print "secondPerFile: $secondPerFile\n";
$totalSeconds = ($totalDownloads * $secondPerFile);
print "totalSeconds: $totalSeconds\n";
$totalHours = ($totalSeconds / 3600);
print "totalHours: $totalHours\n";
return $totalHours;
}

But no matter what I try, even sprintf, I still can't get $stuff to be anything but 0. Could someone explain how the system works?

UPDATE-Solved: Due to stupid self-inflicted mistake. I had

use integer; 

in the code. headdesk


Once variables have been declared and the prototype removed from the function, your code seems to work:

use warnings;
use strict;

sub timeEstimate {
    my $number = shift;
    print "Number: $number\n";

    my $stuff = sprintf("%d", $number * $number * $number) * .2045;
    print "stuff: $stuff\n";

    my $totalDownloads = $stuff + ($number * $number) + $number;
    print "totalDownloads: $totalDownloads\n";

    my $secondPerFile = .4464;
    print "secondPerFile: $secondPerFile\n";

    my $totalSeconds = ($totalDownloads * $secondPerFile);
    print "totalSeconds: $totalSeconds\n";

    my $totalHours = ($totalSeconds / 3600);
    print "totalHours: $totalHours\n";
    return $totalHours;
}

timeEstimate 10;


Number: 10
stuff: 204.5
totalDownloads: 314.5
secondPerFile: 0.4464
totalSeconds: 140.3928
totalHours: 0.038998

In Perl functions, you always need to declare your variables with the my keyword (which allocates a lexically scoped variable) otherwise you will run into problems. Using use warnings; use strict; at the top of every program will keep you from forgetting, and will also provide many useful diagnostic messages.

The () prototype you have on the timeEstimate function is in error. It specifies that the timeEstimate function does not accept any arguments. Do not use Perl's function prototypes until you know exactly why you need to be using them.

Lastly, your use of sprintf is not needed. The line can be rewritten as:

my $stuff = 0.2045 * ($number ** 3);


Your problem is with the subroutine prototype -- you have an empty argument list but you're calling it with one argument.

Make it timeEstimate($) and you'll be fine. Then call "timeEstimate( 10 )" or whatever.

However I don't see how you even could run it without getting an error message.


perl -e '$var = "0.123"; print sprintf("%02.5f\n", $var);'
0.12300

sprintf works fine with decimal numbers.

Edit:

Looks like you have also not used "use strict", which will help with any variable name typos. And i'd drop the () from the function declaration.


The code in your question has a semantic error. You should start it with

sub timeEstimate {  # no parens!

Are you forgetting to pass an argument to timeEstimate? Calling it as

timeEstimate 3;

gives the following output:

Number: 3
stuff: 5.5215
totalDownloads: 17.5215
secondPerFile: 0.4464
totalSeconds: 7.8215976
totalHours: 0.002172666
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜