darxus: (Default)
darxus ([personal profile] darxus) wrote2009-09-09 04:38 pm
Entry tags:

[geek] How apache 2's autoindex converts bytes to kilobytes and megabytes.

This was tedious to figure out, so hopefully I can spare someone else. In perl:

if ($sizeunit eq 'K') {
  $realsize = round($realsize/1024);
} elsif ($sizeunit eq 'M') {
  $realsize = round($realsize/1024/1024);
}

sub round {
  my($number) = shift;
  my $left = (split('\.',$number))[0];
  if (length($left) <= 1) {
    return int( $number * 10 + .5 )/10;
  } else {
    return int( $number + .5 );
  }
}


I'm convinced there is no simpler way (I read the whole sprintf man page at least twice).

I'm writing a monitoring script that checks the sizes of files on a web server. So I figured I should do a little random validation that apache is reporting the same file size that I'm getting from length($content). So I needed to convert length($content) to the same form I get from apache.

[identity profile] ectropy.livejournal.com 2009-09-09 09:02 pm (UTC)(link)
There's no way to include such basic math functions as Round() or use the modulo operator?

[identity profile] darxus.livejournal.com 2009-09-09 09:06 pm (UTC)(link)
Perl has no round(). The faq entry tells you how to write it. I'm not great with modulo, so it's not obvious to me how it would be useful.
cos: (Default)

how to do it with sprintf

[personal profile] cos 2009-09-09 09:40 pm (UTC)(link)
if ($sizeunit eq 'K') {
  $realsize = sprintf("%d",$realsize/1024 +.5);
} elsif ($sizeunit eq 'M') {
  $realsize = sprintf("%d",$realsize/1024/1024 +.5);
}
cos: (Default)

real rounding with sprintf

[personal profile] cos 2009-09-09 09:43 pm (UTC)(link)
Oops, I forgot the floating point conversation always does real rounding, so another way is:

$rounded = sprintf("%.0f", $number/1024);

Re: real rounding with sprintf

[identity profile] darxus.livejournal.com 2009-09-09 10:09 pm (UTC)(link)
You skipped the variable precision.
cos: (Default)

Re: real rounding with sprintf

[personal profile] cos 2009-09-09 10:33 pm (UTC)(link)
Ahh, I see what you're saying. Something like this?

$size /= 1024;
$pr = $size<9.5 ? 1 : 0;
$round = sprintf("%.${pr}", $size);

Re: real rounding with sprintf

[identity profile] darxus.livejournal.com 2009-09-09 11:42 pm (UTC)(link)
Looks likely to work.

Re: real rounding with sprintf

[identity profile] feng-huang.livejournal.com 2009-09-10 01:05 am (UTC)(link)
Hmm... I can't remember where the discussion was (aside from "somebody else's journal), but I got chided semi-recently for suggesting sprintf as a method of rounding. The other person said it was slow and error-prone. I can't recall if I asked for a reference for this claim. I'll try to remember where this discussion took place and see if there was anything to substantiate the other person's claim.
cos: (Default)

Re: real rounding with sprintf

[personal profile] cos 2009-09-10 03:26 am (UTC)(link)
You could do $rounded = int($num+.5) but he mentioned sprintf specifically. For variable precision sprintf is easier, and I highly doubt rounding performance is a big issue in this kind of application.