Representing numbers and Basic MATLAB 1. Representing numbers  numbers used by computers do not behave the same as numbers used in mathematics  e.g.,

Slides:



Advertisements
Similar presentations
2009 Spring Errors & Source of Errors SpringBIL108E Errors in Computing Several causes for malfunction in computer systems. –Hardware fails –Critical.
Advertisements

Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Fixed Point Numbers The binary integer arithmetic you are used to is known by the more general term of Fixed Point arithmetic. Fixed Point means that we.
Types and Arithmetic Operators
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Data types and variables
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
String Escape Sequences
Floating Point Numbers.  Floating point numbers are real numbers.  In Java, this just means any numbers that aren’t integers (whole numbers)  For example…
Binary Number Systems.
Binary Representation and Computer Arithmetic
Data Representation Number Systems.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
CENG 311 Machine Representation/Numbers
CPS120: Introduction to Computer Science Lecture 8.
IT 251 Computer Organization and Architecture Introduction to Floating Point Numbers Chia-Chi Teng.
Computer Architecture
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Why does it matter how data is stored on a computer? Example: Perform each of the following calculations in your head. a = 4/3 b = a – 1 c = 3*b e = 1.
COMP 116: Introduction to Scientific Programming Lecture 28: Data types.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Calculating Two’s Complement. The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of.
Input, Output, and Processing
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
CPS120: Introduction to Computer Science Operations Lecture 9.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Week 1: Variables, assignment, expressions READING: 1.2 – 1.4.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Primitive Variables.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Copyright © – Curt Hill Types What they do.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CPS120: Introduction to Computer Science Variables and Constants.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Topics Designing a Program Input, Processing, and Output
ITEC113 Algorithms and Programming Techniques
Multiple variables can be created in one declaration
Arithmetic Operator Operation Example + addition x + y
(Part 3-Floating Point Arithmetic)
C++ Data Types Data Type
CS 101 – Sept. 4 Number representation Integer Unsigned √ Signed √
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Floating Point Numbers
Primitive Types and Expressions
Variables and Constants
Presentation transcript:

Representing numbers and Basic MATLAB 1

Representing numbers  numbers used by computers do not behave the same as numbers used in mathematics  e.g., try the following in MATLAB: help intmax x = intmax; x + 1 2

Representing numbers  numbers used by computers do not behave the same as numbers used in mathematics  e.g., try the following in MATLAB: x = ; x == == is the equality operator: Is the value of x equal to 0.3 ? The result is either 1 (true) or 0 (false).

Binary numbers  both of the previous examples are a consequence of how numbers are typically represented in software  for most software applications, numbers are represented using a base-2 (or binary) numeral system  a binary digit is called a bit  a bit can have one of two possible values  true or false  on or off  1 or 0 4

Binary numbers  how many different values can you represent using 1 bit? 5 0 1

Binary numbers  how many different values can you represent using 2 bits?

Binary numbers  how many different values can you represent using 3 bits?

Binary numbers  using n bits we can represent 2 n distinct values 8

Base-10 (decimal) integers  humans typically use a base-10 number system  the way we normally write numbers is a just a compact way to represent the underlying mathematical meaning: 4937 is shorthand for 4* * * *10 0 9

Converting binary to decimal integers  in a similar fashion, the binary integer 101 is shorthand for 1* * *2 0 which equals 5 10

Converting binary to decimal integers  using this convention, we get the unsigned binary integers 11 binarydecimal 0000* * * * * * * * * * * * * * * * * * * * * * * *2 0 7

Converting binary to decimal  to get negative numbers we can change 2 2 to -2 2 ; this gives us the signed binary integers 12 binarydecimal 0000* * * * * * * * * * * * * * * * * * * * * * * *2 0

Converting binary to decimal  using n bits, the range of unsigned binary integers in decimal is 0 to 2 n - 1  using n bits, the range of signed binary integers in decimal is -2 n-1 to 2 n

Integers in MATLAB  MATLAB supports 8, 16, 32, and 64 bit integers  unsigned  uint8, uint16, uint32, uint64  signed  int8, int16, int32, int64  the names uint8, uint16, uint32, uint64, int8, int16, int32, int64 are all examples of types  a type defines what values can be represented and what operations can be performed 14

Integers in MATLAB  we can now explain why the first example produces an unusual result: x = intmax; x + 1  line 1 means:  store the value intmax in the variable named x  line 2 means:  calculate the value x

Integers in MATLAB  we can now explain why the first example produces an unusual result: x = intmax; x + 1  the value x + 1 is the same value as x because x is already the maximum value that an int32 can hold 16

Integers in MATLAB  you get a similar result if you try to subtract 1 from intmin x = intmin; x

Integers in MATLAB  these are examples of saturation arithmetic  if the result of an integer arithmetic operation is greater than the maximum value, then the result is the maximum value  if the result of an integer arithmetic operation is less than the minimum value, then the result is the minimum value  occurs because we use a fixed number of bits to represent each integer type 18

Real numbers  most MATLAB applications deal with real numbers (as opposed to integer numbers)  if you type a plain number into MATLAB then MATLAB will interpret that number to be a real number of type double  short for "double precision" 19

Binary real numbers  the representation of double precision binary real numbers is complicated   some facts:  64 bits  smallest positive value ≈ *  largest positive value ≈ *  between 15–17 significant digits 20

Real numbers in MATLAB  any plain number that you type into MATLAB is treated as a double; e.g.,   you can also use the letter e or E for scientific notation 21 scientific notation meaningvalue 1e21 * e-21 * e+453 * e * e2.2error

Arithmetic operators  for numbers you can use the following arithmetic operators: 22 operationoperatorexampleresult addition subtraction -7 – multiplication *9.1 * division /pi / exponentiation ^5 ^ 225

Variables  except for trivial calculations, you will almost always want to store the result of a computation  a variable is a name given to a stored value; the statement: z = causes the following to occur: 1. compute the value store the result in the variable named z  MATLAB automatically creates z if it does not already exist 23 Note: The statement = z is an error in MATLAB

Variables  the = operator is the assignment operator  the statement: z = means: 1. evaluate the expression on the right-hand side of = 2. store the result in the variable on the left-hand size of = can you explain why = z is an error in MATLAB? 24 Note: The statement = z is an error in MATLAB

Variable names  a variable name must start with a letter  the rest of the name can include letters, digits, or underscores  names are case sensitive, so A and a are two different variables  MATLAB has some reserved words called keywords that cannot be used as variable names  use the command iskeyword to get a list of keywords 25

Variable names 26 valid variable names invalid variable names reason invalid x$ does not begin with a letter $ is not allowed in variable names x66x does not begin with a letter lastValueif if is a keyword pi_over_2pi/2 / is not allowed in variable names