How Computers Handle Numbers

Similar documents
NAN propagation versus fault trapping in floating point code

Variables and Data Representation

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

A Java program contains at least one class definition.

Important From Last Time

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

Important From Last Time

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

Introduction to OpenMP

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

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

Rationale for TR Extension to the programming language C. Decimal Floating-Point Arithmetic

Page 1. Today. Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right? Compiler requirements CPP Volatile

WG21/N2088 J16/ IEEE 754R Support and Threading (and Decimal)

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Foundations of Computer Systems

APPENDIX B. Fortran Hints

What Every Programmer Should Know About Floating-Point Arithmetic

Chapter 21a Other Library Issues

Introduction to Modern Fortran

Introduction to Modern Fortran

Deep C (and C++) by Olve Maudal

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

Computer arithmetics: integers, binary floating-point, and decimal floating-point

Rationale for TR Extension to the programming language C. Decimal Floating-Point Arithmetic

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

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

Introduction to OpenMP

Introduction to OpenMP

Introduction to OpenMP. Tasks. N.M. Maclaren September 2017

Floating Point January 24, 2008

Integer Representation

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

DDR #1 ============================================

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

Testing is a very big and important topic when it comes to software development. Testing has a number of aspects that need to be considered.

Floating Point Numbers

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

18-642: Code Style for Compilers

Giving credit where credit is due

2. You are required to enter a password of up to 100 characters. The characters must be lower ASCII, printing characters.

Data Representation Floating Point

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

Introduction to Modern Fortran

Giving credit where credit is due

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

System Programming CISC 360. Floating Point September 16, 2008

Divisibility Rules and Their Explanations

These are notes for the third lecture; if statements and loops.

Digital Arithmetic. Digital Arithmetic: Operations and Circuits Dr. Farahmand

Directed Rounding Arithmetic Operations (Revision 1) WG21 Document : N2876=

Arithmetic and Bitwise Operations on Binary Data

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

Message-Passing and MPI Programming

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

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Expressions and Casting

Table : IEEE Single Format ± a a 2 a 3 :::a 8 b b 2 b 3 :::b 23 If exponent bitstring a :::a 8 is Then numerical value represented is ( ) 2 = (

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

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

Slide Set 11. for ENCM 369 Winter 2015 Lecture Section 01. Steve Norman, PhD, PEng

Floating Point Arithmetic

Topic 9: Type Checking

Topic 9: Type Checking

Topic Notes: Bits and Bytes and Numbers

Variables and literals

Computer arithmetics: integers, binary floating-point, and decimal floating-point

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

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

Bits, Words, and Integers

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

Introduction to Modern Fortran

Programming and Data Structure

Programming with MPI

Floating Point Arithmetic

Data Representation Floating Point

The Perils of Floating Point

CS429: Computer Organization and Architecture

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

18-642: Code Style for Compilers

Computer Systems C S Cynthia Lee

COMP2611: Computer Organization. Data Representation

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

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Deep C by Olve Maudal

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017

We do not teach programming

Data Representation Floating Point

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

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Math 340 Fall 2014, Victor Matveev. Binary system, round-off errors, loss of significance, and double precision accuracy.

Computer Organization: A Programmer's Perspective

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

Lecture Notes on Arrays

Arithmetic Operators. Portability: Printing Numbers

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Recitation 10: Malloc Lab

Transcription:

How Computers Handle Numbers p. 1/?? How Computers Handle Numbers Some of the Sordid Details Nick Maclaren July 2009

How Computers Handle Numbers p. 2/?? This Is Now Later Explanations of a few of the warnings Please ask for more detail if interested WARNING: This May Cause Nightmares Anyone already confused should leave now Yes, I AM serious about that!

How Computers Handle Numbers p. 3/?? More on Shifts Hardware and languages mess these up I know the history/excuses, from 40 years back [ Gate count on discrete--logic computers ] %deity alone knows why no improvement Shifts often unsigned only in hardware Involving the sign bit can have weird effects Usually, only some bits of the count used Typically, the bottom 5/6/8 bits of count Why? The ICL 1904 (c. 1965) did it properly

How Computers Handle Numbers p. 4/?? What Languages Do Shifts bits in word usually undefined As well as when shift value is negative Java defined, but uses only 5/6 bits of shift Usually undefined if signed shifts overflow I.e. left shift a one into or out of sign bit Right shifts on negatives usually unspecified As mentioned earlier, Python gets these right Why don t other languages do the same?

How Computers Handle Numbers p. 5/?? More on Signed/Unsigned Exactly what does the language specify? May vary with compiler versions, options, etc. Main gotcha is with implicit conversions Rules often depend on language context In C: preprocessor/constant/initializer/other Often undefined behaviour unpredictable C result depends on order of type changes Not just signed/unsigned/float but size of number

How Computers Handle Numbers p. 6/?? C/C++ Signed/Unsigned (1) C/C++ can be insane even when defined Assume 32--bit ints and 64--bit longs: (long)--0x7f000000 = --2130706432 (long)--0x80000000 = 2147483648 char x = a ; x == a may be false char x = a ; islower(x) may be undefined

How Computers Handle Numbers p. 7/?? C/C++ Signed/Unsigned (2) extern void fred(long,long); int a = 1, b = --1; unsigned int c = 1, d = --1; long A = 1, B = --1; unsigned long C = 1, D = --1; fred(a * d,b * c) fred(--1,--1) fred(a * D,B * C) fred(--1,--1) fred(a * D,b * C) fred(--1,--1) fred(a * d,b * c) fred(--1,--1)...... OR fred(4294967295,--1)

How Computers Handle Numbers p. 8/?? More on IEEE Signed NaNs Consider X = 0.0/0.0 = NaN Fortran SIGN(1.0,X) and C copysign(1.0,x) Both must be either --1.0 or +1.0, unpredictably And, for both, SIGN(1.0,X) = --SIGN(1.0,--X) But SIGN(1.0,0.0+X), SIGN(1.0,1.0 * X) etc.? They must be either --1.0 or +1.0, unpredictably That is a useful specification? Get real

How Computers Handle Numbers p. 9/?? IEEE and Decimal (1) Two encodings (a committee compromise) One encodes 3 decimal digits into 10 bits The other uses binary, but bounded by 10 N The standard says bad values are valid Random rubbish will give defined nonsense Exact half rounding is language--defined It does have a recommended default

How Computers Handle Numbers p. 10/?? IEEE and Decimal (2) Infinities and NaNs are similar to binary No denormalised numbers but there are cohorts Probably separate decimal and binary types Probably only IBM will push it much Unformatted I/O may well become much trickier There MAY be compiler options to convert That is possible in Fortran and C++ but not C

How Computers Handle Numbers p. 11/?? Decimal Cohorts Just like IBM 370 unnormalised numbers E.g. 1.23 = 0.123 10 1 = 0.00123 10 3 Cohort members are used in arcane ways I haven t bothered to study this area in detail May cause strange output (e.g. 0.00123e32) Decimal might do just what you want And pigs might fly, but it s not likely

How Computers Handle Numbers p. 12/?? IEEE 754 Rounding Modes DON T GO THERE The reasons are too complicated to go into Yes, even in these extra slides sorry Nor primarily IEEE 754 s fault Please ask if you want to know them

How Computers Handle Numbers p. 13/?? Exception Handling Design Clean model 1 (trad./lia--1) -- trap on failure Now generally rejected on dogmatic grounds Clean model 2 (IEEE 754) -- use error values OK, when done properly -- but it isn t Ghastly model 3 (Java/C99/C++?) -- define result Changes numeric error to logical error Ghastly model 4 (very common) -- undefined If you make a mistake, that s your problem

How Computers Handle Numbers p. 14/?? Fortran and IEEE Exceptions (1) This is available only in Fortran 2003 It defines some IEEE 754 exception handling Actually pretty well, considering the constraints But an implementation need not support it I don t know many implementations yet, either I expect to retire before seeing it much used I have no idea how useful or reliable it will be

How Computers Handle Numbers p. 15/?? Fortran and IEEE Exceptions (2) Flags are associated with the call tree They are saved and cleared on procedure entry And merged back on procedure return Flags never get unset except by programmer Intrinsics and I/O never set flags unnecessarily But any serious exception flag shows an error I.e. Divide--by--zero, overflow and invalid

How Computers Handle Numbers p. 16/?? Fortran and IEEE Exceptions (3) Big question are they set reliably? There is one explicit exception IF (X/Y > Z) PRINT *, Oh And the general requirement is a little vague Please tell me if you investigate!

How Computers Handle Numbers p. 17/?? C99 and IEEE Exceptions CENSORED Reason: good taste and public decency Ask me for the sordid details if you need it You cannot imagine the gotchas Don t trust anything that implies it is useful There is a bit on it later, actually...

How Computers Handle Numbers p. 18/?? C++ (Latest Standard) C++ is schizophrenic about C Is it a separate language? Is it a language extension? No, it s %deity alone knows what C++11 inherits most of C99, not C90 I failed to get the inconsistencies fixed C++ relies on C for its arithmetic etc. So that area will be broken in the same way It s actually worse ask offline if wanted

How Computers Handle Numbers p. 19/?? Exception Implementation Modern FP hardware/software is very sick C99 IEEE 754 requires flag and continue Permits trapping to interrupt routine But hardware interrupts are totally privileged Fixups by kernel/library/compiler handshakes Unlike in 1970s, not documented in architecture Option/configuration--dependent bugs are legion Can even crash systems from applications

How Computers Handle Numbers p. 20/?? Complex Number Exceptions (1) Not an easy problem, made worse by misdesign Complex and real fundamentally incompatible The real line is closed by two infinities One at each end, obviously i.e. like IEEE 754 The complex plane is closed by one infinity A sort of enclosing circle, but a single point Cartesian representation is all wrong for that

How Computers Handle Numbers p. 21/?? Complex Number Exceptions (2) IEEE 754 shows the problem very clearly Consider division as an example (A,B)/(C,D) = (A * C+B * D,B * C--A * D)/(C * C+D * D) Blows up in almost any arithmetic when: abs (C,D) > maxreal So we need something a bit fancier

How Computers Handle Numbers p. 22/?? Complex Number Exceptions (3) A better (but not perfect) approach is: if abs(c) > abs(d) : r = D/C; (A,B)/(C,D) = (A+B * r,b--a * r)/(c+d * r) else : r = C/D; (A,B)/(C,D) = (A * r+b,b * r--a)/(c * r+d) That gets it right, except near infinity

How Computers Handle Numbers p. 23/?? Complex Number Exceptions (4) X = 1.0e308, N = 0.0, 0.1, 0.2, 0.3,... Calculate (X,X)/(X,N * X) N=0.0...0.7 (1.0,1.0)... (1.14,0.20) N=0.8 (+infinity,0.12) N=0.9...1.2 (NaN,0.0) N=1.3...1.7 (0.0,0.0) N=1.8... (NaN,NaN) C99 Annex G example code is even worse

How Computers Handle Numbers p. 24/?? General C99 Nightmares Wording is ambiguous and inconsistent Footnotes/optional wording overrides main text No agreement even on the intent in SC22WG14 Perhaps 1 2 full implementations after 12 years Developers/customers still often specify C90 long was longest integer type -- now isn t Breaks most portable C90 code, subtly Implications and details rarely understood

How Computers Handle Numbers p. 25/?? C99/IEEE Nightmares (1) <math.h> may set either errno or IEEE flags All but <math.h>/<fenv.h> may set spuriously Error values may be anything -- 0.0, 42.0, NaN Only implementation--defined value, anyway Makes portable error detection a nightmare Mode setting is disaster -- but you don t want it Can t even call standard library or return Don t even think about setjmp/longjmp/signal

How Computers Handle Numbers p. 26/?? C99/IEEE Nightmares (2) IEEE 754 only if ------ STDC --- IEC --- 559 ------ is set Nobody knows what IEEE features do if not Or what FP --- CONTRACT ON means if it is set Or if CX --- LIMITED --- RANGE ON means anything Flags also need pragma FENV --- ACCESS ON Totally incompatible with optimisation Flags corrupted by library, just like errno Compilers will probably just get them wrong

How Computers Handle Numbers p. 27/?? C99/IEEE Nightmares (3) Many REQUIRED ways to lose NaN values Contradicts IEEE 754 s stated intent (6.2) fmax(1.23,nan) = 1.23 atan(±0.0,±0.0) returns π,--0.0, +0.0 or +π pow(--1,±infinity) returns 1.0 Simply comparing values is ambiguous And so on, ad nauseam The sign of NaNs is meaningful (e.g. copysign) But they don t actually contain any meaning!

How Computers Handle Numbers p. 28/?? C99/IEEE Annex G (1) Complex infinity/nan totally broken A = (1.0,0.0)/0.0 = (+inf,nan) = infinity A must be infinities A, A+A, A * A * (+inf) + A * (I * NaN) must be a NaN A+(--1.0,0.0) * A must be either infinity OR NaN double d = (INFINITY+I * 0.0) * (1.0+I * 0.0) double e = (NAN+I * NAN) * (NAN+I * NAN)... d,e are undefined (may be anything)

How Computers Handle Numbers p. 29/?? C99/IEEE Annex G (2) Complex arithmetic may set flags spuriously <complex.h> need not set & may corrupt errno CX --- LIMITED --- RANGE OFF may be very slow Is misimplemented under some systems Math. functions defined to lose NaNs/infs Depending on explicitly undefined behaviour Division already mentioned see also: http:/ /www.softintegration.com/docs/... whitepaper/j --- ddj.pdf