/* 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
)
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
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
)
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.
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
const (
kilo = 1000
mega = 1000 * 1000
...
)
const (
kibi = 1024
mebi = 1024 * 1024
...
)
or however you like to manage your constants.
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.
>
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
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.
Never been into physics much, huh? ;)
/* 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
)
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.
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?
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))
)
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
/* 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
)
/* 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
)
And one more:
Deka -> Deca
Also here is a more authoritative source than Wikipedia:
http://www.bipm.org/en/si/prefixes.html
http://www.thic.org/pdf/Jun04/fujifilm.mmccorkle.pdf
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.
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
import "math/unit"
http://codereview.appspot.com/891042/show
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
Frankly, I agree.
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.
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.
> /* 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
const (
_ = 1 << (10*iota)
Kibi