For American numbers

  • Thread starter Scott David Daniels
  • Start date
S

Scott David Daniels

Kind of fun exercise (no good for British English).

def units(value, units='bytes'):
magnitude = abs(value)
if magnitude >= 1000:
for prefix in ['kilo mega giga tera peta '
'exa zetta yotta').split():
magnitude /= 1000.
if magnitude < 1000.:
break
elif magnitude < 1:
for prefix in ('milli micro nano pico femto '
'atto zepto yocto'.split():
magnitude *= 1000.
if magnitude >= 1.0:
break
if magnitude < .001:
return 'zero %s' % units
else:
prefix = ''
if value < 0:
return '-%.3f %s%s' % (magnitude, prefix, units)
return '%.3f %s%s' % (magnitude, prefix, units)

--Scott David Daniels
(e-mail address removed)
 
P

python

Scott said:
Kind of fun exercise (no good for British English).

def units(value, units='bytes'):
magnitude = abs(value)
if magnitude >= 1000:
for prefix in ['kilo mega giga tera peta '
'exa zetta yotta').split():
magnitude /= 1000.
if magnitude < 1000.:
break
elif magnitude < 1:
for prefix in ('milli micro nano pico femto '
'atto zepto yocto'.split():
magnitude *= 1000.
if magnitude >= 1.0:
break
if magnitude < .001:
return 'zero %s' % units
else:
prefix = ''
if value < 0:
return '-%.3f %s%s' % (magnitude, prefix, units)
return '%.3f %s%s' % (magnitude, prefix, units)

--Scott David Daniels
(e-mail address removed)
 
P

Peter Hansen

Scott said:
Kind of fun exercise (no good for British English).

def units(value, units='bytes'):
magnitude = abs(value)
if magnitude >= 1000:
for prefix in ['kilo mega giga tera peta '
'exa zetta yotta').split():
magnitude /= 1000.
if magnitude < 1000.:
break

Only for hard drive manufacturers, perhaps.

For the rest of the computer world, unless I've missed
a changing of the guard or something, "kilo" is 1024
and "mega" is 1024*1024 and so forth...

-Peter
 
N

Nick Coghlan

Scott said:
Kind of fun exercise (no good for British English).

def units(value, units='bytes'):
magnitude = abs(value)
if magnitude >= 1000:
for prefix in :
magnitude /= 1000.
if magnitude < 1000.:
break
elif magnitude < 1:
for prefix in :
magnitude *= 1000.
if magnitude >= 1.0:
break
if magnitude < .001:
return 'zero %s' % units
else:
prefix = ''
if value < 0:
return '-%.3f %s%s' % (magnitude, prefix, units)
return '%.3f %s%s' % (magnitude, prefix, units)

--Scott David Daniels
(e-mail address removed)

Because I can't resist generalising this. . .

def units(value, units='bytes', base=1000.0,
super_prefixes = ('kilo mega giga tera peta '

'exa zetta yotta').split(),
sub_prefixes = ('milli micro nano pico femto '
'atto zepto yocto').split()):
magnitude = abs(value)
if magnitude >= base:
for prefix in super_prefixes:
magnitude /= base
if magnitude < base:
break
elif magnitude < 1.0:
for prefix in sub_prefixes:
magnitude *= base
if magnitude >= 1.0:
break
if not sub_prefixes or magnitude < .001:
return 'zero %s' % units
else:
prefix = ''
if value < 0:
return '-%.3f %s%s' % (magnitude, prefix, units)
return '%.3f %s%s' % (magnitude, prefix, units)

def bytecount(value):
# Should check if these prefixes are right. . .
return units(value, 'bytes', 1024.0,
'kibi mebi gibi tebi pebi ebi zebi yobi'.split(), [])

Py> for i in range(1, 28, 3):
.... print units(10**i)
.... print bytecount(10**i)
....
10.000 bytes
10.000 bytes
10.000 kilobytes
9.766 kibibytes
10.000 megabytes
9.537 mebibytes
10.000 gigabytes
9.313 gibibytes
10.000 terabytes
9.095 tebibytes
10.000 petabytes
8.882 pebibytes
10.000 exabytes
8.674 ebibytes
10.000 zettabytes
8.470 zebibytes
10.000 yottabytes
8.272 yobibytes

Cheers,
Nick.
 
N

Nick Coghlan

Peter said:
Only for hard drive manufacturers, perhaps.

For the rest of the computer world, unless I've missed
a changing of the guard or something, "kilo" is 1024
and "mega" is 1024*1024 and so forth...

Given that there are perfectly good ISO prefixes for the multiples of 2**10, I
don't see any reason to continue misusing the 10**3 prefixes for the purpose.

It may be an uphill slog, but the fact that the CS usage is ambiguous at best,
and just plain wrong at worst, gives me hope :)

The hard drive guys are just leading the charge because it makes for better
marketing copy. . .

Cheers,
Nick.
 
R

Roel Schroeven

Peter said:
Scott said:
Kind of fun exercise (no good for British English).

def units(value, units='bytes'):
magnitude = abs(value)
if magnitude >= 1000:
for prefix in ['kilo mega giga tera peta '
'exa zetta yotta').split():
magnitude /= 1000.
if magnitude < 1000.:
break


Only for hard drive manufacturers, perhaps.

And physicists and chemists and engineers and all other kinds of
scientists all over the world. Plus those of us in the computer world
who agree that 'kilo == 1024' is an abomination that should never have
existed and which we should get rid of as fast as possible.

As fast as possible is not very fast, unfortunately.
 
P

Pierre Hanser

Peter said:
Scott said:
Kind of fun exercise (no good for British English).

def units(value, units='bytes'):
magnitude = abs(value)
if magnitude >= 1000:
for prefix in ['kilo mega giga tera peta '
'exa zetta yotta').split():
magnitude /= 1000.
if magnitude < 1000.:
break


Only for hard drive manufacturers, perhaps.

For the rest of the computer world, unless I've missed
a changing of the guard or something, "kilo" is 1024
and "mega" is 1024*1024 and so forth...

-Peter
even for cpu frequency?
 
N

Nick Craig-Wood

Peter Hansen said:
Only for hard drive manufacturers, perhaps.

For the rest of the computer world, unless I've missed
a changing of the guard or something, "kilo" is 1024
and "mega" is 1024*1024 and so forth...

Yes. Unless you work in the telcoms industry, where, for example if
you order a 2 Mbit/s line you'll get

2 * 1024 * 1000 bits / s

;-)
 
D

Dan Bishop

Nick said:
Yes. Unless you work in the telcoms industry, where, for example if
you order a 2 Mbit/s line you'll get

2 * 1024 * 1000 bits / s

They must have gotten the idea from floppy disks, which also use a
1024000-byte "megabyte".
 
P

Peter Hansen

Roel said:
Peter said:
Scott said:
Kind of fun exercise (no good for British English).

def units(value, units='bytes'):
magnitude = abs(value)
if magnitude >= 1000:
for prefix in ['kilo mega giga tera peta '
'exa zetta yotta').split():
magnitude /= 1000.
if magnitude < 1000.:
break



Only for hard drive manufacturers, perhaps.


And physicists and chemists and engineers and all other kinds of
scientists all over the world. Plus those of us in the computer world
who agree that 'kilo == 1024' is an abomination that should never have
existed and which we should get rid of as fast as possible.

Physicists and chemists (and most engineers) don't go around
talking about "kilobytes" all that often, and when they do
it's generally unimportant whether they mean 1000 or 1024.

Given the clear "units='bytes'" default above, and my restricting
my comments to "the rest of the computer world", it should be
clear I was talking about a very limited subset of the planet.

A subset, however, which has an extremely strong attachment to
1024 instead of 1000 (for very good reasons), and which is
less likely to abandon backwards compatibility and widely accept
1000 than the US is likely to adopt metric widely in the near
future...

-Peter
 
P

Peter Hansen

Pierre said:
Peter said:
Scott said:
Kind of fun exercise (no good for British English).

def units(value, units='bytes'):
magnitude = abs(value)
if magnitude >= 1000:
for prefix in ['kilo mega giga tera peta '
'exa zetta yotta').split():
magnitude /= 1000.
if magnitude < 1000.:
break



Only for hard drive manufacturers, perhaps.

For the rest of the computer world, unless I've missed
a changing of the guard or something, "kilo" is 1024
and "mega" is 1024*1024 and so forth...

even for cpu frequency?

I don't think so. But who cares? CPU frequency, apart
from being fairly meaningless anyway, doesn't cover
enough ground for anyone to need a routine like the
above to deal with it.

Anyway, I was focusing on the "units='bytes'" part above
which suggested a byte-oriented focus for the routine,
and CPU frequencies aren't measured in bytes...

-Peter
 
R

Roel Schroeven

Peter said:
Roel said:
Peter said:
Scott David Daniels wrote:

Kind of fun exercise (no good for British English).

def units(value, units='bytes'):
magnitude = abs(value)
if magnitude >= 1000:
for prefix in ['kilo mega giga tera peta '
'exa zetta yotta').split():
magnitude /= 1000.
if magnitude < 1000.:
break




Only for hard drive manufacturers, perhaps.



And physicists and chemists and engineers and all other kinds of
scientists all over the world. Plus those of us in the computer world
who agree that 'kilo == 1024' is an abomination that should never have
existed and which we should get rid of as fast as possible.


Physicists and chemists (and most engineers) don't go around
talking about "kilobytes" all that often, and when they do
it's generally unimportant whether they mean 1000 or 1024.

Scientists and engineers use kilo as a prefix for all kinds of units,
and it means 1000 regardless of what unit it is used with. Except when
used with bytes and some other units, and I feel that that is a
historical misfeature.
Given the clear "units='bytes'" default above, and my restricting
my comments to "the rest of the computer world", it should be
clear I was talking about a very limited subset of the planet.

"units='bytes'" means that 'bytes' is the default, but also that the
function can be called with any other unit.
A subset, however, which has an extremely strong attachment to
1024 instead of 1000 (for very good reasons),

Well, in computer science 1024 is often more practical than 1000, I'm
not going to argue that. My point is that 1024 is not 1000 (though it is
quite close, unfortunately), so another prefix should have been chosen
to represent it. I don't mind interchanging 1000 and 1024 in informal
contexts, but I think a clear distinction is needed in other cases:

- the difference between 1000 and 1024 is only 2.4%, but the difference
between 1 GB and 1GiB is 7.4. That can IMO no longer be looked at as
insignificant.
- computer scientists are used to having everyting just right. We're
used to compilers that cannot deal with the slightest typo, TCP doesn't
work right if we're ACKing the wrong sequence numbers. In the view of
that, it feels ackward that we're so sloppy with unit prefixes.

History has tought is that standardization is a good thing, and I think
we should start using the existing standard (SI) as it is inteded to be
used: k as a prefix equals 1000, regardless of the context. I agree that
'kibi', 'mebi' and 'gibi' sound silly, but I think it's better to use
silly sounding units than units of which you can never be really sure
what they mean.
less likely to abandon backwards compatibility and widely accept
1000 than the US is likely to adopt metric widely in the near
future...

I'm afraid you're right, but I think we should at least try.

In fact, I don't really understand that: computer science is a modern
science, computers and other digital devices are modern stuff, but still
the computer world is super tradionalist about this. The worst part is
that it's a tradition we invented ourselves which is in contradiction
with an older, commonly accepted tradition.
 
A

Alan Kennedy

[Scott David Daniels]
Kind of fun exercise (no good for British English).

def units(value, units='bytes'):
magnitude = abs(value)
if magnitude >= 1000:
for prefix in ['kilo mega giga tera peta '
'exa zetta yotta').split():
magnitude /= 1000.
if magnitude < 1000.:
break

[Peter Hansen]
Only for hard drive manufacturers, perhaps.

For the rest of the computer world, unless I've missed
a changing of the guard or something, "kilo" is 1024
and "mega" is 1024*1024 and so forth...

Maybe you missed these?

http://en.wikipedia.org/wiki/Kibibyte
http://en.wikipedia.org/wiki/Mebibyte
http://en.wikipedia.org/wiki/Gibibyte

kilo-mega-giga-etc-should-be-powers-of-10-ly y'rs,
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Peter said:
For the rest of the computer world, unless I've missed
a changing of the guard or something, "kilo" is 1024
and "mega" is 1024*1024 and so forth...

In case this isn't clear yet: you have missed a changing
of the guard or something. "kibi" is 1024, "mebi" is
1024*1024 and so forth. "kilo" is 1000.

Regards,
Martin
 
R

Ruud de Jong

Dan Bishop schreef:
They must have gotten the idea from floppy disks, which also use a
1024000-byte "megabyte".

Not really. It is actually related to the bandwidth of individual
telephony speech connections. These channels have a bandwith
of 64 kb/sec, where the k-prefix is used in the proper (SI) way.
So this bandwidth really is exactly 64,000 bits/sec
(8,000 samples/sec, each sample having 8 bits).
The M in a 2Mb/s connection is neither an SI prefix,
noris it identical to Mi. It's just sloppy language.
A 2 Mb/s connection is just a bundling of 32 such 64 kb/s channels,
resulting in a bandwidth of 32*64,000 = 2,048,000 bits/sec.

Cheers,

Ruud
 
E

Erik Max Francis

Dan said:
They must have gotten the idea from floppy disks, which also use a
1024000-byte "megabyte".

It's pretty common industry-wide. Memory is measured in binary prefixes
(x 1024), but disk space and bandwidth are measured in decimal prefixes
(x 1000).
 
P

Peter Hansen

Alan said:
[Peter Hansen]
For the rest of the computer world, unless I've missed
a changing of the guard or something, "kilo" is 1024
and "mega" is 1024*1024 and so forth...

Maybe you missed these?

http://en.wikipedia.org/wiki/Kibibyte
http://en.wikipedia.org/wiki/Mebibyte
http://en.wikipedia.org/wiki/Gibibyte

Definitely missed them, in the sense of "didn't see them
yet". I must say I'm somewhat astounded that anyone
bothered to do this.

Don't miss them at all, in the sense of "have no plans
to use them and am not stressed in the least over the fact
that they are lacking in my life."
kilo-mega-giga-etc-should-be-powers-of-10-ly y'rs,

Maybe, but they aren't always... that's just the way
it is.

I take a descriptive view rather than a prescriptive one.
Google for the usage of the above and you'll find the
following ratios for the numbers of pages that use
the two forms:

kibibyte vs. kilobyte: 1:60 (impressively high, I admit)
mebibyte vs. megabyte: 1:234
gibibyte vs. gigabyte: 1:2082

I strongly suspect that the vast majority of the former
set of pages are of the form "use this instead of kilo!",
but perhaps I really have been asleep while much of the
computing world converted.

I'll be one of the last holdouts, too... it's really not
so hard to work in powers of two if you try...

-Peter
 
P

Peter Hansen

Martin said:
In case this isn't clear yet: you have missed a changing
of the guard or something. "kibi" is 1024, "mebi" is
1024*1024 and so forth. "kilo" is 1000.

Yeah, saw the link. The IEC doesn't speak for me, I'm afraid.
And as the wikipedia notes, "As of 2005 this naming convention
has not gained widespread use."

I suspect I and many others will go to our graves not being
comfortable mebbling and gibbling over our bytes, though
we'll probably spend a lot of time kibbling over the issue...

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top