11. Februar 2009

by

In: Tech

1 comment

When you have to deal with kilobyte or kibibyte values in today’s real-world computers, you often want extremely similar values to evaluate to „equal“. An easy solution is to round them to full Mebibytes. This can be easily done in every programming language which supports integer division and the modulo operator.

For example in Perl:

sub _rnd_kbval_to_mib_borders {
      my $input  = shift;
      my $base   = $input DIV 1024;
      my $modulo = $input % 1024;
      return ( $modulo < 512 ) ? $base * 1024 : ( $base + 1 ) * 1024;
   }

This approach is trivial but can safe you a lot of headache.

Tags: , ,

1 comment

Trackback e pingback

No trackback or pingback available for this article