Suggestion for multiples of bytes

847 views
Skip to first unread message

Joan Miller

unread,
Apr 4, 2010, 7:10:53 AM4/4/10
to golang-nuts
It should be included the constants related to storage capacity in the
package bytes.


/* Multiples of the unit byte, generally for computer storage. */
const (
KB = 1000 // kilobyte
MB = 1000000 // megabyte
GB = 1000000000 // gigabyte
TB = 1000000000000 // terabyte
PB = 1000000000000000 // petabyte
EB = 1000000000000000000 // exabyte
)

/* Multiples of the unit byte, generally for computer memory. */
const (
KiB = 1024 // kibibyte
MiB = 1048576 // mebibyte
GiB = 1073741824 // gibibyte
TiB = 1099511627776 // tebibyte
PiB = 1125899906842624 // pebibyte
EiB = 1152921504606846976 // exbibyte
)

abiosoft

unread,
Apr 4, 2010, 5:10:47 PM4/4/10
to golang-nuts
I think it may be useful sometimes

Blake Mizerany

unread,
Apr 4, 2010, 5:19:47 PM4/4/10
to abiosoft, golang-nuts
+1

I find myself defining these over and over when profiling/benchmarking.
I would be nice to always have them by default.

> --
> To unsubscribe, reply using "remove me" as the subject.
>

--
Blake Mizerany
blake.m...@gmail.com

Esko Luontola

unread,
Apr 4, 2010, 7:11:37 PM4/4/10
to golang-nuts
+1

To make sure the constants have really been calculated right, I would
prefer declaring the 2^10 based values like this:

const (
KiB = 1024 // kibibyte

MiB = 1024*1024 // mebibyte
GiB = 1024*1024*1024 // gibibyte
TiB = 1024*1024*1024*1024 // tebibyte
PiB = 1024*1024*1024*1024*1024 // pebibyte
EiB = 1024*1024*1024*1024*1024*1024 // exbibyte
)

Joan Miller

unread,
Apr 4, 2010, 7:28:07 PM4/4/10
to golang-nuts

for the another ones, could be used:

const (
KB = 1e3
MB = 1e6
...
)

Or to create a test that checks the literal constants of the first
post on this thread.

peterGo

unread,
Apr 4, 2010, 7:47:02 PM4/4/10
to golang-nuts
Esko,

If you doubt that constants have been calculated right, how do you
know that 2^10 is 1,024?

Why not use idiomatic Go?

const (
KiB = 1 << (10 * (iota + 1)) // kibibyte
MiB = 1 << (10 * (iota + 1)) // mebibyte
GiB = 1 << (10 * (iota + 1)) // gibibyte
TiB = 1 << (10 * (iota + 1)) // tebibyte
PiB = 1 << (10 * (iota + 1)) // pebibyte
EiB = 1 << (10 * (iota + 1)) // exbibyte
)

Peter

Peter Bourgon

unread,
Apr 5, 2010, 3:08:23 AM4/5/10
to golang-nuts
If you're going to do this (which I find arguably useful, at best)
seems like the 'byte' suffix isn't really accurate, as you're just
defining a scalar. Makes more sense to me to define in the 'math'
package (or similar), something like

const (
kilo = 1000
mega = 1000 * 1000
...
)

const (
kibi = 1024
mebi = 1024 * 1024
...
)

or however you like to manage your constants.

Joan Miller

unread,
Apr 5, 2010, 4:49:38 AM4/5/10
to golang-nuts
It makes sense since that it's more generic

On 5 abr, 07:08, Peter Bourgon <peterbour...@gmail.com> wrote:
> If you're going to do this (which I find arguably useful, at best)
> seems like the 'byte' suffix isn't really accurate, as you're just
> defining a scalar. Makes more sense to me to define in the 'math'
> package (or similar), something like
>
>   const (
>       kilo = 1000
>       mega = 1000 * 1000
>       ...
>   )
>
>   const (
>       kibi = 1024
>       mebi = 1024 * 1024
>       ...
>   )
>
> or however you like to manage your constants.
>

Joan Miller

unread,
Apr 5, 2010, 4:51:42 AM4/5/10
to golang-nuts
yeah, the bits arithmetic is faster than multiplications.
+1

Alexander Surma

unread,
Apr 5, 2010, 5:39:43 AM4/5/10
to Joan Miller, golang-nuts
> yeah, the bits arithmetic is faster than multiplications.
Which is highly irrelevant for compiletime constants... isn't it

Joan Miller

unread,
Apr 5, 2010, 5:42:29 AM4/5/10
to golang-nuts

On 5 abr, 09:39, Alexander Surma <alexander.su...@googlemail.com>
wrote:


> > yeah, the bits arithmetic is faster than multiplications.
>
> Which is highly irrelevant for compiletime constants... isn't it

It will be faster to compile the package with those constants

Joan Miller

unread,
Apr 5, 2010, 5:04:08 PM4/5/10
to golang-nuts

On 5 abr, 08:49, Joan Miller <pelok...@gmail.com> wrote:
> It makes sense since that it's more generic
>
> On 5 abr, 07:08, Peter Bourgon <peterbour...@gmail.com> wrote:
>
> > If you're going to do this (which I find arguably useful, at best)
> > seems like the 'byte' suffix isn't really accurate, as you're just
> > defining a scalar. Makes more sense to me to define in the 'math'
> > package (or similar), something like
>
> >   const (
> >       kilo = 1000
> >       mega = 1000 * 1000
> >       ...
> >   )
>
> >   const (
> >       kibi = 1024
> >       mebi = 1024 * 1024
> >       ...
> >   )

I've been reading about it [1] and I think that hasn't sense use only
the binary prefix since that they're only used in the units of digital
information.

[1] http://en.wikipedia.org/wiki/Binary_prefix

Alexander Surma

unread,
Apr 5, 2010, 5:07:22 PM4/5/10
to Joan Miller, golang-nuts
> since that they're only used in the units of digital
> information.

Never been into physics much, huh? ;)

Joan Miller

unread,
Apr 5, 2010, 5:07:43 PM4/5/10
to golang-nuts
/* Multiples of the unit byte, generally for computer storage. */
const (
KB = int(1e3) // kilobyte
MB = int(1e6) // megabyte
GB = int(1e9) // gigabyte
TB = int(1e12) // terabyte
PB = int(1e15) // petabyte
EB = int(1e18) // exabyte
)

/* Multiples of the unit byte, generally for computer memory. */

const (
KiB = 1 << (10 * (iota + 1)) // kibibyte
MiB = 1 << (10 * (iota + 1)) // mebibyte
GiB = 1 << (10 * (iota + 1)) // gibibyte
TiB = 1 << (10 * (iota + 1)) // tebibyte
PiB = 1 << (10 * (iota + 1)) // pebibyte
EiB = 1 << (10 * (iota + 1)) // exbibyte
)

Joan Miller

unread,
Apr 5, 2010, 5:13:55 PM4/5/10
to golang-nuts

On 5 abr, 21:07, Alexander Surma <alexander.su...@googlemail.com>
wrote:


> > since that they're only used in the units of digital
> > information.
>
> Never been into physics much, huh? ;)

I mainly refer to binary prefixes like kibibyte, mebibyte.

Peter Bourgon

unread,
Apr 5, 2010, 6:05:27 PM4/5/10
to golang-nuts
On Mon, Apr 5, 2010 at 11:04 PM, Joan Miller <pelo...@gmail.com> wrote:
> I've been reading about it [1] and I think that hasn't sense use only
> the binary prefix since that they're only used in the units of digital
> information.

The point is, calling the actual scalar number 1024 a "kibibyte" is
incorrect, because it's _only_ a scalar number, ie. there's no "byte"
involved. One can have fifteen kibibits, four kilotonnes, a megajoule,
1.21 gigawatts, etc., and defining those values must be type-agnostic
in order to be correct. Meaning,

great_scott := 1.21 * math.Giga * Watt // correct
great_scott := 1.21 * bytes.GB * Watt // incorrect -- 1.21 giga byte watts?

Joan Miller

unread,
Apr 5, 2010, 6:40:46 PM4/5/10
to golang-nuts

On 5 abr, 22:05, Peter Bourgon <peterbour...@gmail.com> wrote:

Ok. You're right
http://physics.nist.gov/cuu/Units/binary.html

/* Standard prefixes for the International System of Units */
const (
Kilo = int(1e3)
Mega = int(1e6)
Giga = int(1e9)
Tera = int(1e12)
Peta = int(1e15)
Exa = int(1e18)
)

/* Prefixes for binary multiples. */
const (
Kibi = 1 << (10 * (iota + 1))
Mebi = 1 << (10 * (iota + 1))
Gibi = 1 << (10 * (iota + 1))
Tebi = 1 << (10 * (iota + 1))
Pebi = 1 << (10 * (iota + 1))
Exbi = 1 << (10 * (iota + 1))
)

peterGo

unread,
Apr 5, 2010, 8:52:45 PM4/5/10
to golang-nuts
Joan,

You should check that sample code at least compiles. This doesn't.

> /* Standard prefixes for the International System of Units */
> const (
> Kilo = int(1e3)
> Mega = int(1e6)
> Giga = int(1e9)
> Tera = int(1e12)
> Peta = int(1e15)
> Exa = int(1e18)
> )

Omit the conversions to int; they are wrong (overflow) and
unnecessary.

Peter

On Apr 5, 6:40 pm, Joan Miller <pelok...@gmail.com> wrote:
> On 5 abr, 22:05, Peter Bourgon <peterbour...@gmail.com> wrote:> On Mon, Apr 5, 2010 at 11:04 PM, Joan Miller <pelok...@gmail.com> wrote:
> > > I've been reading about it [1] and I think that hasn't sense use only
> > > the binary prefix since that they're only used in the units of digital
> > > information.
>
> > The point is, calling the actual scalar number 1024 a "kibibyte" is
> > incorrect, because it's _only_ a scalar number, ie. there's no "byte"
> > involved. One can have fifteen kibibits, four kilotonnes, a megajoule,
> > 1.21 gigawatts, etc., and defining those values must be type-agnostic
> > in order to be correct. Meaning,
>
> >   great_scott := 1.21 * math.Giga * Watt // correct
> >   great_scott := 1.21 * bytes.GB * Watt // incorrect -- 1.21 giga byte watts?
>

> Ok. You're righthttp://physics.nist.gov/cuu/Units/binary.html

Joan Miller

unread,
Apr 6, 2010, 3:26:16 AM4/6/10
to golang-nuts

On 6 abr, 00:52, peterGo <go.peter...@gmail.com> wrote:
> Joan,
>
> You should check that sample code at least compiles. This doesn't.
>
> > /* Standard prefixes for the International System of Units */
> > const (
> >         Kilo = int(1e3)
> >         Mega = int(1e6)
> >         Giga = int(1e9)
> >         Tera = int(1e12)
> >         Peta = int(1e15)
> >         Exa = int(1e18)
> > )
>
> Omit the conversions to int; they are wrong (overflow) and
> unnecessary.
I only had checked it for Giga. I supposed that would be correct for
the anothers.

Joan Miller

unread,
Apr 6, 2010, 4:02:27 AM4/6/10
to golang-nuts
I've added the another prefixes for the SI.

/* Standard prefixes for the International System of Units.
In computer, they are generally used for storage.
*/
const (
Deka = 1e1 // da
Hecto = 1e2 // h
Kilo = 1e3 // k
Mega = 1e6 // M
Giga = 1e9 // G
Tera = 1e12 // T
Peta = 1e15 // P
Exa = 1e18 // E
Zeta = 1e21 // Z
Yotta = 1e23 // Y
)

/* Prefixes for binary multiples. Generally used for computer memory.
*/
const (
Kibi = 1 << (10 * (iota + 1)) // Ki
Mebi = 1 << (10 * (iota + 1)) // Mi
Gibi = 1 << (10 * (iota + 1)) // Gi
Tebi = 1 << (10 * (iota + 1)) // Ti
Pebi = 1 << (10 * (iota + 1)) // Pi
Exbi = 1 << (10 * (iota + 1)) // Ei
)

Joan Miller

unread,
Apr 6, 2010, 5:21:33 AM4/6/10
to golang-nuts
It's now complete.

/* Standard prefixes from the International System of Units (SI).
In computer, the positive units are generally used for storage.


*/
const (
Deka = 1e1 // da
Hecto = 1e2 // h
Kilo = 1e3 // k
Mega = 1e6 // M
Giga = 1e9 // G
Tera = 1e12 // T
Peta = 1e15 // P
Exa = 1e18 // E
Zeta = 1e21 // Z
Yotta = 1e23 // Y

// Negative units
Deci = 1e-1 // d
Centi = 1e-2 // c
Mili = 1e-3 // m
Micro = 1e-6 // µ
Nano = 1e-9 // n
Pico = 1e-12 // p
Femto = 1e-15 // f
Atto = 1e-18 // a
Zepto = 1e-21 // z
Yocto = 1e-23 // y
)

/* Prefixes from the National Institute of Standards and Technology
(IEC).
These prefixes for binary will better express electronic information.


Generally used for computer memory.
*/
const (
Kibi = 1 << (10 * (iota + 1)) // Ki
Mebi = 1 << (10 * (iota + 1)) // Mi
Gibi = 1 << (10 * (iota + 1)) // Gi
Tebi = 1 << (10 * (iota + 1)) // Ti
Pebi = 1 << (10 * (iota + 1)) // Pi
Exbi = 1 << (10 * (iota + 1)) // Ei
)

Esko Luontola

unread,
Apr 6, 2010, 6:05:59 AM4/6/10
to golang-nuts
Some typos:

Zeta -> Zetta
Mili -> Milli

http://en.wikipedia.org/wiki/SI_prefix#List_of_SI_prefixes

Esko Luontola

unread,
Apr 6, 2010, 6:09:12 AM4/6/10
to golang-nuts
On Apr 6, 1:05 pm, Esko Luontola <esko.luont...@gmail.com> wrote:
> Some typos:
>
> Zeta -> Zetta
> Mili -> Milli
>
> http://en.wikipedia.org/wiki/SI_prefix#List_of_SI_prefixes

And one more:

Deka -> Deca

Also here is a more authoritative source than Wikipedia:
http://www.bipm.org/en/si/prefixes.html

Joan Miller

unread,
Apr 6, 2010, 6:20:20 AM4/6/10
to golang-nuts
You've reason about Milli but I'm not sure about the anothers (Deka
and Zeta).

http://www.thic.org/pdf/Jun04/fujifilm.mmccorkle.pdf

Joan Miller

unread,
Apr 6, 2010, 6:38:44 AM4/6/10
to golang-nuts
Well, according to these 2 pages:

http://physics.nist.gov/cuu/Units/prefixes.html
http://www.unc.edu/~rowlett/units/prefixes.html

Zeta -> Zetta
Mili -> Milli

as you said, but Deka is right.

Joan Miller

unread,
Apr 6, 2010, 6:43:26 AM4/6/10
to golang-nuts
and has been fixed another failure:

Yotta = 1e24
Yocto = 1e-24

On 6 abr, 10:38, Joan Miller <pelok...@gmail.com> wrote:
> Well, according to these 2 pages:
>

> http://physics.nist.gov/cuu/Units/prefixes.htmlhttp://www.unc.edu/~rowlett/units/prefixes.html

Joan Miller

unread,
Apr 6, 2010, 7:21:29 AM4/6/10
to golang-nuts

On 5 abr, 07:08, Peter Bourgon <peterbour...@gmail.com> wrote:
> Makes more sense to me to define in the 'math' package (or similar),
What about package unit, under math?

import "math/unit"

Joan Miller

unread,
Apr 6, 2010, 9:16:52 AM4/6/10
to golang-nuts
Added to the Code Review Tool

http://codereview.appspot.com/891042/show

Peter

unread,
Apr 6, 2010, 9:40:18 AM4/6/10
to golang-nuts
I'm sorry, is this thread just a large troll?

You're talking about defining *numbers*. In order for it to be worth
it, those numbers have to be so ubiquitous to mathematics, or used so
much in software engineering that their absence would be obvious.
Numbers like pi.

"1e-23" does not fall into the category of "often used and annoying to
recalculate". In fact none of these do.

In the interest of cluttering up the math package further, may I
suggest the following constants:

const(
Seven = 7
FiveAndAHalf = 5.5
CubeRootOfEight = 2.08008382
PointFour = 0.4
Googol = 1e100
)

Peter

Peter Bourgon

unread,
Apr 6, 2010, 9:51:36 AM4/6/10
to golang-nuts
On Tue, Apr 6, 2010 at 3:40 PM, Peter <peter.a...@gmail.com> wrote:
> "1e-23" does not fall into the category of "often used and annoying to
> recalculate". In fact none of these do.

Frankly, I agree.

chris dollin

unread,
Apr 6, 2010, 9:56:35 AM4/6/10
to Peter, golang-nuts
On 6 April 2010 14:40, Peter <peter.a...@gmail.com> wrote:
I'm sorry, is this thread just a large troll?

You're talking about defining *numbers*. In order for it to be worth
it, those numbers have to be so ubiquitous to mathematics, or used so
much in software engineering that their absence would be obvious.
Numbers like pi.

Or numbers whose names are useful in expressing scale,
like Kilo and Giga.

But I think they would be better in a non-core package; put them
on one of goinstall's well-known sites and let people who want
them install them.

--
Chris "allusive" Dollin

Joan Miller

unread,
Apr 6, 2010, 10:29:26 AM4/6/10
to golang-nuts
Well, simply were added the negative units.

Joan Miller

unread,
Apr 6, 2010, 10:41:06 AM4/6/10
to golang-nuts

On 6 abr, 13:40, Peter <peter.armit...@gmail.com> wrote:

> I'm sorry, is this thread just a large troll?

One thing which is very clear is that this thread is not a troll. Here
you have the definition of troll [1].

I prefer to create a thread where to discuss about a proposal before
of directly upload the code to the review tool. And thanks to it,
other people on this thread has contributed to the initial proposal.


[1] http://en.wikipedia.org/wiki/Troll_%28Internet%29

Rob 'Commander' Pike

unread,
Apr 6, 2010, 2:35:15 PM4/6/10
to Joan Miller, golang-nuts

On Apr 6, 2010, at 2:21 AM, Joan Miller wrote:

> /* Prefixes from the National Institute of Standards and Technology
> (IEC).
> These prefixes for binary will better express electronic information.
> Generally used for computer memory.
> */
> const (
> Kibi = 1 << (10 * (iota + 1)) // Ki
> Mebi = 1 << (10 * (iota + 1)) // Mi
> Gibi = 1 << (10 * (iota + 1)) // Gi
> Tebi = 1 << (10 * (iota + 1)) // Ti
> Pebi = 1 << (10 * (iota + 1)) // Pi
> Exbi = 1 << (10 * (iota + 1)) // Ei
> )

You're working far too hard. You're even using too many parentheses.

const (
_ = iota // skip zero
Kibi = 1 << 10*iota
Mebi
Gibi
Tebi
Pebi
Exbi
)

-rob

Rob 'Commander' Pike

unread,
Apr 6, 2010, 5:40:12 PM4/6/10
to Joan Miller, golang-nuts
This thread isn't long enough yet, so Ken points out that the () are
indeed necessary since << and * are at the same precedence level. But
then he suggests

const (
_ = 1 << (10*iota)
Kibi

Reply all
Reply to author
Forward
0 new messages