Martin said:
Hi,
I allocate some memory of size_t but want to express:
printf("Total amount of allocated memory is: %lu bytes.\n", (unsigned
long) total_mem);
in kilo-bytes. So is method 1 or 2, the correct one? I've seen both
explanations using google:
1)
printf("Total amount of allocated memory is: %lu kb.\n", (unsigned long)
total_mem/1024);
2)
printf("Total amount of allocated memory is: %lu kb.\n", (unsigned long)
total_mem/1000);
Method #2 is correct[*]. The International System of Units (SI)
defines the prefix "kilo" to mean multiplication by one thousand,
hence, the conversion from bytes to kilobytes (or volts to kilovolts,
or manjaros to kilomanjaros) requires dividing by one thousand.
See <
http://physics.nist.gov/cuu/Units/prefixes.html>.
[*] Since your code performs an integer division, any fractional
part will be truncated. For example, 1024 bytes will be reported as
one kilobyte, rather than 1.024 kilobyte. Whether this truncation is
"correct" or not depends on the use to which the result is put.
Method #1 is traditional among computer geeks, who noticed long
ago that 1024 is not terribly different from 1000. Since the number
1024 cropped up often enough in computerdom to make a convenient
abbreviation desirable, the geeks co-opted the prefix "kilo" to mean
multiplication by 1024. This was the Original Sin, from which has
flowed much grief.
... because computers grew larger, and pretty soon the geeks
needed a convenient way to express multiplication by 1048576, two
to the twentieth. With the evil precedent of "kilo" already in
place, it was easy for them to sin again and abuse "mega" in a
similar fashion. "Giga" followed, and then "tera," and "peta"
has begun to pop up -- in the last year, I've even seen "zetta"
abused, skipping right over "exa." SI defines "zetta" to mean
multiplication by 1,000,000,000,000,000,000,000 (ten to the 21st),
while in geekery it multiplies by 1,180,591,620,717,411,303,424
(two to the seventieth).
Notice how the Original Sin has become worse with the passage
of time and the growth of the numbers. The misuse of "kilo" causes
an error of 2.4%, about one part in forty-two (the significance of
the denominator is left as an exercise for the hitchhiker). By
co-opting "mega" the geeks magnified the error to 4.9%, one part
in twenty-one. "Giga" is off by 7.4% (one part in fourteen), and
by the time the geeks have gored "zetta" they're off by a whopping
18% (one part in six). This proves that computer people have no
interest in accuracy. (It has long been said that mathematicians
are people who can't add, while computer programmers are people
who can't even count.)
Does the inaccuracy cause trouble? Yes! At a PPOE I was doing
last-minute bug-fixing before product release, and one of the Q/A
people reported a bug in the installation phase. The documentation
said installation required X megabytes of available disk space, so
the Q/A engineer decided to make sure it would actually work. He
filled a disk with garbage files until only X megabytes of free
space remained, then tried to install. The installation failed for
lack of sufficient space, and the Q/A engineer filed a bug. I'm sure
you can see where this is going: the documentation (and the install
program) wanted X * 1048576 bytes, while the disk on the test machine
had only X * 1000000 bytes available. Yes, Virginia, a 4.9% error
can make a difference.
The oddest and perhaps most bletcherous abuse in this sad history
of wilful corruption is the "1.44 megabyte" floppy disk. Said disk
does *not* hold 1.44 megabytes using either the true SI "mega" or
the geekish two-to-the-twentieth "mega." What is actually holds is
1.44 "kilo" (SI) "kilobytes" (geekish), a truly bastardized unit
unloved by both parents.
The International Electrotechnical Commission has made a noble
attempt to extend an olive branch to the geeks by standardizing
a set of prefixes for binary multipliers: kibi, mebi, gibi, ...
(See <
http://physics.nist.gov/cuu/Units/binary.html>.) The geeks,
however, are an ungracious and ungrateful lot and have spurned this
peace offering; they continue to misuse "kilo" et al. for their own
dastardly purposes. They are in agreement with Humpty Dumpty, who
said "When I use a word it means just what I choose it to mean."
Such an attitude evidences a certain independence, perhaps, but also
a lack of interest in communicating.
When all those geeks find themselves condemned to a few megayears
in Purgatory for their prefix abuse, let's be sure to ask them how
they'd like "mega" defined, shall we?