On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Similar documents
Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Operators. Lecture 3 COP 3014 Spring January 16, 2018

LECTURE 3 C++ Basics Part 2

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

C++ Programming: From Problem Analysis to Program Design, Third Edition

Lecture 3 Tao Wang 1

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

CIS133J. Working with Numbers in Java

The C++ Language. Arizona State University 1

Section we will not cover section 2.11 feel free to read it on your own

Computer System and programming in C

Engineering Problem Solving with C++, Etter/Ingber

Divide: Paper & Pencil

These are reserved words of the C language. For example int, float, if, else, for, while etc.

LECTURE 02 INTRODUCTION TO C++

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Programming in C++ 6. Floating point data types

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Objectives. In this chapter, you will:

Chapter 2. Outline. Simple C++ Programs

Programming in C++ 5. Integral data types

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

CHAPTER 3 BASIC INSTRUCTION OF C++

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

CS Kangmei Yang. Page 1

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Lesson 3: Arithmetic & Casting. Pic 10A Ricardo Salazar

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Chapter 7 Arithmetic

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CHAPTER 3 Expressions, Functions, Output

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Unit 3. Constants and Expressions

Chapter 2: Overview of C++

CS313D: ADVANCED PROGRAMMING LANGUAGE

Fixed-Point Math and Other Optimizations

Creating a C++ Program

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

Chapter 3. Numeric Types, Expressions, and Output

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Outline. Data and Operations. Data Types. Integral Types

C: How to Program. Week /Mar/05

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Number Systems and Computer Arithmetic

Introduction to Programming

BASIC ELEMENTS OF A COMPUTER PROGRAM

Module 2: Computer Arithmetic

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Unit 3. Operators. School of Science and Technology INTRODUCTION

Chapter 3: Operators, Expressions and Type Conversion

Chapter 2 - Introduction to C Programming

Add Subtract Multiply Divide

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Lecture 13: (Integer Multiplication and Division) FLOATING POINT NUMBERS

Reserved Words and Identifiers

Exponential Numbers ID1050 Quantitative & Qualitative Reasoning

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

CSCE 110 PROGRAMMING FUNDAMENTALS

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Computer Programming CS F111

Chapter 3 Structure of a C Program

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Our Strategy for Learning Fortran 90

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Information Science 1

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

Floating Point Arithmetic

COMP Primitive and Class Types. Yi Hong May 14, 2015

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

ANSI C Programming Simple Programs

Programming and Data Structures

The type of all data used in a C (or C++) program must be specified

Chapter 4. Operations on Data

CIS 110: Introduction to Computer Programming

Number Systems. Both numbers are positive

Expressions, Input, Output and Data Type Conversions

Chapter 3. Fundamental Data Types

Signed umbers. Sign/Magnitude otation

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Transcription:

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

unsigned short x; //range 0 to 65553 signed short x; //range ± 32767 short x; //assumed signed

There are (usually) no unsigned floats or doubles. Using in a program will result in: - a compiler error (must be corrected) - a compiler warning (compiler ignores )

digits with an assumed radix point after the first digit. (actually an exponent of 2 in binary) (in decimal) 1234.567 = 1.234567 x 10 3 Mantissa = 1234567 Exponent = 3

1.234567 x 10 Exponents must match! 7.654321 x 10 (4-2 = 2) 123 456700 x 10 Move radix point, subtract from exponent 7.654321 x 10 2 131.110321 x 10 2 Add the mantissas, keep the common exponent

131.110321 x 10 2 1 31110321 x 10 Convert the result to Scientific Notation 1.311103 x 10 4 Round off to 7 significant digits 1.311103 x 10 4 Done!

Consideration Integer Float Increasing Size increases Range increases number of significant digits Arithmetic fast and easy slow and complicated Precision always precise imprecise due to max number of significant digits Range Limited (small) Unlimited (practically)

- Use Integers unless Floats are needed - Use larger Integer types when increased (larger numbers) is needed - Use larger Float types when increased (number of significant digits) is needed.

String variables are not native to C++; they are an Extension. To use this type, you must:

NAME: an identifier (created by the programmer) (DATA) TYPE: describes data values and operations CONTENTS: can change during execution of the program

type identifier; type identifier = literal; type id1, id2, id3; type id1 = lit1, id2 = lit2, id3 = lit3;

type identifier = literal; - Creates a new variable named identifier of type type - Sets the initial value of the variable to literal

Example: int a = 3; 1. Memory is allocated ( set aside ), the size of an int. 2. The location is named. 3. The value 3 is placed in the location.

change the value of a variable variable = expression; - The variable must already be declared - The expression must evaluate to a value that is compatible with the type of the variable.

compatible usually means same type, as for and. char lettergrade = A ; string our_cheer = Go Big Blue! ; char lettergrade = A ; string our_cheer = Go Big Blue! ; lettergrade = our_cheer;

floats and integers are generally cross- compatible syntactically, but semantically there are caveats ("dangers"). double y = 3; // no problem short x = 2.5;//OK, BUT see below

variable = expression; - Evaluate the expression on the right. - When the types differ but are compatible, convert the expression s type to the variable s type. - Change the current value of the variable to the evaluated result.

Float values are truncated (not rounded). int x = 2.9; // sets x to 2 double y = 3.6; int z = y; // sets z to 3 cout << x << " " << y; 2 3

Operators,,, work as expected except: - integer integer results in an integer - Mixed operands (one int, one float) always results in a float. double x = 2/3; // int/int->int cout << x; // prints 0 x = 2/3.0; // int / float -> float cout << x; // prints 0.6666

Modulus Operator for integers: int dividend, remainder; dividend = 7 / 5; // dividend = 1 remainder = 7 % 5; // remainder = 2

Order of Precedence: First:,, left to right Second:, left to right int x; x = 5 + 2 * 4 6 / 3; // x = 11 // x = 5 + (2*4) (6/3) // x = 5 + 8 2 // x = 11

Parentheses may be used double x; x = (3 + 2) * 5; // x = 25 X = 3 + 2 * 5; // x = 13

Raising to an Exponent, Square Root, Round double x = sqrt(16); // x = 4 double y = pow(3,2); // y = 9 double z = round(2.8); // z = 3.0 double w = round(2.2); // w = 2 double d = round(3.12345 * 100) / 100; // d = 3.12

int y=0, x=0; x++; // x = x + 1; ++x; // x = x + 1; y = x++; // y = x; x = x + 1; y = ++x; // x = x + 1; y = x; x--; // x = x 1; --x; // x = x 1;

int X=1; X += 2; // x = x + 2; X -= 2; // x = x 2; X *= 2; // x = x * 2; X /= 2; // x = x / 2;

An identifier with a value that can never change. Same as a variable declaration with an initial value, but with the reserved word in front: const type identifier = literal;

const double tax_rate = 0.06; // syntax error: value cannot be changed tax_rate = 0.07;

Why use constants? 1. Clarity 2. Consistency 3. Maintainability

Now we know! how well a programmer can understand a program due to use of good style. What is 0.06? A tax rate? Interest rate? Fee rate?

when a value that should be exactly the same throughout a program actually is. The value of is not consistent this is better:

Any change to a program after it is finished. The ease/speed of doing Maintenance.

In 2013, by coincidence, the state and local rates were the same In 2016, the local tax rate changed to 0.07 (but the state rate remained at 0.06) Now we have to go through all programs, find all 0.06 s and decide if each needs to change to 0.07 or not.

This would have been better: Now, all we have to do is change the constant s value in one place, and re-compile all programs that use it.

Term Variable Declaration Allocate Assignment Mantissa Modulus Constant Definition Container for data: has a name, type and current contents. Statement that defines a variable (name, type, initial variable) and allocates the variable during execution. During execution, the reservation of memory location(s) represented by a variable. Statement that changes the value (current contents) of a variable. The significant digits of a number in Scientific Notation, with an assumed radix point after the first digit. Operator that calculates the remainder of an integer division. % in C++ An identifier with a declared type and value that cannot change.

Term Clarity Consistency Maintenance Maintainability Definition How well a programmer can understand a program due to use of good style. When a value that should be the same throughout a program actually is. Any changes to a program after it is finished. The ease/speed of changing a program after it is finished.