473,883 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Natural size: int


On modern 32-Bit PC's, the following setup is common:

char: 8-Bit
short: 16-Bit
int: 32-Bit
long: 32-Bit

"char" is commonly used to store text characters.
"short" is commonly used to store large arrays of numbers, or perhaps wide
text characters (via wchar_t).
"int" is commonly used to store an integer.
"long" is commonly used to store an integer greater than 65535.

Now that 64-Bit machines are coming in, how should the integer types be
distributed? It makes sense that "int" should be 64-Bit... but what should
be done with "char" and "short"? Would the following be a plausible setup?

char: 8-Bit
short: 16-Bit
int: 64-Bit
long: 64-Bit

Or perhaps should "short" be 32-Bit? Or should "char" become 16-Bit (i.e.
16 == CHAR_BIT).

Another semi-related question:

If we have a variable which shall store the quantity of elements in an
array, then should we use "size_t"? On a system where "size_t" maps to
"long unsigned" rather than "int unsigned", it would seem to be inefficient
most of the time. "int unsigned" guarantees us at least 65535 array
elements -- what percentage of the time do we have an array any bigger than
that? 2% maybe? Therefore would it not make sense to use unsigned rather
than size_t to store array lengths (or the positive result of subtracting
pointers)?

--

Frederick Gotham
Aug 8 '06
78 3915

"Keith Thompson" <ks***@mib.orgh a scritto nel messaggio
news:ln******** ****@nuthaus.mi b.org...

[...]
Is it possible to write a macro such that
UNSIGNED_TYPE(1 000000)
portably expands to either "unsigned int" or "unsigned long",
depending on which type can hold the specified value? I suspect not.
#define UNSIGNED_TYPE(x ) x ## u

--
Giorgio Silvestri
DSP/Embedded/Real Time OS Software Engineer

Aug 10 '06 #61

"Giorgio Silvestri" <gi************ **@libero.itha scritto nel messaggio
news:gD******** *************@t wister1.libero. it...
>
"Keith Thompson" <ks***@mib.orgh a scritto nel messaggio
news:ln******** ****@nuthaus.mi b.org...

[...]
Is it possible to write a macro such that
UNSIGNED_TYPE(1 000000)
portably expands to either "unsigned int" or "unsigned long",
depending on which type can hold the specified value? I suspect not.

#define UNSIGNED_TYPE(x ) x ## u

ops. you probably mean textually "unsigned int" or "unsigned long" ...

--
Giorgio Silvestri
DSP/Embedded/Real Time OS Software Engineer

Aug 10 '06 #62
jacob navia <ja***@jacob.re mcomp.frwrites:
[...]
Look, if you have
double a,b,c,d;
...
d = a*b+c;

you need more precision? You do:
qfloat a,b,c,d;
...
d = a*b+c;

You see?
Yes, jacob, we understand. Most of us know what operator overloading
is.

C doesn't have it. We discuss C here. I'm very happy for you that
you've implemented what may or may not be a useful extension, but it's
off-topic here.

If you want to propose a change to the language, take it to
comp.std.c. If you want to discuss some non-standard extension, take
it to comp.compilers. lcc.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 10 '06 #63
"Giorgio Silvestri" <gi************ **@libero.itwri tes:
"Giorgio Silvestri" <gi************ **@libero.itha scritto nel messaggio
news:gD******** *************@t wister1.libero. it...
>"Keith Thompson" <ks***@mib.orgh a scritto nel messaggio
news:ln******* *****@nuthaus.m ib.org...
[...]
Is it possible to write a macro such that
UNSIGNED_TYPE(1 000000)
portably expands to either "unsigned int" or "unsigned long",
depending on which type can hold the specified value? I suspect not.

#define UNSIGNED_TYPE(x ) x ## u

ops. you probably mean textually "unsigned int" or "unsigned long" ...
Yes. For example:

UNSIGNED_TYPE(1 000000) obj;

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 10 '06 #64


Keith Thompson wrote On 08/10/06 17:00,:
"Giorgio Silvestri" <gi************ **@libero.itwri tes:
>>"Giorgio Silvestri" <gi************ **@libero.itha scritto nel messaggio
news:gD****** *************** @twister1.liber o.it...
>>>"Keith Thompson" <ks***@mib.orgh a scritto nel messaggio
news:ln***** *******@nuthaus .mib.org...
[...]

Is it possible to write a macro such that
UNSIGNED_TYPE(1 000000)
portably expands to either "unsigned int" or "unsigned long",
depending on which type can hold the specified value? I suspect not.

#define UNSIGNED_TYPE(x ) x ## u

ops. you probably mean textually "unsigned int" or "unsigned long" ...


Yes. For example:

UNSIGNED_TYPE(1 000000) obj;
All I can think of is

#include <limits.h>
#define T unsigned int
typedef T T0;
typedef T T1;
...
typedef T T65535;
#if UINT_MAX == 65535
#undef T
#define T unsigned long
#endif
typedef T T65536;
typedef T T65537;
...
typedef T T131071;
#if UINT_MAX == 131071
#undef T
#define T unsigned long
#endif
typedef T T131072
...
typedef T T4294967295
... /* !!! */
#undef T /* for cleanliness' sake */
#define UNSIGNED_TYPE(x ) T ## x

.... which might be deemed less than elegant.

--
Er*********@sun .com

Aug 10 '06 #65


Keith Thompson wrote On 08/10/06 16:58,:
jacob navia <ja***@jacob.re mcomp.frwrites:
[...]
>>Look, if you have
double a,b,c,d;
...
d = a*b+c;

you need more precision? You do:
qfloat a,b,c,d;
...
d = a*b+c;

You see?


Yes, jacob, we understand. Most of us know what operator overloading
is.

C doesn't have it. [...]
Please explain why binary `-' should not be considered
overloaded, given that its operands can be any of the twelve
basic arithmetic types (after promotion), or any additional
non-promotable types the implementation might provide, or any
object pointer type paired with a promoted integer type, or
any "commensura te" object pointer types.

Off-hand, I cannot think of any C operator that is *not*
overloaded. No, not even the comma operator.

--
Er*********@sun .com

Aug 10 '06 #66
Eric Sosman <Er*********@su n.comwrites:
Keith Thompson wrote On 08/10/06 16:58,:
>jacob navia <ja***@jacob.re mcomp.frwrites:
[...]
>>>Look, if you have
double a,b,c,d;
...
d = a*b+c;

you need more precision? You do:
qfloat a,b,c,d;
...
d = a*b+c;

You see?


Yes, jacob, we understand. Most of us know what operator overloading
is.

C doesn't have it. [...]

Please explain why binary `-' should not be considered
overloaded, given that its operands can be any of the twelve
basic arithmetic types (after promotion), or any additional
non-promotable types the implementation might provide, or any
object pointer type paired with a promoted integer type, or
any "commensura te" object pointer types.

Off-hand, I cannot think of any C operator that is *not*
overloaded. No, not even the comma operator.
I was insufficiently precise.

C doesn't have user-defined operator overloading. Like many
languages, it overloads the predefined operators on its own predefined
types.

If this "qfloat" were part of the C language, presumably there would
be language-defined overloaded operators for it, just as there are for
any other language-defined floating-point type. But it isn't; it's an
extension implemented by a certain compiler that jacob keeps trying to
advertise here.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 10 '06 #67

jacob navia wrote:
Skarmander wrote:
Ancient_Hacker wrote:
case (3): I need an integer that can do exact math with decimal prices
from 0.01 to 999,999,999,99. COBOL and PL/I can do this.
C cannot do this natively, so you'll need libraries. Luckily, C also
makes it possible to implement such libraries efficiently. This is a
good way of highlighting the differences in philosophy.

using a signed 64 bit integer type and working in cents
should be able to handle money quantities up to

92 233 720 368 547 758 US$ and 8 cents.

Enough to accomodate the now considerable total US
debt... :-)

Using native types is quite easy considering the progres in
hardware in the last years. Besides, accounting people that
need those extreme money amounts will not shudder to buy a
latest model PC for a few thousand dollars.

You can work in decimals of cents if you need sensible rounding.

The Cobol folks are requiring 36 decimal digit support in the lastest
Cobol standard (up from 18), which in a binary format would imply a 128
bit number. For example, until the Turkish Lira was revalued a few
years ago, the US GDP (to say nothing of the global GDP) represented in
TL would rather thoroughly overflowed a 64 bit int. Right now
Indonesian Rupiah's are right on the edge, and Columbian Pesos and
Venezuelan Bolivars are not far behind.

Even worse, C does not have extended range intermediates (required by
Cobol), so currency calculations will overflow far short of the nominal
limits implied by a 64 bit int. Just multiply the GDP of the U.S. in
cents by a 99.99 percentage, and you will overflow 64 bit
intermediates.

A bignum package remains the only way to do reliable currency
calculations in C.

Aug 10 '06 #68
ro***********@y ahoo.com wrote:
jacob navia wrote:
>>Skarmander wrote:
>>>Ancient_Hack er wrote:

case (3): I need an integer that can do exact math with decimal prices
from 0.01 to 999,999,999,99. COBOL and PL/I can do this.
C cannot do this natively, so you'll need libraries. Luckily, C also
makes it possible to implement such libraries efficiently. This is a
good way of highlighting the differences in philosophy.

using a signed 64 bit integer type and working in cents
should be able to handle money quantities up to

92 233 720 368 547 758 US$ and 8 cents.

Enough to accomodate the now considerable total US
debt... :-)

Using native types is quite easy considering the progres in
hardware in the last years. Besides, accounting people that
need those extreme money amounts will not shudder to buy a
latest model PC for a few thousand dollars.

You can work in decimals of cents if you need sensible rounding.

The Cobol folks are requiring 36 decimal digit support in the lastest
Cobol standard (up from 18), which in a binary format would imply a 128
bit number.
The lcc-win32 C compiler provides a 128 bit integer, and it
will be more and more common in the near future as 64 bit
machines become common. A 64 bit machine can do 128 bit
arithmetic quite fast. And that will really finish it since

pow(2,128) = 3.4028236692093 846346337460743 1768211456E38 exactly

If you imagine that the smallest bill of currency X is a 10 million
bill, and its thickness is 0.1mm, to a pile worth 3.4e38 of
that currency would measure around 3.59E11 Light years.

The radius of the known universe is only around 1.3E9 light years.

So, we are protected against inflation with 128 bits. :-)

Using lcc-win32:

* a=pow(2,128)
3.4028236692093 846346337460743 176821145600000 000000000000000 000000000000E38
* b=a/10000000 // smallest bill is 10 million
3.4028236692093 846346337460743 176821145600000 000000000000000 000000000000E31
* c=b/10000 // Thickness of 0.1mm, expressed in meters: divide by 10 000
3.4028236692093 846346337460743 176821145600000 000000000000000 000000000000E27
* e=300000*1000*3 65*24*3600 // meters in a light year
9.4608000000000 000000000000000 000000000000000 000000000000000 000000000000E15
* c/e // number of light years necessary for the pile of bills
3.5967610236020 047296568430516 633710833756130 559783527820057 500422797226E11
*

I hope I did not do a mistake somewhere :-)
Aug 10 '06 #69
jacob navia <ja***@jacob.re mcomp.frwrites:
[...]
The lcc-win32 C compiler provides a 128 bit integer,
[...]

With what syntax? Is it consistent with the C standard? Do you need
to include some non-standard header to use it?

If it's called "long long", or if it's implemented as an "extended
integer type" (types, actually, if you provide signed and unsigned
variants) as described in C99 6.2.5, and if it's used appropriately in
<stdint.h>, that's terrific. If it's implemented as a non-standard
extension, that's not nearly as interesting. (I think Mathematica
implements very long integers, and it's not C either.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 10 '06 #70

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1329
by: C. Barnes | last post by:
Summary: Sorts strings in a way that seems natural to humans. If the strings contain integers, then the integers are ordered numerically. For example, sorts into the order . Code:
3
1750
by: Derek Basch | last post by:
Hello All, I need to sort a list using an unnatural sequence. I have a list like so: foo = print foo.sort()
1
2429
by: Connelly Barnes | last post by:
Summary: Sorts strings in a way that seems natural to humans. If the strings contain integers, then the integers are ordered numerically. For example, sorts into the order . Code: #---------------------------------------------------------
4
2198
by: Andrew E | last post by:
Hi all I've written a python program that adds orders into our order routing simulation system. It works well, and has a syntax along these lines: ./neworder --instrument NOKIA --size 23 --price MARKET --repeats 20 etc However, I'd like to add a mode that will handle, say:
7
1883
by: tommaso.gastaldi | last post by:
This is a curious question. I'd like to know your opinion. I am attaching a drawing resize to a mouse wheel event. Frankly, to me was most natural that, if I mouse wheel UP, the shape gets bigger, while it gets smaller when mouse wheel DOWN. I just noticed that Photoshop does the opposite. Any though about the most natural way (or perhaps are both "natural") ?
1
1766
by: Hendri Adriaens | last post by:
Hi, I have an onmouseover script that displays a div with some comment. There are several links getting this onmouseover functionality, all with their own comment text. Now I want the div to have a max width, but if the natural width is smaller than the max width, it should get the natural width. commentObj is the div and commentObj.offsetWidth returns the natural width, as long as we didn't set it to anything else (it seems). As soon as...
1
2639
by: Steven Bird | last post by:
NLTK — the Natural Language Toolkit — is a suite of open source Python modules, data sets and tutorials supporting research and development in natural language processing. It comes with 50k lines of code, 300Mb of datasets, and a 360 page book which teaches both Python and Natural Language Processing. NLTK has been adopted in at least 40 university courses. NLTK is hosted on sourceforge, and is ranked in the top 200 projects. ...
3
4325
by: anon538 | last post by:
I am creating an application that has to check for natural numbers. By natural numbers, I mean positive whole numbers. //natural needs to equal 1,2,3, etc. if(minutes % 60 == natural) { minuteField.value = minutes; } How can I can I do this? Thanks.
10
8085
by: Ben | last post by:
Hi at all I am looking for the natural size of an image I found this function : while ((imgHeight = image.getHeight(this)) == -1 ) { // loop until image loaded
0
9797
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10762
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5807
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6005
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4622
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.