![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |
| |||
| float/double/long double are floating point "real" numbers. C/C++ uses double (aka. double-precision) by default for representing real values. so you should prefer double when holding real values. float is typically smaller than double, with lesser precision. long double is known as extended precision, which is typically greater than double. Though you'll inevitably come across compilers where one or more of these types are the same size. The only guarantee you have is that sizeof(float) <= sizeof(double) and sizeof(double) <= long double Generally, float isn't used very often, it may be useful if you're economising on space, and don't need the precision (Such as sending bits over a network stream). long double is useful if you need the extra precision where its available. wchar_t is typically used for holding unicode values (useful when support for foreign language character sets is needed) as for long/short - These are shorthand for long int and short int. As is the case for the real types above, you've only got one guarantee for each.. sizeof(char) == 1 sizeof(char) <= sizeof(short) sizeof(short) <= sizeof(int) sizeof(int) <= sizeof(long int) in general, use int. which is the default integral type in C/C++. If you need a bit more range than int can provide, then use long, be aware that long may be the same size as int. (if that's not enough, use a "Big Number" library) If you're economising on space, you might feel the need to use short, although in practice, its not used all that commonly. Again, being aware that short can be the same size as int. If you're not sure what range or precision you need, start with the default ones - char for characters, int for integrals and double for real numbers. If it transpires later that one of the others are needed, then you can change it. Although swapping char for wchar_t is a little trickier, it has seperate string and I/O facilities to plain char. generally these are prefixed with 'w' - eg, wcout, wcin, wstring. Last edited by Bench : 09-03-2007 at 04:18 AM. |
| The Following User Says Thank You to Bench For This Useful Post: | ||
Lee (09-03-2007) | ||
| ||||
| Wow, i wasn't expecting that answer, that's awesome and loads of help, couple of small thing though, i always use int for numbers, Does that mean i should really think about using double? If so when should i use int over double or vice versa? What do you mean by real values? Thanks, Lee. |
| |||
| by "real" and "floating point" numbers, I mean non-integers that represent data with a fractional part. floating point types can't be used for anything which requires an integer - Things like array indexes, enums, pointer-offsets, character representation, etc all need an integral value You'll probably find that double will be useful in real-world calculations, such as trigonometry, currency, scientific values, etc. Last edited by Bench : 09-03-2007 at 06:15 AM. |
| The Following User Says Thank You to Bench For This Useful Post: | ||
Lee (09-03-2007) | ||
| ||||
| Thanks again, thats cleared everything about them i can think of for now ![]() |
| ||||
| I personally still don't get of why do you ask of "When to use one variables over another?" Because I'd say that their functionalities are different. Well, there are some of them those are similar, though, they have different sizes. So, it's either on how do you want to use the variable for, OR if you need more size for whatever information to be stored in a variable ![]() |
| ||||
| I'm afraid I take a far more simplistic approach. In whatever language I'm working with, I go to the documentation and print out the minimum and maximum values. Then, when choosing a type, I choose the type that uses the smallest number of bytes and covers the entire range of possible values. |
| |||
| Quote:
boolean = _Bool/bool byte = char char = wchar_t short = short int int = long int (note that on my machine, a C/C++ int = a C/C++ long int, but a C/C++ int = a C/C++ short int on some other machines) long = long long (C99 only, not valid with C++98) As Bench wrote, C++ only guarantees that a char is at least 1 byte in size and each integral data type is at least the same size. The same is true for the real/floating-point types. Honestly, a char in C++ could be the same size as a long int, meaning that your long int would only be able to hold -128 to 127 (signed) or 0 to 255 (unsigned). Also, a char is often an int with a restricted range. As such, int is often preferred because using char causes conversion to take place at runtime, which can affect performance somewhat, depending on the efficiency of the instructions used for the conversion and the machine's abilities itself. Quote:
__________________ "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." -- Bjarne Stroustrup, creator of what is now known as C++ For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that. |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (09-04-2007) | ||
![]() |
| Thread Tools | |
| Display Modes | |
| |