For American numbers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott David Daniels

    For American numbers

    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
    Scott.Daniels@A cm.Org

  • python

    #2
    Re: For American numbers


    Scott David Daniels wrote:[color=blue]
    > 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
    > Scott.Daniels@A cm.Org[/color]

    Comment

    • Peter Hansen

      #3
      Re: For American numbers

      Scott David Daniels wrote:[color=blue]
      > 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[/color]

      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

      Comment

      • Nick Coghlan

        #4
        Re: For American numbers

        Scott David Daniels wrote:[color=blue]
        > 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
        > Scott.Daniels@A cm.Org
        >[/color]

        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.

        --
        Nick Coghlan | ncoghlan@email. com | Brisbane, Australia
        ---------------------------------------------------------------

        Comment

        • Nick Coghlan

          #5
          Re: For American numbers

          Peter Hansen wrote:[color=blue]
          > 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...[/color]

          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.

          --
          Nick Coghlan | ncoghlan@email. com | Brisbane, Australia
          ---------------------------------------------------------------

          Comment

          • Roel Schroeven

            #6
            Re: For American numbers

            Peter Hansen wrote:[color=blue]
            > Scott David Daniels wrote:
            >[color=green]
            >> 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[/color]
            >
            >
            > Only for hard drive manufacturers, perhaps.[/color]

            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.

            --
            "Codito ergo sum"
            Roel Schroeven

            Comment

            • Pierre Hanser

              #7
              Re: For American numbers

              Peter Hansen wrote:[color=blue]
              > Scott David Daniels wrote:
              >[color=green]
              >> 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[/color]
              >
              >
              > 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[/color]
              even for cpu frequency?

              Comment

              • Nick Craig-Wood

                #8
                Re: For American numbers

                Peter Hansen <peter@engcorp. com> wrote:[color=blue]
                > 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...[/color]

                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

                ;-)
                --
                Nick Craig-Wood <nick@craig-wood.com> -- http://www.craig-wood.com/nick

                Comment

                • Dan Bishop

                  #9
                  Re: For American numbers

                  Nick Craig-Wood wrote:[color=blue]
                  > Peter Hansen <peter@engcorp. com> wrote:[color=green]
                  > > 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...[/color]
                  >
                  > 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[/color]

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

                  Comment

                  • Peter Hansen

                    #10
                    Re: For American numbers

                    Roel Schroeven wrote:[color=blue]
                    > Peter Hansen wrote:
                    >[color=green]
                    >> Scott David Daniels wrote:
                    >>[color=darkred]
                    >>> 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[/color]
                    >>
                    >>
                    >>
                    >> Only for hard drive manufacturers, perhaps.[/color]
                    >
                    >
                    > 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.[/color]

                    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='byt es'" 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

                    Comment

                    • Peter Hansen

                      #11
                      Re: For American numbers

                      Pierre Hanser wrote:[color=blue]
                      > Peter Hansen wrote:
                      >[color=green]
                      >> Scott David Daniels wrote:
                      >>[color=darkred]
                      >>> 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[/color]
                      >>
                      >>
                      >>
                      >> 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...[/color]
                      >
                      > even for cpu frequency?[/color]

                      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='byt es'" part above
                      which suggested a byte-oriented focus for the routine,
                      and CPU frequencies aren't measured in bytes...

                      -Peter

                      Comment

                      • Alan Kennedy

                        #12
                        Re: For American numbers

                        [Scott David Daniels][color=blue][color=green]
                        >> 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[/color]
                        >
                        >[/color]

                        [Peter Hansen][color=blue]
                        > 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...[/color]

                        Maybe you missed these?





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

                        --
                        alan kennedy
                        ------------------------------------------------------
                        email alan: http://xhaus.com/contact/alan

                        Comment

                        • Roel Schroeven

                          #13
                          Re: For American numbers

                          Peter Hansen wrote:[color=blue]
                          > Roel Schroeven wrote:
                          >[color=green]
                          >> Peter Hansen wrote:
                          >>[color=darkred]
                          >>> 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.[/color]
                          >>
                          >>
                          >>
                          >> 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.[/color]
                          >
                          >
                          > 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.[/color]

                          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.
                          [color=blue]
                          > Given the clear "units='byt es'" 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.[/color]

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

                          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.
                          [color=blue]
                          > less likely to abandon backwards compatibility and widely accept
                          > 1000 than the US is likely to adopt metric widely in the near
                          > future...[/color]

                          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.

                          --
                          "Codito ergo sum"
                          Roel Schroeven

                          Comment

                          • Martin v. Löwis

                            #14
                            Re: For American numbers

                            Peter Hansen wrote:[color=blue]
                            > 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...[/color]

                            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

                            Comment

                            • Ruud de Jong

                              #15
                              Re: For American numbers

                              Dan Bishop schreef:[color=blue]
                              > Nick Craig-Wood wrote:
                              >[color=green]
                              >>Peter Hansen <peter@engcorp. com> wrote:
                              >>[color=darkred]
                              >>> 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...[/color]
                              >>
                              >>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[/color]
                              >
                              >
                              > They must have gotten the idea from floppy disks, which also use a
                              > 1024000-byte "megabyte".
                              >[/color]

                              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

                              Comment

                              Working...