Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
[C++] Data Types

 
Post new topic   Reply to topic Printable version
 View previous topic  Digital Hockey Post :: Post Throw your windows around  View next topic  
Author Message
L.C.
Server Help Squatter


Age:33
Gender:Gender:Male
Joined: Jan 03 2003
Posts: 574
Location: Missouri, US
Offline

PostPosted: Sat Jun 07, 2008 2:52 pm    Post subject: [C++] Data Types Reply to topic Reply with quote

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?
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website AIM Address Yahoo Messenger MSN Messenger
CypherJF
I gargle nitroglycerin


Gender:Gender:Male
Joined: Aug 14 2003
Posts: 2582
Location: USA
Offline

PostPosted: Sat Jun 07, 2008 3:01 pm    Post subject: Reply to topic Reply with quote

Yes, memorize it. Or, understand computer architecture and it's just inherit.
_________________
Performance is often the art of cheating carefully. - James Gosling
Back to top
View users profile Send private message Add User to Ignore List
L.C.
Server Help Squatter


Age:33
Gender:Gender:Male
Joined: Jan 03 2003
Posts: 574
Location: Missouri, US
Offline

PostPosted: Sat Jun 07, 2008 3:18 pm    Post subject: Reply to topic Reply with quote

Quote:
Or, understand computer architecture and it's just inherit.
What architecture? Erm, please be more elaborate? ;_;
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website AIM Address Yahoo Messenger MSN Messenger
Bak
?ls -s
0 in


Age:24
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Sat Jun 07, 2008 3:25 pm    Post subject: Reply to topic Reply with quote

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;

_________________
SubSpace Discretion: A Third Generation SubSpace Client
Back to top
View users profile Send private message Add User to Ignore List AIM Address
L.C.
Server Help Squatter


Age:33
Gender:Gender:Male
Joined: Jan 03 2003
Posts: 574
Location: Missouri, US
Offline

PostPosted: Sat Jun 07, 2008 4:29 pm    Post subject: Reply to topic Reply with quote

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'?
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website AIM Address Yahoo Messenger MSN Messenger
Dr Brain
Flip-flopping like a wind surfer


Age:38
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Sat Jun 07, 2008 4:51 pm    Post subject: Reply to topic Reply with quote

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).
_________________
Hyperspace Owner

Smong> so long as 99% deaths feel lame it will always be hyperspace to me
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Samapico
No, these DO NOT look like penises, ok?


Joined: May 08 2003
Posts: 1252
Offline

PostPosted: Sat Jun 07, 2008 5:24 pm    Post subject: Reply to topic Reply with quote

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.
_________________
(Insert a bunch of dead links here)
Back to top
View users profile Send private message Add User to Ignore List
L.C.
Server Help Squatter


Age:33
Gender:Gender:Male
Joined: Jan 03 2003
Posts: 574
Location: Missouri, US
Offline

PostPosted: Sat Jun 07, 2008 6:51 pm    Post subject: Reply to topic Reply with quote


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?




2-imgvars1.gif - 4.85 KB
File downloaded or viewed 33 time(s)
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website AIM Address Yahoo Messenger MSN Messenger
k0zy
Server Help Squatter


Gender:Gender:Male
Joined: Jan 11 2003
Posts: 571
Location: Germany
Offline

PostPosted: Sat Jun 07, 2008 7:39 pm    Post subject: Reply to topic Reply with quote

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.
_________________
It's a shark! Oh my god! Unbelievable!
Back to top
View users profile Send private message Add User to Ignore List
Samapico
No, these DO NOT look like penises, ok?


Joined: May 08 2003
Posts: 1252
Offline

PostPosted: Sun Jun 08, 2008 12:08 pm    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List
Bak
?ls -s
0 in


Age:24
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Sun Jun 08, 2008 2:13 pm    Post subject: Reply to topic Reply with quote

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
Back to top
View users profile Send private message Add User to Ignore List AIM Address
k0zy
Server Help Squatter


Gender:Gender:Male
Joined: Jan 11 2003
Posts: 571
Location: Germany
Offline

PostPosted: Sun Jun 08, 2008 4:01 pm    Post subject: Reply to topic Reply with quote

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);
Back to top
View users profile Send private message Add User to Ignore List
Bak
?ls -s
0 in


Age:24
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Sun Jun 08, 2008 8:48 pm    Post subject: Reply to topic Reply with quote

but then he's just memorizing functions icon_sad.gif
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Non-Subspace Related Coding All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 661 page(s) served in previous 5 minutes.

phpBB Created this page in 0.419739 seconds : 40 queries executed (90.2%): GZIP compression disabled