Server Help

Non-Subspace Related Coding - [C++] Data Types

L.C. - Sat Jun 07, 2008 2:52 pm
Post subject: [C++] Data Types
http://www.cplusplus.com/doc/tutorial/variables.html

Under "Fundamental data types" the table. How do you learn that? Do you memorize it or what?
CypherJF - Sat Jun 07, 2008 3:01 pm
Post subject:
Yes, memorize it. Or, understand computer architecture and it's just inherit.
L.C. - Sat Jun 07, 2008 3:18 pm
Post subject:
Quote:
Or, understand computer architecture and it's just inherit.
What architecture? Erm, please be more elaborate? ;_;
Bak - Sat Jun 07, 2008 3:25 pm
Post subject:
variables can either be signed or unsigned.... signed means they can be negative.

computers work in binary... and usually along byte boundaries... so a one byte variable has 8 bits, (each bit is either a 1 or a 0, 2 possbilities)

how many possible numbers can you represent with eight bits? 2^8 which is 256... an unsigned one byte variable (unsigned char) has a range of 0 to 255. the signed version (char) has its range shifted over so it can represent -128 to 127 (still 256 possibilities). Two byte (16 bit) numbers can represent 2^16 possibilities or 65536... so 0 to 65535 for unsigned or -32768 to 32767 for signed.

four byte integers (32 bits) have 2^32 possibilities which is over 4 billion... the signed version goes from about -2 billion to 2 billion.

floating point numbers are like real numbers (don't have to be integers)... they're sort of complicated but it's basically scientific notation in binary. With decimal scientific notation you have <number> * 10^exponent. The floating point numbers use <number> * 2^exponent. In this way they can represent both very large and very small numbers (exponent can be negative), which works well in practice. However, as you can imagine <number> and <exponent> have to be integers otherwise you have a chicken and egg problem of how you represent those. So that means even though you might be able to represent 2^100 exactly with them, you can't do 2^100 + 0.1 without rounding and losing precision.

Anyways if you don't like the names themselves you can rename them. In mervbot they are renamed to like Uint32(unsigned 32 bit integer) and Uint16 (unsigned 16 bit integer). If you use ASSS you'll see things like i8 (signed 8 bit integer) or u32 (unsigned 32 bit integer). The way to rename them is put a typedef outside of main (if you look around the mervbot or asss source code you'll find these):

Code: Show/Hide
typedef int i32;

L.C. - Sat Jun 07, 2008 4:29 pm
Post subject:
What is the different between int and long int? What's so special about long?

For float and double, how do you get 3.4e +/- 38 and 1.7e +/- 308 ranges? For float, 4 bytes is 32 bits, and for double is 64 bits. Using the x*2^y formula you mention, how do you get those ranges?

What are some examples of uses for each data type? Like what is or can each one be used for? How is it used in the 'real world'?
Dr Brain - Sat Jun 07, 2008 4:51 pm
Post subject:
a long int (also known as a "long") changes from machine to machine. On x86 (32 bit intels) it's the same as an int, or 4 bytes long. On the newer 64 bit operating systems (it depends on the operating system rather than the chip in most cases) it's 8 bytes long.

Look at http://en.wikipedia.org/wiki/IEEE-754 for a deeper explanation of how the bits are arranged for floating point numbers.

Examples of usage? I use ints for basically everything in Hyperspace. I'll occasionally use a double when I need fractional multiplication (which isn't often). No one ever uses shorts or floats these days except for very specific cases (huge arrays are the only example I can think of).
Samapico - Sat Jun 07, 2008 5:24 pm
Post subject:
Don,t really have to 'memorize' them... their names are usually pretty self-explanatory. integer, floating point, etc...
Only in rare cases you need to care about the number of bytes then take up, but you learn it with usage. Plus, most of these can change depending on the machine, like Brain said.

But in most cases, you only need numbers and you'll find yourself using ints... and chars when you need text.

If you do the tutorials on that site, I'm pretty sure you'll find good uses for the different types.
L.C. - Sat Jun 07, 2008 6:51 pm
Post subject:

What does [20] in char string [20] mean? Does that define the maximum number of characters in 'string'?

What is the difference between int a (0); and int a = 0;? Why are there two different ways of doing 'it' for the same thing?

EDIT :: http://www.cplusplus.com/reference/string/string/
What are those and how do I use them? For example, max_size.

EDIT2 :: http://www.cplusplus.com/doc/tutorial/constants.html
At the bottom of the page: what are constants, what are they used for and how? Why would you want to use a constant?
k0zy - Sat Jun 07, 2008 7:39 pm
Post subject:
It's 1:31am here... so forgive me any errors.

The 20 really means the max size of the string.
A string is nothing else than an array of characters, the array is 20 chars long.
That means you can store 19 characters there, because the last one has to be 0 ('\0').

I guess a(0) calls the constructor of an int class... not sure there.

The functions you asked for in edit1 are member funcions of the string class.
Create a new string
with
string s("")
and use it with s.max_size
or
string s = new string("")
s->max_size
You might want to read about Object Oriented Programming if you don't know.

Constants are variables that don't vary... they are constant.
You use them so that the compiler can optimize some stuff, because there value will always be the same.
Some people use them as an alternative to #define.
Samapico - Sun Jun 08, 2008 12:08 pm
Post subject:
Well, 'constants' as described in that page are simply values entered in the code:

int a;

a = 2+8;

//2 and 8 are constants

If you scroll in that page, you'll see something about 'defined constants', which is what people usually use.
For example, instead of typing 3.14159 everywhere in your code, you can type:

#define PI 3.14159

at the beginning, and then wherever you type 'PI', it will be considered as 3.14159 by the compiler.

Another common usage of constants is if there is a value that you use at different places in your code, for example it could be the maximum allowed number of items at some place. Instead of typing '10' everywhere in your code, and then if you want to modify that value, you have to go change all the places you wrote 10. Instead, you define yourself a constant for it, and you'll have only one place to change.


Quote:
What does [20] in char string [20] mean? Does that define the maximum number of characters in 'string'?
Check the page about arrays.
Basically it's an array of 20 'char' type items. So yeah, if you try to put more than 20 characters in there, you'll be writing outside of the memory assigned to it, most likely causing a crash.
Bak - Sun Jun 08, 2008 2:13 pm
Post subject:
array is basically a bunch of variables... if you wanted to have a word you could do:

Code: Show/Hide

char letter0 = 'a';
char letter1 = 'p';
char letter2 = 'p';
char letter3 = 'l';
char letter4 = 'e';
char letter5 = 0; // 0 or '\0' means its the end


or

Code: Show/Hide

char letters[6] = "apple";


and then you can extract letters with letters[0], letters[1], ect.

it's also nicer because you can loop over the entire array with a variable... so say you wanted to clear the letters (set them all to 0), you'd have to do

Code: Show/Hide

letter0 = 0;
letter1 = 0;
letter2 = 0;
letter3 = 0;
letter4 = 0;
letter5 = 0;


or with arrays you could do it a in a loop

Code: Show/Hide

for (int i = 0; i < 6; ++i)
   letters[i] = 0;


For floating point ranges, you can read the wikipedia link brain posted... there's a chart about half way down that lists the "Largest normalized number". Basically for 32 bit floating point numbers it's about 2^128 which is the 3.4 * 10^38 and for 64 bit floating point numbers it's about 2^1024 which is the 1.7 * 10^308 from your site.

cplusplus.com is wrong, however, in that the range "1.7e +/- 308" means 1.7 * 10^(+/- 308) when what they really mean is +/- 1.7 * 10^308 (otherwise floating point numbers couldn't represent negative numbers, or even zero!)

my first introductory c++ book made the same mistake, someone should send them an e-mail icon_smile.gif
k0zy - Sun Jun 08, 2008 4:01 pm
Post subject:
To zero an array you should use memset.
http://www.cplusplus.com/reference/clibrary/cstring/memset.html

Code: Show/Hide
char string[6];
memset(string, 0, 6);

Bak - Sun Jun 08, 2008 8:48 pm
Post subject:
but then he's just memorizing functions icon_sad.gif
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group