Presentation is loading. Please wait.

Presentation is loading. Please wait.

Math in C The math blocks you've used in Scratch can all be recreated in C!

Similar presentations


Presentation on theme: "Math in C The math blocks you've used in Scratch can all be recreated in C!"— Presentation transcript:

1 Math in C The math blocks you've used in Scratch can all be recreated in C!

2 Numerical Variables int float double long long
Numerical data in C is classified into several different types. From a variable’s type, you’ll be able to tell what kind of data it can store, how much data it can store, and which operations can be performed on this data. An int is a whole number. The appliance uses 32 bits to store an int. A float is a number with a decimal point. The appliance uses 32 bits to store a float. A double is a number with a decimal point, but with more space for precision after the decimal point. The appliance uses 64 bits to store a double. A long long is a whole number that is twice as big in memory as an int. The appliance uses 64 bits to store a long long.

3 Let’s add some ints! // declare x int x; // initialize x x = 2;
// declare and initialize y int y = x + 1; Here, we sum two variables of type int. Let's think very carefully about what's happening as these three lines of code execute: The first line declares an int named x. More precisely, it asks the operating system for enough memory to store an int and gives that chunk of memory the name x. The second line initializes x by assigning it a value of 2. Remember that in C, a single equal sign is called an assignment operator. Whatever's to the right of the equal sign will be stored in the variable to the left of the equal sign. The third line declares and initializes an int named y in a single line of code.

4 Division int main(void) { // declare and initialize answer
float answer = 1 / 10; // print answer to two decimal places printf("%.2f\n", answer); } Let’s try some division next, and store the result of our division in a float because we know it will be a decimal value. %.2f tells the OS to print a floating-point number, but only to two decimal places. What will print? 0.00 Why? 1 and 10 are ints. In C, an int divided by an int is an int and any decimal places will be truncated off.

5 Fixed version: Typecasting
int main(void) { // declare and initialize answer float answer = (float) 1 / (float) 10; // print answer to two decimal places printf("%.2f\n", answer); } We can fix this! We can use this syntax (desired data type within parentheses) to cast the ints 1 and 10 to floats. This is called typecasting. A float divided by a float is a float, which preserves decimal places! What will print this time? 0.10

6 Another way int main(void) { // declare and initialize answer
float answer = 1.0 / 10.0; // print answer to two decimal places printf("%.2f\n", answer); } Another way to accomplish the same thing is to replace the ints 1 and 10 with floats 1.0 and 10.0. This method also results in the printing of 0.10.

7 Operator Precedence What is x? 1. int x = 2 * 10 + 10 / 2 + 2;
Remember "order of operations" from elementary school? The same concept applies in programming and is called operator precedence. 1. 27 2. 22 3. 10

8 Modulo 1. 55 % 10 2. 3 % 5 3. 8 % 8 4. 16 % 15 The modulo operator gives you the remainder of the division of the first number by the second. 1. 5 2. 3 3. 0 4. 1

9 What will print? int main(void) { // declare and initialize x, y, z
int x = 1; int y = 2; int z = (x + y) * y % y + y; // print z printf("%i\n", z); } What will be printed out? 2

10 Floating Point Imprecision
int main(void) { // initialize x and y float answer = 1.0 / 10.0; // print answer to two decimal places printf("%.20f\n", answer); } Why is it that will print rather than simply ? Some fractions like 0.1 can't be represented precisely using a finite number of binary bits. This is known as floating point imprecision and can cause significant rounding errors if you're not careful!


Download ppt "Math in C The math blocks you've used in Scratch can all be recreated in C!"

Similar presentations


Ads by Google