Comparing floating point numbers

Slides:



Advertisements
Similar presentations
QM Spring 2002 Business Statistics Sampling Concepts.
Advertisements

Data Types. Every program must deal with data The data is usually described as a certain type This type determines what you can do with the data and how.
By Asma Khalil.  As now a days world is known as the global village. We can share our ideas through out the world and in this mean computer helps us.
Computer Science 101 Introduction to Programming.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
From Scratch to Python Learn to program like the big boys / girls!
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Python File Handling. In all the programs you have made so far when program is closed all the data is lost, but what if you want to keep the data to use.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
What is Correlation? Do we have to learn it with high calculations? Do we have to go for a complicated formula to understand it?
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
ORDER OF CONTENT AND INSTRUCTIONS A program in its simplest form usually contains three kinds of activity:  INPUT : The program asks the user for some.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
PROGRAMMING In Lesson 2. STARTER ACTIVITY Complete the starter activity in your python folder – lesson 2 Now we will see how you got on and update your.
ECS 15 Variables. Outline  Using IDLE  Building blocks of programs: Text Numbers Variables!  Writing a program  Running the program.
Graphical User Interface You will be used to using programs that have a graphical user interface (GUI). So far you have been writing programs that have.
MAKING PRODUCT DECISIONS Economics, March  Remember: we are the supplier, making decisions about what to PRODUCE!
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Computer Science 101 For Statement. For-Statement The For-Statement is a loop statement that is especially convenient for loops that are to be executed.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Software. Introduction n A computer can’t do anything without a program of instructions. n A program is a set of instructions a computer carries out.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Type Casting. Type casting Sometimes you need a piece of data converted to a different type In Python you do a typecast to cause that to happen int(3.599)
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
Module 2.2 Errors 03/08/2011. Sources of errors Data errors Modeling Implementation errors Absolute and relative errors Round off errors Overflow and.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Agenda Introduction Computer Programs Python Variables Assignment
what is computer programming?
Whatcha doin'? Aims: To start using Python. To understand loops.
Lesson 1 - Sequencing.
Data Types and Conversions, Input from the Keyboard
Topic: Python’s building blocks -> Variables, Values, and Types
IPO Charts Basic Design.
Introduction to Python Data Types and Variables
IF statements.
Algorithm and Ambiguity
Chapter 1. Introduction to Computers and Programming
Printing Lines of Asterisks
A Level Computing Component 2
String Manipulation.
Lesson 2: Program design process & Intro to code
Learning to Program in Python
Python Lesson 3 Mr. Kalmes.
Numeric Accuracy and Precision
Bools and simple if statements
Varying very versatile variables
A Digression On Using Floating Points
Selection (IF Statements)
Welcome back to Software Development!
The switch statement: an alternative to writing a lot of conditionals
Coding Concepts (Basics)
Processing Computer Components.
Chapter 5 Loops.
Inputs and Variables Programming Guides.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Programming with Alice
Reference semantics, variables and names
JavaScript.
INTRODUCTION To COMPUTER what is information? Information is that which informs. In other words, it is the answer to a question of some kind. It is thus.
Python 3 Mr. Husch.
Data Types and Maths Programming Guides.
Errors.
Python 10 Mr. Husch.
If-Statements and If/Else Statements
Hint idea 2 Split into shorter tasks like this.
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Lecture 36 – Unit 6 – Under the Hood Binary Encoding – Part 2
Python Creating a calculator.
Presentation transcript:

Comparing floating point numbers Decisions in Python Comparing floating point numbers

Comparing floating point numbers Remember that floating point numbers are stored in a computer with some inevitable error If you are working with floats a lot in a program, you should be aware that your numbers may not be exactly what you think they are If you need to compare two floating point numbers (or float variables), it’s not a good idea to test for exact equality (==). Especially if one or both have been calculated (not input from the user), there may be enough difference between them that the == will not consider them equal!

How to deal with this? Most of the time you have an idea of how close is “good enough” to be considered equal. Suppose the margin is 0.01. That is, if the two numbers are within 0.01 of each other, then they can be considered ‘equal’ for your purposes. This can be coded pretty easily: if abs(x – y) <= 0.01: print(“good enough”)