Topic: suffix for integral numbers


Author: pavel_vozenilek@yahoo.co.uk (Pavel Vozenilek)
Date: Fri, 31 May 2002 15:14:46 GMT
Raw View
ros0230@iperbole.bologna.it (Natale Fietta) wrote in message
> If needed we can already make this:
>
> const int K = 1024
> if ( a > 2*K )
>
That's the way with the same expressiveness as a suffix. Silly me, I
didn't think this.

Thanks,
/Pavel

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Allan_W@my-dejanews.com (Allan W)
Date: Fri, 31 May 2002 21:26:36 GMT
Raw View
Dan Smart <cppdan@dansmart.com> wrote
[some very good points]
and then added:

> It
> is worth noting that the only two numbers that affect "efficient" use of
> space are alignment, and maybe (just maybe) page size.

There's also the block and/or cluster size on disk, which isn't
always the same as the page size. If your disk I/O uses 511 bytes
per fixed-length record, you might find that "wasting" one byte
per record will help performance tremendously.

...not that I would do this without profiling first.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: pavel_vozenilek@yahoo.co.uk (Pavel Vozenilek)
Date: Tue, 28 May 2002 01:37:38 GMT
Raw View
> > A bit of syntactical sugar: define three new suffixes (K, M, G) for
> > integer constants.
> >
> But what about Terra, Exa and Peta? Why are you excluding them?
> Of course 'E' for Exa is problematic...
>
The intention is convenience shortcut. Kilo- to giga- sized numbers
are used quite often, unlike the bigger ones and these suffixes are
used freely. Say,  machine with 128MB or RAM and disk 12GB....

> > This would allow a writer avoid expressions like:
> >     if (a > 2185232384) { ...
> Of course I would avoid expressions like the above by electrocuting the
> programmer responsible. There is nothing wrong with:
>      static const size_t k_maxSizeOfA = 2 * 1024 * 1024 // 2 Mb
>      ...
>      if (a > k_maxSizeOfA) {...
>
Maybe rather this:

  char buffer[5K];
  doSomething(buffer, sizeof(buffer));

The numeric value may be used in only one place and result the
statement can be read without stopping to decipher (5 * 1024). Also
there is no need to add explaing comments.

/Pavel

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: pavel_vozenilek@yahoo.co.uk (Pavel Vozenilek)
Date: Tue, 28 May 2002 15:09:59 GMT
Raw View
John G Harris <john@nospam.demon.co.uk> wrote in message news:<2T9SQKF8P878Ew2S@jgharris.demon.co.uk>...
>
> There must be thousands of ISO and National standards that use M and G
> to mean powers of 1000. You need different symbols such as M2 and G2
> (meaning nearest power of 2). For consistency you would then use k2.
>
Sure, but K as 1024, etc. become de-facto standard in IT. I don't know
if one can unlearn their domain specific meaning.

/Pavel

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Sungbom Kim <musiphil@bawi.org>
Date: Tue, 28 May 2002 17:45:18 GMT
Raw View
Pavel Vozenilek wrote:
>
> > > A bit of syntactical sugar: define three new suffixes (K, M, G) for
> > > integer constants.
> > >
> > But what about Terra, Exa and Peta? Why are you excluding them?
> > Of course 'E' for Exa is problematic...
> >
> The intention is convenience shortcut. Kilo- to giga- sized numbers
> are used quite often, unlike the bigger ones and these suffixes are
> used freely. Say,  machine with 128MB or RAM and disk 12GB....

Where would you draw the line between often and not?
How about adopting suffixes centi, milli, micro, nano and so on
for floating-point constants? :-)

By the way, strictly according to the SI unit system,

   kilo- is 10^3, not 2^10 (and spelled 'k', not 'K'),
   mega- is 10^6, not 2^20,
   giga- is 10^9, not 2^30.

Someone would see 3M in codes and take it as 3*10^6,
or argue that it should be so.

I think that, for 2^10, 3*2^20 and so on, it would be better and wise
to stick to 1<<10, 3<<20 and so on, to avoid confusion.

--
Sungbom Kim <musiphil@bawi.org>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Dan Smart <cppdan@dansmart.com>
Date: Tue, 28 May 2002 17:47:32 GMT
Raw View
pavel_vozenilek@yahoo.co.uk (Pavel Vozenilek) wrote in
news:731020ca.0205271722.3d443745@posting.google.com:
>> But what about Terra, Exa and Peta? Why are you excluding them?
>> Of course 'E' for Exa is problematic...
>>
> The intention is convenience shortcut. Kilo- to giga- sized numbers
> are used quite often, unlike the bigger ones and these suffixes are
> used freely. Say,  machine with 128MB or RAM and disk 12GB....
>
Firstly, it wasn't that long ago that giga- was ridiculously big. The
new machine I've just ordered has 1/3 of a terrabyte of disk (which is only
340G). Secondly, if you are going to allow k,m and g; for consistency you
should allow the remainder. Of course I'm arguing you shouldn't allow any
of them, because they are not sugar, but sacharinne.

> Maybe rather this:
>
>   char buffer[5K];
>   doSomething(buffer, sizeof(buffer));
>
> The numeric value may be used in only one place and result the
> statement can be read without stopping to decipher (5 * 1024). Also
> there is no need to add explaing comments.
But this is my point, You say that if you use 5K there is no need for an
explaining comment. I say the number is irrelevant, what is important is
why that number was chosen; hence you always need the comment, but how the
number is written is unimportant.

In your example, why is the buffer size 5K, why not 6K, or 5001, or 5K+1
(don't forget the \0)? Thus the above would be better coded as:

/*
 * The maximum record size is 5K bytes, and records
 * are not null terminated.
 * See page n of foo for details on format and size.
 */
static const unsigned int k_bufferSize = 5*1024;
....
char buffer[k_bufferSize];

Deciphering 5*1024 takes no time at all, whereas knowing why the value
5*1024 was used is likely very important.

As an aside, programmers seem fixated by binary powers, which is odd
considering that very few things in the real world are governed by such. It
is worth noting that the only two numbers that affect "efficient" use of
space are alignment, and maybe (just maybe) page size.
>
> /Pavel
>

Dan "Size things appropiately, n*2^(10*m) for m > 0 is unlikely to be
appropiate" Smart
--
Dan Smart. C++ Programming and Mentoring.
cppdan@dansmart.com
ADDvantaged

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ros0230@iperbole.bologna.it (Natale Fietta)
Date: Tue, 28 May 2002 18:33:16 GMT
Raw View
On Fri, 24 May 2002 19:49:06 CST, pavel_vozenilek@yahoo.co.uk (Pavel
Vozenilek) wrote:

>A bit of syntactical sugar: define three new suffixes (K, M, G) for
>integer constants.
>
>  1K (= 1024)
>  2M (= 2 * 1024 * 1024)
>  3G (= 3 * 1024 * 1024 * 1024)
>
>This would allow a writer avoid expressions like:
>    if (a > 2 * 1024 * 1024) { ...
>or even:
>    if (a > 2185232384) { ...
>
>that are error prone and hard to grasp quickly.

I am quite happy with 2 * 1024 * 1024 (well, i really prefer a named
constant instead of a literal, but this is not the question at hand),
instead i found unreadable 2K or 2M...
why K must be 1024 instead of 1000 (the usual meaning of K anywere
except computer science) ?
why M must be Mega istead of Meters or some other thing ?

If needed we can already make this:

const int K = 1024
if ( a > 2*K )

and also the more readables:

const int KiloBytes = 1024;
if ( a > 2*KiloBytes ) //do you really prefer if (a>2K) over this ?

const int KiloMeters = 1000;
if ( a > 2*KiloMeters )

Regards,
Natale Fietta

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Ken Alverson" <Ken@Alverson.com>
Date: Tue, 28 May 2002 19:04:09 GMT
Raw View
"Sungbom Kim" <musiphil@bawi.org> wrote in message
news:3CF196A0.832923DE@bawi.org...
> Pavel Vozenilek wrote:
> >
> > A bit of syntactical sugar: define three new suffixes (K, M, G) for
> > integer constants.
> >
> > E.g.:
> >
> >   1K (= 1024)
> >   2M (= 2 * 1024 * 1024)
> >   3G (= 3 * 1024 * 1024 * 1024)
>
> You can use 1<<10, 2<<20, 3<<30 instead.

Shifting is good, but compare the likely result of:

int elevenGig = 11 << 30;
with
int elevenGig = 11G;

Assuming 32 bit integers, the first statement will likely succeed, silently,
with undesired results.  The second statement, on the other hand, will
likely result in a truncation warning.

A warning could be generated for the first case, but it would likely annoy
just as many people as it would help...

Ken


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Sungbom Kim <musiphil@bawi.org>
Date: Wed, 29 May 2002 19:33:01 GMT
Raw View
Ken Alverson wrote:
>
> Shifting is good, but compare the likely result of:
>
> int elevenGig = 11 << 30;
> with
> int elevenGig = 11G;
>
> Assuming 32 bit integers, the first statement will likely succeed, silently,
> with undesired results.  The second statement, on the other hand, will
> likely result in a truncation warning.

It's a QoI issue IMO. Any decent compiler can easily give an overflow/
truncation warning for such a constant expression. Is there any reason
that the two should be handled differently?

> A warning could be generated for the first case, but it would likely annoy
> just as many people as it would help...

Why is that? I think such a warning would be always desirable.

--
Sungbom Kim <musiphil@bawi.org>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Dan Smart <cppdan@dansmart.com>
Date: Sun, 26 May 2002 16:01:17 GMT
Raw View
pavel_vozenilek@yahoo.co.uk (Pavel Vozenilek) wrote in
news:731020ca.0205241510.42e553e4@posting.google.com:
>
> A bit of syntactical sugar: define three new suffixes (K, M, G) for
> integer constants.
>
But what about Terra, Exa and Peta? Why are you excluding them?
Of course 'E' for Exa is problematic...
> E.g.:
>
>   1K (= 1024)
>   2M (= 2 * 1024 * 1024)
>   3G (= 3 * 1024 * 1024 * 1024)
>
> This would allow a writer avoid expressions like:
>     if (a > 2 * 1024 * 1024) { ...
> or even:
>     if (a > 2185232384) { ...
Of course I would avoid expressions like the above by electrocuting the
programmer responsible. There is nothing wrong with:
     static const size_t k_maxSizeOfA = 2 * 1024 * 1024 // 2 Mb
     ...
     if (a > k_maxSizeOfA) {...
>
> that are error prone and hard to grasp quickly.
>
Which is why they should never be written, note that
if (a > 2M) {..
is just as error prone and hard to grasp quickly, "why is 2M special?"

Dan "Eschew 37 Magic Numbers" Smart
--
Dan Smart. C++ Programming and Mentoring.
cppdan@dansmart.com
ADDvantaged

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Gennaro Prota <gennaro_prota@yahoo.com>
Date: Sun, 26 May 2002 16:02:38 GMT
Raw View
On Sun, 26 May 2002 00:58:49 GMT, John G Harris
<john@nospam.demon.co.uk> wrote:

>There must be thousands of ISO and National standards that use M and G
>to mean powers of 1000. You need different symbols such as M2 and G2
>(meaning nearest power of 2). For consistency you would then use k2.

Maybe we shift a little off-topic here, but the following is worth to
take a look at:

http://groups.google.it/groups?threadm=32CEEC6C.292B%40cs.purdue.edu


Genny.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: rhairgroveNoSpam@Pleasebigfoot.com (Bob Hairgrove)
Date: Tue, 28 May 2002 00:42:16 GMT
Raw View
On Fri, 24 May 2002 19:49:06 CST, pavel_vozenilek@yahoo.co.uk (Pavel
Vozenilek) wrote:

>"Tom Puverle" <tp225@cam.ac.uk> wrote in message
>> I am trying to put together a wish list of language extensions
>> that people would like to see in C++0x.
>>
>
>A bit of syntactical sugar: define three new suffixes (K, M, G) for
>integer constants.
>
>E.g.:
>
>  1K (= 1024)
>  2M (= 2 * 1024 * 1024)
>  3G (= 3 * 1024 * 1024 * 1024)
>
>This would allow a writer avoid expressions like:
>    if (a > 2 * 1024 * 1024) { ...
>or even:
>    if (a > 2185232384) { ...
>
>that are error prone and hard to grasp quickly.
>
>These suffixes can coexist with U and L ones:
>
>   1KU (= 1024 unsigned)
>   2GL (= 2 * 1024 * 1024 * 1024 long)
>
>

I doubt that this would be very useful, because it's a bad idea to
hard-wire such "magic numbers" in your code anyway ... you should use
appropriate constants instead.


Bob Hairgrove
rhairgroveNoSpam@Pleasebigfoot.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: rhairgroveNoSpam@Pleasebigfoot.com (Bob Hairgrove)
Date: Tue, 28 May 2002 00:43:28 GMT
Raw View
On Sun, 26 May 2002 16:01:17 GMT, Dan Smart <cppdan@dansmart.com>
wrote:

>> This would allow a writer avoid expressions like:
>>     if (a > 2 * 1024 * 1024) { ...
>> or even:
>>     if (a > 2185232384) { ...
>Of course I would avoid expressions like the above by electrocuting the
>programmer responsible. There is nothing wrong with:
>     static const size_t k_maxSizeOfA = 2 * 1024 * 1024 // 2 Mb
>     ...
>     if (a > k_maxSizeOfA) {...


I agree 100%, which is why I do believe there is absolutely no need
for this.


Bob Hairgrove
rhairgroveNoSpam@Pleasebigfoot.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Sungbom Kim <musiphil@bawi.org>
Date: Tue, 28 May 2002 00:43:46 GMT
Raw View
Pavel Vozenilek wrote:
>
> A bit of syntactical sugar: define three new suffixes (K, M, G) for
> integer constants.
>
> E.g.:
>
>   1K (= 1024)
>   2M (= 2 * 1024 * 1024)
>   3G (= 3 * 1024 * 1024 * 1024)

You can use 1<<10, 2<<20, 3<<30 instead.

--
Sungbom Kim <musiphil@bawi.org>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: pavel_vozenilek@yahoo.co.uk (Pavel Vozenilek)
Date: Fri, 24 May 2002 19:49:06 CST
Raw View
"Tom Puverle" <tp225@cam.ac.uk> wrote in message
> I am trying to put together a wish list of language extensions
> that people would like to see in C++0x.
>

A bit of syntactical sugar: define three new suffixes (K, M, G) for
integer constants.

E.g.:

  1K (= 1024)
  2M (= 2 * 1024 * 1024)
  3G (= 3 * 1024 * 1024 * 1024)

This would allow a writer avoid expressions like:
    if (a > 2 * 1024 * 1024) { ...
or even:
    if (a > 2185232384) { ...

that are error prone and hard to grasp quickly.

These suffixes can coexist with U and L ones:

   1KU (= 1024 unsigned)
   2GL (= 2 * 1024 * 1024 * 1024 long)


/Pavel

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: John G Harris <john@nospam.demon.co.uk>
Date: Sun, 26 May 2002 00:58:49 GMT
Raw View
In article <731020ca.0205241510.42e553e4@posting.google.com>, Pavel
Vozenilek <pavel_vozenilek@yahoo.co.uk> writes
>"Tom Puverle" <tp225@cam.ac.uk> wrote in message
>> I am trying to put together a wish list of language extensions
>> that people would like to see in C++0x.
>>
>
>A bit of syntactical sugar: define three new suffixes (K, M, G) for
>integer constants.
>
>E.g.:
>
>  1K (= 1024)
>  2M (= 2 * 1024 * 1024)
>  3G (= 3 * 1024 * 1024 * 1024)
  <snip>

There must be thousands of ISO and National standards that use M and G
to mean powers of 1000. You need different symbols such as M2 and G2
(meaning nearest power of 2). For consistency you would then use k2.

Now K can be reserved for a restricted float type that can never be less
than 0  :-)

  John
--
John Harris
  mailto:john@jgharris.demon.co.uk

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Mathew Hendry <mathewhendry@hotmail.com>
Date: Sun, 26 May 2002 15:57:40 GMT
Raw View
On Sun, 26 May 2002 00:58:49 GMT, John G Harris
<john@nospam.demon.co.uk> wrote:

>In article <731020ca.0205241510.42e553e4@posting.google.com>, Pavel
>Vozenilek <pavel_vozenilek@yahoo.co.uk> writes
>
>>A bit of syntactical sugar: define three new suffixes (K, M, G) for
>>integer constants.
>>
>>  1K (= 1024)
>>  2M (= 2 * 1024 * 1024)
>>  3G (= 3 * 1024 * 1024 * 1024)
>
>There must be thousands of ISO and National standards that use M and G
>to mean powers of 1000. You need different symbols such as M2 and G2
>(meaning nearest power of 2). For consistency you would then use k2.

IEC has already come up with some suitable symbols: Ki, Mi, Gi, ...

  http://physics.nist.gov/cuu/Units/binary.html

-- Mat.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]