The float type and more on variables FEB 6 TH 2012

Similar documents
About Variables in Python F E B 1 1 T H

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

Variable and Data Type I

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Jython. secondary. memory

ENGR 101 Engineering Design Workshop

Beyond Base 10: Non-decimal Based Number Systems

Variable and Data Type I

Beyond Base 10: Non-decimal Based Number Systems

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

PROGRAMMING FUNDAMENTALS

Computer System and programming in C

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

Beyond Blocks: Python Session #1

Chapter 2 Writing Simple Programs

Algorithms and Data Structures

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Finite arithmetic and error analysis

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

Python Programming Exercises 1

Built-in Types of Data

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

CS177 Python Programming. Recitation 2 - Computing with Numbers

Programming with Python

What Every Programmer Should Know About Floating-Point Arithmetic

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Introduction to Scientific Computing Lecture 1

Imelda C. Go, South Carolina Department of Education, Columbia, SC

static int min(int a, int b) Returns the smaller of two int values. static double pow(double a,

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

Expressions in JavaScript. Jerry Cain CS 106AJ October 2, 2017

Numeric Precision 101

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

Up next. Midterm. Today s lecture. To follow

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Introduction to Computer Science and Object-Oriented Programming

3. Java - Language Constructs I

Lecture 2: Python Arithmetic

Full file at

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS


CEN 414 Java Programming

egrapher Language Reference Manual

Python Numbers. Learning Outcomes 9/19/2012. CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17

Short, Unique and Mysterious

EE 109 Unit 3. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

Variables, expressions and statements

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

Program Planning, Data Comparisons, Strings

CMSC 201 Computer Science I for Majors

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B

Zheng-Liang Lu Java Programming 45 / 79

CSCE 110 Programming I

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Fundamentals of Programming (Python) Getting Started with Programming

Variables, Expressions, and Statements

Functions!!! Why functions? Functions provide good way to design systems!

Module 01: Introduction to Programming in Python

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

Outline. Data and Operations. Data Types. Integral Types

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Getting Started with Python

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Like most objects, String objects need to be created before they can be used. To create a String object, we can write

Hexadecimal Numbers. Journal: If you were to extend our numbering system to more digits, what digits would you use? Why those?

SI Networked Computing: Storage, Communication, and Processing, Winter 2009

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CS 102 Lab 3 Fall 2012

Computer Science & Engineering 150A Problem Solving Using Computers

Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

PYTHON. Values and Variables

Basic Syntax - First Program 1

Inductive Data Types

Week 3 Lecture 2. Types Constants and Variables

CSCE 110: Programming I

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Introduction to Python, Cplex and Gurobi

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Bits, Words, and Integers

Chapter 2. Data Representation in Computer Systems

Downloaded from Chapter 2. Functions

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Data Representation Floating Point

Floating Point Representation in Computers

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

cs1114 REVIEW of details test closed laptop period

Integers and Rational Numbers

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Floating Point : Introduction to Computer Systems 4 th Lecture, May 25, Instructor: Brian Railing. Carnegie Mellon

printf( Please enter another number: ); scanf( %d, &num2);

XQ: An XML Query Language Language Reference Manual

Transcription:

The float type and more on variables FEB 6 TH 2012

The float type Numbers with decimal points are easily represented in binary: 0.56 (in decimal) = 5/10 + 6/100 0.1011 (in binary) = ½+0/4 + 1/8 +1/16 The i th bit after the decimal point has place value 1/2 i. Example: 0.1101 = ½ + ¼ + 1/16 = 13/16 = 0.8125 However, not all real numbers (even rational numbers) can be represented exactly by finite sums of these fractions.

Be wary of floating point errors Try 0.1 + 0.2 Try adding 0.1 ten times. Try 0.1 + 0.1 + 0.1 0.3 In general, never test for equality with floating point numbers. This is an infinite loop! Try it. sum = 0.1 while sum!= 1: sum = sum + 0.1

Some functions for floating point numbers The math module contains functions (e.g., math.sqrt (x)) for floating point numbers. Function What it does math.ceil(x) math.floor(x) math.trunc(x) math.exp(x) math.log(x) math.log(x, b) Returns the ceiling of x as a float Returns the floor of x as a float Returns x truncated to an int Returns e x Returns logarithm of x to the base e Returns logarithm of x to the base b There are many other functions in the math module: trignometric, hyperbolic, etc. There are also constants: math.pi and math.e.

Try solving these problems Given the radius of a circle, find its area. Given a positive integer, find the number of digits it has. Example: int(math.ceil(math.log(565656, 10))) There are also some built-in Python functions that are useful for math: round(x, n): returns the floating point value x rounded to n digits after the decimal point. If n is omitted, it defaults to zero. abs(x): returns the absolute value of x

Range of floating point numbers What is the largest floating point number in Python? Here is an interesting way to find out: prod = 1.0 while prod*1.1!= prod: prev = prod prod = prod*1.1 print prev, prod The output is 1.78371873262e+308 inf

What does this output mean? Python uses an object called inf to represent positive infinity. When 1.78371873262e+308 was multiplied by 1.1 (i.e., increased by 10%), we went beyond the upper limits of type float. This means that the largest floating point number in Python has 308 digits. Notice that the while-loop terminated because inf * 1.1 equals inf.

A better version of this program import math prod = 1.0 while not math.isinf(prod): prev = prod prod = prod*1.1 print prev, prod There is a function called isinf(x) in the math module that tells us if x equals inf.

The sys module contains information on the largest float Try: import sys sys.float_info.max On my machine this value is 1.7976931348623157e+308

Sequence types There are seven sequence types in Python: strings, Unicode strings, lists, tuples, bytearrays, buffers, and xrange objects. Later we will study study strings, lists, and tuples in more detail. There are many powerful built-in operations on sequence types provided by Python. Stay tuned for details.

Variables in Python Variables are sticky notes attached to objects. What happens during the assignment statement? x = 10 A memory cell (made up of 4 or 8 bytes) is created and 10 is placed in it. The name x is attached ( stuck ) to this memory cell.

More on variables What happens when x = x + 1 is executed? 1. The object that x is attached to (i.e., 10) is copied into some working area. 2. 1 is added to this object. 3. The new object (i.e., 11) is moved into a (different) memory cell. 4. The name x is now attached to this new memory cell.

Multiple sticky notes at the same location What happens when we execute: x = 10 y = x x = x +1 1. x is a sticky note attached to a memory cell containing 5. 2. Then y is also stuck to this very location. 3. When x = x + 1 is executed, remember the memory cell containing 10 remains unchanged and the sticky note x is moved to the cell with 11. 4. Therefore y continues to have value 10.

Variable names Variable names need to start with a letter (upper or lower case) or an underscore (i.e., _ ). Following the first character, any sequence of letters, digits, and underscores is allowed. Python has a small number of keywords, that cannot be used as variable names: and del from not while as elif global or with assert else if pass yield break except import print class exec in raise continue finally is return def for lambda try

More on variables Case matters. The variables count and Count are different. Do not use lower case el ( l ), upper case oh ( O ), or upper case eye ( I ) as single letter variable names. These are hard to distinguish from numerals 0 and 1 in some fonts. Use meaningful names: e.g., factorbound, myupperlimit, sequencelength, etc. Watch out for spelling errors in variable names.

Scope of a variable In Python there is no explicit variable declaration. In many languages (C, Java, etc.) variables have to be declared before they can be used. In programs in these languages, a variable comes into existence when it gets declared. In Python, a variable comes into existence when it is first assigned a value. The variable lives until the end of the program or until it is explicitly deleted using the del operator (this operator will become useful later). The scope of a variable is the portion of the program that the variable is in existence for.