Math in C

Numerical Variables int float double long long

Let’s add some ints! // declare x int x; // initialize x x = 2; // declare and initialize y int y = x + 1;

Division int main(void) { // declare and initialize answer float answer = 1 / 10; // print answer to two decimal places printf("%.2f\n", answer); }

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); }

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); }

Operator Precedence What is x? 1. int x = 2 * 10 + 10 / 2 + 2; 2. int x = 2 * (10 + 10) / 2 + 2; 3. int x = 2 * (10 + 10) / (2 + 2);

Modulo

1. 55 % 10 2. 3 % 5 3. 8 % 8 4. 16 % 15

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); }

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); }

Math in C

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);. } Page 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);. }

101KB Sizes 2 Downloads 167 Views

Recommend Documents

PDF Math in Focus: Singapore Math
... at home Math Mammoth Light Blue Series Math Mammoth Light Blue Series ... 7 000 additional formats No advertisements Unlimited cloud backup of all your ...

math-g1-m5-topic-c-lesson-7.pdf
Page 1 of 11. NYS COMMON CORE MATHEMATICS CURRICULUM Lesson 7 1. Lesson 7: Name and count shapes as parts of a whole, recognizing relative ...

Cryptography in C and C++
Library of Congress Cataloging-in-Publication Data ..... Libraries in 80x86 assembler (see Chapter 19) in directories flint/lib ...... Call : ml /Cx /c /Gd (filename). 8 ...

Women in Science and Math
Speakers: Dr. Lynne D. Richardson, MD, FACEP. Vice-Chair, Dept. of Emergency Medicine. Mount Sinai School of Medicine. Dr. Doris Withers. Professor of ...

Coroutines in C++17 - GitHub
http://isocpp.org/files/papers/N4403.pdf ... to a function call overhead) ... coroutines call. Allocate frame, pass parameters. Allocate frame, pass parameters return.

O'reilly - Programming Embedded Systems in C and C++ (1999).pdf ...
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. O'reilly ...

Math Learning Disabilities - The Math Wiki
operational sign, at borrowing or carrying appropriately, and at sequencing the steps ... answered problems each on an individual card; they alternate in their ...