Numerical Methods in Physics. Lecture 1 Intro & IEEE Variable Types and Arithmetic

Similar documents
Practical Numerical Methods in Physics and Astronomy. Lecture 1 Intro & IEEE Variable Types and Arithmetic

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Bindel, Fall 2016 Matrix Computations (CS 6210) Notes for

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

Topic Notes: Bits and Bytes and Numbers

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

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

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

Module 2: Computer Arithmetic

COMP2611: Computer Organization. Data Representation

Basics of Computation. PHY 604:Computational Methods in Physics and Astrophysics II

Inf2C - Computer Systems Lecture 2 Data Representation

System Programming CISC 360. Floating Point September 16, 2008

Floating Point Numbers

Floating Point Numbers

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Data Representation Type of Data Representation Integers Bits Unsigned 2 s Comp Excess 7 Excess 8

Topic Notes: Bits and Bytes and Numbers

Floating Point Numbers

Computer Arithmetic Ch 8

Floating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !

Computer Arithmetic Ch 8

Data Representation Floating Point

Floating Point January 24, 2008

Floating Point Numbers

Roundoff Errors and Computer Arithmetic

Numerical computing. How computers store real numbers and the problems that result

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Representing and Manipulating Floating Points. Computer Systems Laboratory Sungkyunkwan University

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

Bits, Words, and Integers

Giving credit where credit is due

Giving credit where credit is due

Foundations of Computer Systems

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

Data Representation Floating Point

Floating Point Puzzles The course that gives CMU its Zip! Floating Point Jan 22, IEEE Floating Point. Fractional Binary Numbers.

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

Representing and Manipulating Floating Points. Jo, Heeseung

Computer Organization: A Programmer's Perspective

8/30/2016. In Binary, We Have A Binary Point. ECE 120: Introduction to Computing. Fixed-Point Representations Support Fractions

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Representing and Manipulating Floating Points

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

Representing and Manipulating Floating Points

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

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

Today CISC124. Building Modular Code. Designing Methods. Designing Methods, Cont. Designing Methods, Cont. Assignment 1 due this Friday, 7pm.

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

Up next. Midterm. Today s lecture. To follow

Computers and programming languages introduction

Characters, Strings, and Floats

Representing and Manipulating Floating Points

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

CS321 Introduction To Numerical Methods

CSCI 402: Computer Architectures. Arithmetic for Computers (3) Fengguang Song Department of Computer & Information Science IUPUI.

Divide: Paper & Pencil

Numerical Methods 5633

Chapter 3. Errors and numerical stability

Written Homework 3. Floating-Point Example (1/2)

CS 33. Data Representation (Part 3) CS33 Intro to Computer Systems VIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Arithmetic and Bitwise Operations on Binary Data

Floating point. Today. IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Next time.

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

Systems Programming and Computer Architecture ( )

The Design and Implementation of a Rigorous. A Rigorous High Precision Floating Point Arithmetic. for Taylor Models

Floating Point. CSE 238/2038/2138: Systems Programming. Instructor: Fatma CORUT ERGİN. Slides adapted from Bryant & O Hallaron s slides

Floating Point Arithmetic

Data Representation Floating Point

Finite arithmetic and error analysis

CS429: Computer Organization and Architecture

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Number Representations

Organisasi Sistem Komputer

Midterm 1 topics (in one slide) Bits and bitwise operations. Outline. Unsigned and signed integers. Floating point numbers. Number representation

CS 101: Computer Programming and Utilization


Floating Point Numbers. Lecture 9 CAP

CO212 Lecture 10: Arithmetic & Logical Unit

Objectives. look at floating point representation in its basic form expose errors of a different form: rounding error highlight IEEE-754 standard

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

Integer Multiplication and Division

Chapter 4. Operations on Data

CS61B Lecture #14: Integers

Chapter 3: Arithmetic for Computers

Fixed-Point Math and Other Optimizations

What Every Programmer Should Know About Floating-Point Arithmetic

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

Chapter 2. Data Representation in Computer Systems

15213 Recitation 2: Floating Point

Today: Floating Point. Floating Point. Fractional Binary Numbers. Fractional binary numbers. bi bi 1 b2 b1 b0 b 1 b 2 b 3 b j

Transcription:

Variable types Numerical Methods in Physics Lecture 1 Intro & IEEE Variable Types and Arithmetic Pat Scott Department of Physics, Imperial College November 1, 2016 Slides available from http://astro.ic.ac.uk/pscott/ course-webpage-numerical-methods-201617

Housekeeping Variable types You should all have copies of the course info sheet. Some things to note: Course webpage: http://astro.ic.ac.uk/pscott/ course-webpage-numerical-methods-201617 Format: lectures, tutorials + group presentation sessions Lectures Tuesdays 2pm, Tutorials Fri 11am 4 lectures + tutorials in Term 1 5 lectures + tutorials in Term 2 4 2hr group sessions after all lectures Textbook: Numerical Recipes Programming & Languages

Housekeeping Variable types You should all have copies of the course info sheet. Some things to note: Assessment 75% assignments (9; weekly) 25% group project: 3 7% group marks for {presentation, code, report} + 4% for participating in others talks no exam Assignment collaboration/copying policy Group project: pairs, 45+15 presentation to the group Choose an advanced topic, let me know what + who your group is ASAP

Housekeeping Variable types You should all have copies of the course info sheet. Some things to note: Assessment 75% assignments (9; weekly) 25% group project: 3 7% group marks for {presentation, code, report} + 4% for participating in others talks no exam Assignment collaboration/copying policy Group project: pairs, 45+15 presentation to the group Choose an advanced topic, let me know what + who your group is ASAP Ask questions! (especially stupid ones)

Outline Variable types 1 Variable types 2 3

Outline Variable types 1 Variable types 2 3

Variable types logicals/booleans Single bit 0/1 = true/false integers multiple logicals, each corresponding to a power of 2 i.e. just natural numbers base 2 unsigned or signed (+1 sign bit) 0 0 1 1 0 1 0 0 + 0 3216 0 4 0 0 = 52 fixed point short (15+1-bit binary strings) or long (31+1)

Variable types floating point inexact representation of R sign bit s, mantissa/significand M and exponent e number = ( 1) s M B e E (given base B and offset E) For this example B = 2, E = 151 single (1 + 23 + 8 = 32-bit), double (1 + 52 + 11 = 64-bit), extended/quadruple (1 + 112 + 15 = 128-bit) complex composite of two floating-point numbers plus some interaction terms The bible: What Every Computer Scientist Should Know About Floating-Point Arithmetic Goldberg (1991) (available here)

Variable types strings/characters cat, dog, TSM DTC, etc. each character is encoded by some set number of bits In standard ASCII, there are 7 bits encoding 2 7 = 128 characters (not all printable) a = 97 (dec) = 61 (Hex) = 0110 0001 (bin) pointers Integer memory addresses saved as variables used to refer to other variables, functions, subroutines, pointers, objects, etc references (C++) Like pointers, but more of an alias than a variable arrays, structures, classes, objects, etc Just bundles of other variables, often with additional operations defined on them

Limitations Variable types Overflows Above the representable range for number Integers tend to wrap around (or throw errors) Floats go to ±Inf Underflows Only an issue for floats number ±0 NaNs Not a Number Only really exists in floating-point specification Most often (at least in my lousy code) results from 1 a divide by 0 2 operations on ±Inf 3 imaginary results for real functions

Outline Variable types 1 Variable types 2 3

Variable types Floating-point Addition and Subtraction A + B (when A > B) 1 shift B to the same exponent as A 2 integer add/subtract the mantissae B = 2, E = 151 again Two numbers are very different in size = the smaller is ignored There exists a smallest number that can be added/subtracted meaningfully from 1.0 machine accuracy Two numbers very similar size = subtraction causes cancellation errors Significant digits are lost, only those affected by round-off remain

Variable types Floating-point Multiplication & Division A B 1 XOR (multiply) the sign bit 2 integer add/subtract the exponents 3 integer multiply/divide the mantissae Flush to zero 0 β e min β e + 1 min β e + 2 min β e + 3 min Gradual underflow 0 β e min β e + 1 min β e + 2 min β e + 3 min Going < smallest normalisable number in the rep. causes headaches subnormal/denormalised numbers are inaccurate and slow Rounding/representation error multiplies as floats are operated on always a problem, but magnified with denormalised numbers

Variable types on integers Integer operations and arithmetic are fast and exact Well, except for division in this case the remainder is truncated there are other tricks to get quick rounded results Standard logical operations can be performed bitwise on whole integers at once This is good for: quick multiplication or division by 2 extracting/setting combinations of flags (booleans)

Outline Variable types 1 Variable types 2 3

Typing Variable types Languages can be statically typed C/C++, Fortran, many others = compatibility of variable types for each expression is checked at compile-time e.g. int mynum = 3; std::string bird = "duck"; mynum = 5; //OK! mynum = "5"; //nope. bird = mynum; //err, no. or dynamically typed e.g. Perl, python compatibility of types checked only at runtime (or not at all) = type conversion happens automatically sometimes convenient, usually just dangerous (in my opinion) Even statically-typed languages can have implicit typing The golden rule: always declare variable types explicitly in your numerical codes!!!

Default widths Variable types Most new systems are 64-bit Most scientific code was written on 32-bit machines Variable widths can differ! Classic nasty bug: passing pointers between C and Fortran C pointer has same width as long int (32 bits) on 32-bit, but twice the width on 64-bit Fortran has no separate pointer type (attribute only) Linked C-F90 code compiles fine on both machines Crashes with seg fault on 64-bit machine, runs OK on 32-bit = not enough space reserved for 64-bit pointer in C-F90 interface

Variable types Comparison using floating-point variables Never, ever write if (myvar == 0.) or if (myvar1 == myvar2) Floating point numbers cannot be trusted to be exactly equal to anything!!! Much safer to write if (std::abs(myvar)< absprec) or if (std::abs(myvar1/myvar2-1.0)< relprec) where absprec = Aɛ and relprec = Bɛ Floating point comparisons only OK to within some suitable multiple A, B >> 1 of ɛ!!! In practice A and B are set per-algorithm (according to round-off error, speed, truncation error, etc)

Basic optimisation Variable types Write things in terms of + and, minimise / if possible i.e. (x+x+x) / (y*z) is faster than 3*x / y / z Multiplication is faster than exponentiation i.e. (x*x) is faster than x^2 at some stage the extra operations add up though; (x*x*x*x*x) is probably slower than x^5 Define any simple function inline if possible There are lots of other tricks modern compilers will do most of these things for you automatically, but you can usually help them out by thinking about minimising number and complexity of operations

Variable types Renormalisation of variables Avoid working with very big floats Speed - most CPUs are quicker doing double prec than extended prec Space - doubles take less space than extendeds (although nobody seems to care nowadays) Accuracy - it is easy to go beyond the max/min representable number Also avoid working with very small floats Speed - denormalised arithmetic is slooooow Space - as with big floats: prefer lower precision Accuracy - denormalised arithmetic is inaccurate Instead, choose a scale and renormalise A B A/A max Easier + faster to multiply by a scalefactor afterwards, even if that requires type conversion

Variable types Renormalisation of variables Consider working in log space if your data span a huge range of scales, and you care about the small ones e.g. a range of 2 70 is way easier to work with than 100 10 70 often a lot faster and more accurate; no huge or tiny floats Easier + faster to exponentiate afterwards, even if that requires type conversion

Variable types OK, that s it for today. Assignment 1 is available on the homepage Covers: Floating point issues Due in: Monday Nov 7 (via your BitBucket git repo) Next meeting: Tutorial, 11am Friday Nov 4 (here) Next lecture: Tuesday Nov 8 Interpolation