User Tools

Site Tools


ch03-places-to-put-things

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
ch03-places-to-put-things [2017/08/24 01:33]
mithat [Data types]
ch03-places-to-put-things [2017/08/24 02:03] (current)
mithat [Potential pitfalls]
Line 221: Line 221:
 === int === === int ===
  
-The ''​%%int%%''​ type is used to represent integer values---any number that has no fractional part (..., -3, -2, -1, 0, 1, 2, ...). In mathematics,​ integer values have no upper or lower limit; in Processing they do. An int cannot be smaller than -2,​147,​483,​648 nor be larger than 2,​147,​483,​647. ​An int occupies 32 bits in memory. Example:+The ''​%%int%%''​ type is used to represent integer values---any number that has no fractional part (..., -3, -2, -1, 0, 1, 2, ...). In mathematics,​ integer values have no upper or lower limit; in Processing they do. An int cannot be smaller than -2,​147,​483,​648 nor be larger than 2,​147,​483,​647. ​If you're curious about such things, an int occupies 32 bits in memory. 
 + 
 +Example:
  
 <code java> <code java>
Line 227: Line 229:
 numBeers = 99; numBeers = 99;
 </​code>​ </​code>​
 +
 === long === === long ===
  
-A ''​%%long%%''​ is identical to an ''​%%int%%''​ except that the limits are wider: -9,​223,​372,​036,​854,​775,​808 to 9,​223,​372,​036,​854,​775,​807. So why would you want to use ''​%%int%%''​ when you've also got ''​%%long%%''​ available? Because ''​%%long%%''​ variables occupy 64 bits of memory, twice as much as an ''​%%int%%''​. **TODO verify:** Also, on most platforms calculations made using ''​%%int%%''​ types will be faster than the same calculations made using ''​%%long%%''​. The ''​%%long%%''​ type is available for your use in Processing if you need it, but it is not used by any of the functions ​available in Processing. Example:+A ''​%%long%%''​ is identical to an ''​%%int%%''​ except that the limits are wider: -9,​223,​372,​036,​854,​775,​808 to 9,​223,​372,​036,​854,​775,​807. So why would you want to use ''​%%int%%''​ when you've also got ''​%%long%%''​ available? Because ''​%%long%%''​ variables occupy 64 bits of memory, twice as much as an ''​%%int%%''​. The ''​%%long%%''​ type is available for your use in Processing if you need it, but it is not used by any of the native Processing ​functions. 
 + 
 +Example:
  
 <code java> <code java>
Line 235: Line 240:
 milesLeftToPluto = 3100000000L;​ milesLeftToPluto = 3100000000L;​
 </​code>​ </​code>​
 +
 When you want to use a ''​%%long%%''​ literal constant, you must add the letter '​L'​ (upper or lower case) after the value. When you want to use a ''​%%long%%''​ literal constant, you must add the letter '​L'​ (upper or lower case) after the value.
  
Line 245: Line 251:
 catLivesLeft = 9; catLivesLeft = 9;
 </​code>​ </​code>​
 +
 === float === === float ===
  
Line 253: Line 260:
 zeroToSixtyTime = 7.2; zeroToSixtyTime = 7.2;
 </​code>​ </​code>​
-Floating point numbers and floating point calculations in computing are full of subtle issues that don't show up in pure math. These issues are due to the fact that in pure math floating point numbers have infinite precision, but in computing the precision is limited. 
  
 Now is a good time to point out that there is a difference between the literal constant ''​%%3.0%%''​ and the literal constant ''​%%3%%''​. The latter specifies a ''​%%float%%''​ literal constant while the latter an ''​%%int%%''​ literal constant. They are different things. Now is a good time to point out that there is a difference between the literal constant ''​%%3.0%%''​ and the literal constant ''​%%3%%''​. The latter specifies a ''​%%float%%''​ literal constant while the latter an ''​%%int%%''​ literal constant. They are different things.
Line 264: Line 270:
 integerThree = 3; integerThree = 3;
 </​code>​ </​code>​
 +
 === double === === double ===
  
 The ''​%%double%%''​ type is identical to the ''​%%float%%''​ except that it has double the precision---and double the memory requirements. The double type is available for your use in Processing if you need it, but it isn't used otherwise. The ''​%%double%%''​ type is identical to the ''​%%float%%''​ except that it has double the precision---and double the memory requirements. The double type is available for your use in Processing if you need it, but it isn't used otherwise.
 +
 +<WRAP center round box 80%>
 +=== Geek break: Floating point numbers ===
 +Floating point numbers calculations that use them in computing are full of subtle issues that don't show up in pure math. As an example, try running this program:
 +
 +<file java floatingpoint.pde>​
 +void setup () { 
 +  double a;
 +  double b;
 +
 +  a = 0.1;
 +  b = 3.0;
 +  ​
 +  println(a * b);
 +}
 +</​file>​
 +
 +The laws of mathematics say the program should print 0.3, but the actual result is different. This odd behavior is caused by two things: (1) in pure math floating point numbers have infinite precision, but in computing floating point precision is limited, and (2) Processing'​s numbers are coded using a base two system (i.e., //binary//) whereas the numbers we humans use are coded in base ten (i.e., //​decimal//​).
 +
 +So, the takeaway from this is that in computing, you should consider floating point as //​approximations//​ to what they are in the math world. For the kinds of things you're likely to do with them for in Processing, this aspect of floating point numbers is unlikely to present a problem. However, there are situations where it does.
 +</​WRAP>​
  
 === char === === char ===
  
-The ''​%%char%%''​ type is used to represent a single alphanumeric or symbolic character---what you might think of as a single letter, number, question mark, etc. A ''​%%char%%''​ occupies 16 bits and uses Unicode encoding--a system that translates characters into numeric codes. Character literal constants are created with single quotes. Examples:+The ''​%%char%%''​ type is used to represent a single alphanumeric or symbolic character---what you might think of as a single letter, number, question mark, etc. A ''​%%char%%''​ occupies 16 bits and uses Unicode encoding, which is a system that translates characters into numeric codes. Character literal constants are created with single quotes. Examples:
  
 <code java> <code java>
Line 290: Line 318:
 integerThree = 3; integerThree = 3;
 </​code>​ </​code>​
 +
 === boolean === === boolean ===
  
Line 298: Line 327:
 isDone = false; isDone = false;
 </​code>​ </​code>​
 +
 === color === === color ===
  
-The ''​%%color%%''​ type is used to represent colors. Opaque colors can be specified using Web color format (e.g., #E300CC). You can also specify a color with an alpha channel (for transparency) in hexadecimal notation (e.g., 0x99E300CC). Examples:+The ''​%%color%%''​ type is used to represent colors. Opaque colors can be specified using Web color format (e.g., ​''​#E300CC''​). You can also specify a color with an alpha channel (for transparency) in hexadecimal notation (e.g., ​''​0x99E300CC''​). Examples:
  
 <code java> <code java>
Line 319: Line 349:
 Processing is an example of a **statically typed** language. In statically typed languages, the type of data that can be stored in a variable cannot change once the variable has been created. The most common way to indicate the data type that can be stored in a variable is through declaration. Other examples of statically types languages are C, C++, C#, and Java. Processing is an example of a **statically typed** language. In statically typed languages, the type of data that can be stored in a variable cannot change once the variable has been created. The most common way to indicate the data type that can be stored in a variable is through declaration. Other examples of statically types languages are C, C++, C#, and Java.
  
-Contrasting with this are **dynamically typed** languages. In dynamically typed languages the type of data that can be stored in the variable is free to change at any time. It can store a number in one statement and a string of characters ​in the next. Since the type of data stored in a variable can change, there'​s no need to explicitly state what type of data you plan to shove into the box. Because of this, many dynamically typed languages will automatically create a variable for you the first time you try to store something in it, and they will automatically change the type associated with the variable as needed. Many programmers are attracted to the simpler syntax that dynamic typing permits. Popular dynamically typed languages include ​Javascript ​and Python.+Contrasting with this are **dynamically typed** languages. In dynamically typed languages the type of data that can be stored in the variable is free to change at any time. It can store a number in one statement and a character ​in the next. Since the type of data stored in a variable can change, there'​s no need to explicitly state what type of data you plan to shove into the box. Because of this, many dynamically typed languages will automatically create a variable for you the first time you try to store something in it, and they will automatically change the type associated with the variable as needed. Many programmers are attracted to the simpler syntax that dynamic typing permits. Popular dynamically typed languages include ​JavaScript ​and Python.
  
-So, dynamically typed languages seem pretty cool. Why don't all languages work that way? One disadvantage of dynamically typed languages is that all the automation associated with them consumes computing resources while the program is running. That means slower execution and/or larger memory requirements. Another, and possibly more important, disadvantage ​is that dynamic typing can be dangerous. Static typing provides an extra safety net by ensuring that new variables are created only when you really wantrather than because you missspeelled a variable that you already created. They also make sure that only code that is designed for a specific type operates on that type. For example, it might make perfect sense to divide two numbers, but it doesn'​t really make sense to divide one string by another string.+So, dynamically typed languages seem pretty cool. Why don't all languages work that way? One disadvantage of dynamically typed languages is that all the automation associated with them consumes computing resources while the program is running. That means slower execution and/or larger memory requirements. Another ​reason ​is that dynamic typing can be dangerous. Static typing provides an extra safety net by ensuring that new variables are created only when you really want rather than because you missspeelled a variable that you already created. They also make sure that only code that is designed for a specific type operates on that type. For example, it might make perfect sense to divide two numbers, but it doesn'​t really make sense to divide one string by another string.
  
 These might seem like small issues when you are just starting programming,​ but you quickly learn to appreciate them when your programs become longer and more complex. These might seem like small issues when you are just starting programming,​ but you quickly learn to appreciate them when your programs become longer and more complex.
Line 329: Line 359:
 ===== Syntactic sugar ===== ===== Syntactic sugar =====
  
-**Syntactic sugar** is any syntax rule that doesn'​t introduce new power or concepts into the language but rather ​makes using existing concepts easier, more compact, or more semantically obvious. Two examples of syntactic sugar in Processing are variable initialization and multiple declarations.+**Syntactic sugar** is any syntax rule that doesn'​t introduce new power or concepts into the language but instead just makes using existing concepts easier, more compact, or more semantically obvious. Two examples of syntactic sugar in Processing are variable initialization and multiple declarations.
  
 ==== Variable initialization ==== ==== Variable initialization ====
Line 338: Line 368:
 int foo = 66; int foo = 66;
 </​code>​ </​code>​
-The above statement declares an integer variable named ''​%%foo%%''​ and gives it an initial value of 66 in one statement. You can initialize any of Processing'​s primitive types this way---as long as the value on the right of the equals sign is compatible with the variable'​s type.+The above statement declares an integer variable named ''​%%foo%%''​ and gives it an initial value of 66 in one statement. You can initialize any of Processing'​s primitive types this wayas long as the value on the right of the equals sign is compatible with the variable'​s type.
  
-If you try to access the value of a variable without initializing it or otherwise giving it a value, the Processing IDE will complain at you. This is because it is widely considered bad programming practice to rely on the default values given to variables by a language. The reason for this is that many languages do not specify //any// default values for variables---at least for some categories. In other words, the "​default"​ value given to a variable you declare might be a random and/or arbitrary value.((The language on which Processing is built, Java, [[http://​docs.oracle.com/​javase/​tutorial/​java/​nutsandbolts/​datatypes.html|//does//]] specify default values for certain kinds of variables. However, the kinds of variables we have been using up to now and will use for most of this text do not fit into that category.+If you try to access the value of a variable without initializing it or otherwise giving it a value, the Processing IDE will complain at you. This is because it is widely considered bad programming practice to rely on the default values given to variables by a language. The reason for this is that many languages do not specify //any// default values for variables---at least for some categories. In other words, the "​default"​ value given to a variable you declare might be a random and/or arbitrary value.((The language on which Processing is built, Java, //[[http://​docs.oracle.com/​javase/​tutorial/​java/​nutsandbolts/​datatypes.html|does]]// specify default values for certain kinds of variables. However, the kinds of variables we have been using up to now and will use for most of this text do not fit into that category.
 )) ))
  
Line 350: Line 380:
 char firstInitial,​ secondInitial;​ char firstInitial,​ secondInitial;​
 </​code>​ </​code>​
-declare two variables---''​%%firstInitial%%''​ and ''​%%secondInitial%%''​---which are both character types. Notice the comma between the two variable names.+declare two variables''​%%firstInitial%%''​ and ''​%%secondInitial%%''​which are both character types. Notice the comma between the two variable names.
  
 You can even combine variable initialization and multiple declaration in the same statement: You can even combine variable initialization and multiple declaration in the same statement:
Line 359: Line 389:
 ==== Potential pitfalls ==== ==== Potential pitfalls ====
  
-Syntactic sugar can be a tricky thing. On the one hand, syntactic sugar makes it easier to write your programs---it increases **writability**. When done right, it also makes programs easier to read--it increases **readability**. However, when a language has too much syntactic sugar, then the number of ways to express the same idea increases to such a level that it ends up making it //harder// to read--because the reader has to know all the intricacies of all the different possible ways of doing things.+Syntactic sugar can be a tricky thing. On the one hand, syntactic sugar makes it easier to write your programs---it increases **writability**. When done right, it also makes programs easier to read---it increases **readability**. However, when a language has too much syntactic sugar, then the number of ways to express the same idea increases to such a level that it ends up making it //harder// to read because the reader has to know all the intricacies of all the different possible ways of doing things.
  
  
ch03-places-to-put-things.1503538390.txt.gz · Last modified: 2017/08/24 01:33 by mithat