I am very confused about how decimal numbers work in perl. I’m having trouble multiplying an int 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:
In Perl functions, you always need to declare your variables with the
mykeyword (which allocates a lexically scoped variable) otherwise you will run into problems. Usinguse 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 thetimeEstimatefunction is in error. It specifies that thetimeEstimatefunction 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
sprintfis not needed. The line can be rewritten as: