Floating point numbers in Python Floats in Python are platform dependent, but usually equivalent to an IEEE754 64-bit C “double” However, because the significand.

Slides:



Advertisements
Similar presentations
CENG536 Computer Engineering department Çankaya University.
Advertisements

CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
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.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
The Fundamental Property of Rational Expressions
1 IEEE Floating Point Revision Guide for Phase Test Week 5.
1 Lecture 9: Floating Point Today’s topics:  Division  IEEE 754 representations  FP arithmetic Reminder: assignment 4 will be posted later today.
Introduction to Python
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Multiplying, Dividing, and Simplifying Radicals
Binary Arithmetic Math For Computers.
Binary Representation and Computer Arithmetic
College Algebra Prerequisite Topics Review
The Binary Number System
Data Representation Number Systems.
Exceptions COMPSCI 105 S Principles of Computer Science.
IT 251 Computer Organization and Architecture Introduction to Floating Point Numbers Chia-Chi Teng.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Inheritance. Inhertance Inheritance is used to indicate that one class will get most or all of its features from a parent class. class Dog(Pet): Make.
Conditional & Joint Probability A brief digression back to joint probability: i.e. both events O and H occur Again, we can express joint probability in.
Algebra Form and Function by McCallum Connally Hughes-Hallett et al. Copyright 2010 by John Wiley & Sons. All rights reserved. 6.1 Integer Powers and the.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Chapter P Prerequisites: Fundamental Concepts of Algebra
CSC1018F: Object Orientation, Exceptions and File Handling Diving into Python Ch. 5&6 Think Like a Comp. Scientist Ch
CEN 316 Computer Organization and Design Computer Arithmetic Floating Point Dr. Mansour AL Zuair.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Data Representation in Computer Systems
Representing numbers and Basic MATLAB 1. Representing numbers  numbers used by computers do not behave the same as numbers used in mathematics  e.g.,
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Input, Output, and Processing
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
Lecture 9: Floating Point
CSC 221 Computer Organization and Assembly Language
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Introduction to OOP OOP = Object-Oriented Programming OOP is very simple in python but also powerful What is an object? data structure, and functions (methods)
Cem Sahin CS  There are two distinguishable kinds of errors: Python's Errors Syntax ErrorsExceptions.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Digital Logic Lecture 3 Binary Arithmetic By Zyad Dwekat The Hashemite University Computer Engineering Department.
Exponentials and Logarithms This chapter is focused on functions which are exponential These functions change at an increasing/decreasing rate Logarithms.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Chapter Seven: Expressions and Assignment Statements Lesson 07.
1 CSC 221: Computer Programming I Fall 2005 simple conditionals and expressions  if statements, if-else  increment/decrement, arithmetic assignments.
Lecture 4 Python Basics Part 3.
Introduction To Logarithms. Warm up You are investing $2500 in an account that earns 4% interest. How much money will you have in your account at the.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Cosc 2150: Computer Organization Chapter 9, Part 3 Floating point numbers.
Topics Designing a Program Input, Processing, and Output
Types CSCE 314 Spring 2016.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
Chapter 7: Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
Chapter 6 Floating Point
Operators and Expressions
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Python’s Errors and Exceptions
(Part 3-Floating Point Arithmetic)
Arithmetic Expressions & Data Conversions
CISC101 Reminders Assn 3 due tomorrow, 7pm.
IFS410 Advanced Analysis and Design
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
ERRORS AND EXCEPTIONS Errors:
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Floating Point Numbers
CISC101 Reminders Assignment 3 due today.
Arithmetic Expressions & Data Conversions
Presentation transcript:

Floating point numbers in Python Floats in Python are platform dependent, but usually equivalent to an IEEE bit C “double” However, because the significand is also finite, we start having issues with precision significantly earlier…. As a practical matter, the smallest float is usually on the order of ~ x (-1) sign ·(1.b -1 ·b -2 ·b -3...b -52 )·2 (ex-1023)

A numerically stable numeric class The idea is to define a class, log_float that we can use as if its objects were regular python floats, even though stored values are represented internally in log space, and all of the arithmetic operations that we will need (multiplication, division and addition) are also defined in such a way that they also take place in log space. We should be able to write: We should be able to freely mix log_floats, floats, and ints Developing the log_float numeric class A = log_float(2) B = log_float(3) C = 2.1 * (A + B) print C #result is 10.2 A = log_float(2) C = A * 2 print C # result is 4 or…

Special Method Attributes Handling zero values in log space is a nuisance We’ll use the built-in None object to represent LOGZERO We’ll adopt the convention of internally representing values as “extended logarithms” where zero valued reals are handled as a special case. if value: ## for positive reals self.value = math.log(value) else: ## for value was zero self.value = LOGZERO

Special Method Attributes These are methods used internally by python that are usually not directly invoked. They are called automatically when needed We will use SMAs to simulate a numeric class def __init__(self): pass The class constructor method __init__ is an example of a special method attribute you are already familiar with def __str__(self): return “This is how my object should print” The class method __str__ allows you to specify how an instance should print itself when the print funciton is invoked

Special Method Attributes These are the special methods we will minimally need to define for our numerically stable log_float class: Some hints as to how each must behave… __init__(self, value, mode = None): __str__(self): __mul__(self,other): __rmul__(self,other): __add__(self,other): __div__(self,other): __rdiv__(self,other):

“ log_float ” Special Method Attributes __init__(self, value, mode = None) Unless mode = True, this function should accept as its first argument a floating point real-space number provided by the user. This number will be then log transformed and stored in an instance variable self.value. In the special case where value is zero, self.value should be set to LOGZERO ( or None) The only time that the mode flag will be set is when the constructor has been called not by the user, but instead by an arithmetic operation involving another log_float object. If this is the case, the value being passed in is already in log space, so the self.value variable should just be set to value. We should also make sure that user-specified reals are positive

__str__(self): This might print a message indicating something about both the internal state of the object and the way that the value would be publically viewed.. So for instance if self.value is None, it might return “Public value is 0, internal value is None” If self.value is defined, it should return a message indicating that self.value itself is the internal value, and the exponent to e of self.value as the public value. Natural log values can converted back to reals with math.exp() “ log_float ” Special Method Attributes

__mul__(self,other): Multiplication is easy if both self and other are log_float objects. Since self.value and other.value should both be in log space, you can just return the sum of these values. But you’ll also need to handle the special cases where one or the other object (or both) has a value variable set to None !!! “ log_float ” Special Method Attributes self and other refer to the operands on the left hand and right hand side of the multiplication operator. These can in principle be of any type In all cases you should return an object of type log_float so that the result of the operation is, itself, a log_float. And the values you are returning are already in log space, so you should make sure to set the mode flag of the initializer. i.e. return log_float(self.value + other.value, True)

__mul__(self,other): This is easiest to handle with a try/except block. If you try to refer to or evaluate the other.value attribute and it turns out not to exist it will throw an AttributeError : try: if self.value is None or other.value is None: ## do some stuff except AttributeError: # do something to handle the fact that “other“ # isn’t a log_float. Just assume it’s a float or # int, then log transform it and carry on “ log_float ” Special Method Attributes What about the special case where the other object isn’t a log_float ?

__add__(self,other): ln(x+y) “ log_float ” Special Method Attributes Log-space addition is a minor PITA. We adopt the procedure suggested by Tobias Mann procedure suggested by Tobias Mann x and y have now been forced into a form compatible with our prior log transform. (order matters, as stability demands we keep the exponential term small) = ln(x) + ln(X+Y) – ln (x) = ln(x) + ln ( x+y ) x = ln(x) + ln ( 1+y ) x = ln(x) + ln(1 + e ln(y/x) ) = ln(x) + ln(1 + e ln(y)-ln(x) )

__add__(self,other): “ log_float ” Special Method Attributes Log-space addition is a minor PITA. We adopt the procedure suggested by Tobias Mann procedure suggested by Tobias Mann Be careful when making comparisons to None. For instance, try evaluating: -999 < None Testing for None should be “ if X is None ”, never “ if X == None ” Again, we need to consider several “edge cases”: self.value or other.value is LOGZERO other is a float or int type so evaluating other.value throws an attribute error A combination of the above scenarios

__div__(self,other): “ log_float ” Special Method Attributes You should also define a __truediv__ method that simply returns a call to your __div__ method. __truediv__ is invoked instead of __div__ if you have used from __future__ import division Division has similar edge cases as most of the other operations, but in addition we need to handle attempts to divide by zero. For instance, this would occur if other.value = LOGZERO. We can raise our own exception to handle this: if other.value is LOGZERO: raise ZeroDivisionError

__cmp__(self,other): “ log_float ” Special Method Attributes It is critical here to remember that LOGZERO is an identity with None, and will NOT evaluate as less than a negative number. You will need to be especially careful here with all the edge cases. __cmp__ is invoked by the comparison operators >, >=, ==, <=, <, or implicitly during operations like sorts. Generally, it should return -1 if object self is “less” than object other, 0 if object self is “equal” to object other, and 1 if object self is greater than object other. Another (preferred actually, but more work) option is to individually define each of the separate “rich comparison operator” methods, such as __lt__, __eq__, __gt__ etc.