Lowering the Level of

Similar documents
Procedural Programming. It's Back? It Never Went

Software. Is

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

Software. Is

ECE2049 E17 Lecture 2: Data Representations & C Programming Basics

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 = (

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

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

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

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

Pointers (continued), arrays and strings

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

Important From Last Time

Real Numbers finite subset real numbers floating point numbers Scientific Notation fixed point numbers

Pointers (continued), arrays and strings

Computer Architecture Chapter 3. Fall 2005 Department of Computer Science Kent State University

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

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

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

Independent Representation

COMP 202 Java in one week

Types, Variables, and Constants

3.5 Floating Point: Overview

Recap: Protection. Prevent unintended/unauthorized accesses. Class hierarchy: root can to everything a normal user can do + alpha

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0

Important From Last Time

Language Fundamentals Summary

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

Computer Components. Software{ User Programs. Operating System. Hardware

INTEGER REPRESENTATIONS

Computer Components. Software{ User Programs. Operating System. Hardware

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I


COMP2611: Computer Organization. Data Representation

Turning Development Outside

Set Theory in Computer Science. Binary Numbers. Base 10 Number. What is a Number? = Binary Number Example

Hi everyone. Starting this week I'm going to make a couple tweaks to how section is run. The first thing is that I'm going to go over all the slides

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Introduction to Information Security Vulnerabilities

IEEE Standard 754 Floating Point Numbers

Internal representation. Bitwise operators

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Programming with GUTs

Chapter 2. Data Representation in Computer Systems

COMP2121: Microprocessors and Interfacing. Number Systems

The four laws of programs

Stream Model of I/O. Basic I/O in C

Syntax and Variables

Real Numbers finite subset real numbers floating point numbers Scientific Notation fixed point numbers

Floating Point. The World is Not Just Integers. Programming languages support numbers with fraction

What did we talk about last time? Selection Loops Lab 3

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

2/6/2018. ECE 220: Computer Systems & Programming. Function Signature Needed to Call Function. Signature Include Name and Types for Inputs and Outputs

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

CMPSCI 145 MIDTERM #1 Solution Key. SPRING 2017 March 3, 2017 Professor William T. Verts

Internal representation. Bitwise operators

Floa=ng- Point Numbers

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

UNIVERSITY OF MASSACHUSETTS Dept. of Electrical & Computer Engineering. Digital Computer Arithmetic ECE 666

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Internal representation. Bitwise operators

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Java Programming. Atul Prakash

Software Engineering Testing and Debugging Testing

Number Representation 3/2/01 Lecture #

CS 61C: Great Ideas in Computer Architecture Performance and Floating-Point Arithmetic

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Floating Point Numbers

Topic 6: A Quick Intro To C

Few reminders and demos

C NUMERIC FORMATS. Overview. IEEE Single-Precision Floating-point Data Format. Figure C-0. Table C-0. Listing C-0.

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

18-642: Code Style for Compilers

Programming Assignment Multi-Threading and Debugging 2

3 The Building Blocks: Data Types, Literals, and Variables

MITOCW watch?v=4dj1oguwtem

The ALU consists of combinational logic. Processes all data in the CPU. ALL von Neuman machines have an ALU loop.

What Every Programmer Should Know About Floating-Point Arithmetic

Representing and Manipulating Floating Points

18-642: Code Style for Compilers

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

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

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

A Java program contains at least one class definition.

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

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

: Intro Programming for Scientists and Engineers Assignment 1: Turtle Graphics

Assertions. Assertions - Example

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Floating Point Arithmetic

Part I Part 1 Expressions

Floating Point. What can be represented in N bits? 0 to 2N-1. 9,349,398,989,787,762,244,859,087, x 1067

Hardware versus software

Module 2: Computer Arithmetic

G Programming Languages - Fall 2012

15213 Recitation 2: Floating Point

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

2 rd class Department of Programming. OOP with Java Programming

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

Transcription:

Lowering the Level of Conversation @KevlinHenney

https://twitter.com/kevlinhenney/status/761081424493449217

A goto completely invalidates the high-level structure of the code. Taligent's Guide to Designing Programs

/ WordFriday

snowclone, noun clichéd wording used as a template, typically originating in a single quote e.g., "X considered harmful", "These aren't the Xs you're looking for", "X is the new Y", "It's X, but not as we know it", "No X left behind", "It's Xs all the way down", "All your X are belong to us"

send(to, from, count) register short *to, *from; register count; { register n=(count+7)/8; switch(count%8){ case 0: do{ *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; }while(--n>0); } }

send(to, from, count) register short *to, *from; register count; { register n=(count+7)/8; switch(count%8){ case 0: do{ *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; }while(--n>0); } } I feel a combination of pride and revulsion at this discovery. Tom Duff

send(to, from, count) register short *to, *from; register count; { register n=(count+7)/8; switch(count%8){ case 0: do{ *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; }while(--n>0); } } Many people have said that the worst feature of C is that switches don't break automatically before each case label. This code forms some sort of argument in that debate, but I'm not sure whether it's for or against. Tom Duff

Earliest informal standard set by 1 st edition of The C Programming Language First de jure standard, included properly typed function declarations (function prototypes), const, better i18n support First international standard basically C89 rebadged Significant update with new language and library features, including better IEEE 754 support, compound literals and portable data types Support for threading, type-generic expressions and safer bounds-checking library functions

16-bit void * short int long

32-bit void * short int long

32-bit void * short int long long long

64-bit void * short int long long long

LP64 void * short int long long long

Java & C# short int long

LLP64 void * short int long long long

int8_t uint8_t int16_t uint16_t int32_t uint32_t int64_t uint64_t intptr_t uintptr_t int_least8_t uint_least8_t int_least16_t uint_least16_t int_least32_t uint_least32_t int_least64_t uint_least64_t int_fast8_t uint_fast8_t int_fast16_t uint_fast16_t int_fast32_t uint_fast32_t int_fast64_t uint_fast64_t

Signed magnitude A sign bit, 0 for + and 1 for, is followed by the bits representing the integer's magnitude 6402 10-6402 10 0-0 0001100100000010 2 1001100100000010 2 0000000000000000 2 1000000000000000 2

One's complement Negative integers are represented as the bitwise complement of the corresponding positive integer, i.e., i is equivalent to ~i 6402 10-6402 10 0-0 0001100100000010 2 1110011011111101 2 0000000000000000 2 1111111111111111 2

Two's complement Negative integers are represented 1 plus as the bitwise complement of the corresponding positive integer, i.e., i is equivalent to ~i + 1, which is the same as saying that it is the complement with respect to 2 N 6402 10-6402 10 0 0001100100000010 2 1110011011111110 2 0000000000000000 2

2 8 2 7-1 -2 7 256 127-128

2 16 2 15-1 -2 15 65536 32767-32768

2 32 2 31-1 -2 31 4294967296 2147483647-2147483648

2 64 2 63-1 -2 63 18446744073709551616 9223372036854775807-9223372036854775808

32767 + 1 32768

32767 + 1 undefined

32767 + 1-32768

INT_MIN - INT_MIN 0

INT_MIN + INT_MIN 0

Extra, Extra - Read All About It: Nearly All Binary Searches and Mergesorts are Broken Joshua Bloch https://ai.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html

I was shocked to learn that the binary search program that Bentley proved correct and subsequently tested in Chapter 5 of Programming Pearls contains a bug. https://ai.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html

Lest you think I'm picking on Bentley, let me tell you how I discovered the bug: The version of binary search that I wrote for the JDK contained the same bug. https://ai.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html

https://ai.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html

(low + high) / 2

low / 2 + high / 2

low + (high low) / 2

time_t

int main(void) { const time_t end_of_time = INT_MAX; puts(asctime(gmtime(&end_of_time))); return 0; }

Tue Jan 19 03:14:07 2038

int main(void) { const time_t end_of_time = INT_MAX + 1; puts(asctime(gmtime(&end_of_time))); return 0; }

Fri Dec 13 20:45:52 1901

printf("%f", 0.1 + 0.2);

0.300000

assert(0.1 + 0.2 == 0.3);

assert(0.1 + 0.2 == 0.3);

printf("%.17f", 0.1 + 0.2);

0.30000000000000004

Floating-Point Numbers Aren't Real Chuck Allison

Real numbers have infinite precision and are therefore continuous and nonlossy; floating-point numbers have limited precision, so they are finite, and they resemble "badly behaved" integers, because they re not evenly spaced throughout their range. Chuck Allison

It should go without saying that you shouldn't use floating-point numbers for financial applications that's what decimal classes in languages like Python and C# are for. Chuck Allison

significand bias exponent

single precision 32-bit double precision 64-bit extended precision 64-,80- or 128-bit

single precision 32-bit IEEE 754 sign biased exponent significand

double same_birthday(int people); // Return the probability that at least 2 people out of a sample // of size 'people' share the same birthday, assuming a uniform // distribution of birthdays and ignoring leap days. int main(void) { int people = 0; while (same_birthday(people) < 0.5) ++people; printf("%i", people); return 0; }

double same_birthday(int people) { double probability = 0; } for (int i = 1; i < people; ++i) probability += (1 probability) * i / 365.0; return probability; int main(void) { int people = 0; while (same_birthday(people) < 0.5) ++people; printf("%i", people); return 0; }

Correlation doesn't imply causation, but it does waggle its eyebrows suggestively and gesture furtively while mouthing 'look over there'. http://xkcd.com/552/

double same_birthday(int people) { double probability = 0; } for (int i = 1; i < people; ++i) probability += (1 - probability) * i / 365.0; return probability;

double same_birthday(int people) { if (people < 0) { errno = EDOM; return NAN; } else { double probability = 0; } } for (int i = 1; i < people; ++i) probability += (1 - probability) * i / 365.0; return probability;

double same_birthday(int people) { if (people < 0) { errno = EDOM; return NAN; } else if (people > 365) { return 1; } else { double probability = 0; } } for (int i = 1; i < people; ++i) probability += (1 - probability) * i / 365.0; return probability;

assert(same_birthday(0) == 0); assert(same_birthday(1) == 0); assert(same_birthday(2) > 0);... assert(same_birthday(365) < 1); assert(same_birthday(366) == 1); assert(same_birthday(367) == 1);... assert(same_birthday(int_max) == 1);

assert(same_birthday(0) == 0); assert(same_birthday(1) == 0); assert(same_birthday(2) > 0);... assert(same_birthday(365) < 1); assert(same_birthday(366) == 1); assert(same_birthday(367) == 1);... assert(same_birthday(int_max) == 1);

assert(same_birthday(0) == 0); assert(same_birthday(1) == 0); assert(same_birthday(2) > 0);... assert(same_birthday(183) < 1); assert(same_birthday(184) < 1);... assert(same_birthday(365) < 1); assert(same_birthday(366) == 1); assert(same_birthday(367) == 1);... assert(same_birthday(int_max) == 1);

The real value of tests is not that they detect bugs in the code but that they detect inadequacies in the methods, concentration, and skills of those who design and produce the code. C A R Hoare

double unique_birthday(int people) { if (people < 0) { errno = EDOM; return NAN; } else if (people > 365) { return 0; } else { double probability = 1; const int nonpeople = 366 people; } } for (int i = 364; i >= nonpeople; --i) probability *= i / 365.0; return probability;

double same_birthday(int people) { if (people < 0) { errno = EDOM; return NAN; } else if (people > 365) { return 1; } else { double probability = 0; } } for (int i = 1; i < people; ++i) probability += (1 - probability) * i / 365.0; if (probability == 1) --*(uint64_t *) &probability; return probability;

assert(same_birthday(0) == 0); assert(same_birthday(1) == 0); assert(same_birthday(2) > 0);... assert(same_birthday(183) < 1); assert(same_birthday(184) < 1);... assert(same_birthday(365) < 1); assert(same_birthday(366) == 1); assert(same_birthday(367) == 1);... assert(same_birthday(int_max) == 1);

printf("%.15f", same_birthday(365)); 1.000000000000000 printf("%.16f", same_birthday(365)); 0.9999999999999999

double same_birthday(int people) { if (people < 0) { errno = EDOM; return NAN; } else if (people > 365) { return 1; } else { double probability = 0; } } for (int i = 1; i < people; ++i) probability += (1 - probability) * i / 365.0; if (probability == 1) --*(uint64_t *) &probability; return probability;

double same_birthday(int people) { if (people < 0) { errno = EDOM; return NAN; } else if (people > 365) { return 1; } else { double probability = 0; } } for (int i = 1; i < people; ++i) probability += (1 - probability) * i / 365.0; if (probability == 1) --*(uint64_t *) &probability; return probability;

double same_birthday(int people) { if (people < 0) { errno = EDOM; return NAN; } else if (people > 365) { return 1; } else { double probability = 0; } } for (int i = 1; i < people; ++i) probability += (1 - probability) * i / 365.0; if (probability == 1) probability = nexttoward(1, 0); return probability;

double same_birthday(int people) { if (people < 0) { errno = EDOM; return NAN; } else if (people > 365) { return 1; } else { double probability = 0; } } for (int i = 1; i < people; ++i) probability += (1 - probability) * i / 365.0; return probability < 1? probability : nexttoward(1, 0);

If a plot works out exactly as you first planned, you're not working loosely enough to give room to your imagination and instincts.

The failure resulted in a loss of more than US$370 million. http://en.wikipedia.org/wiki/cluster_(spacecraft)

Simple Testing Can Prevent Most Critical Failures An Analysis of Production Failures in Distributed Data-Intensive Systems https://www.usenix.org/system/files/conference/osdi14/osdi14-paper-yuan.pdf

A majority of the production failures (77%) can be reproduced by a unit test. https://www.usenix.org/system/files/conference/osdi14/osdi14-paper-yuan.pdf

network code() { switch (line) { case THING1: doit1(); break; case THING2: if (x == STUFF) { do_first_stuff(); if (y == OTHER_STUFF) break; do_later_stuff(); } /* coder meant to break to here... */ initialize_modes_pointer(); break; default: processing(); } /*...but actually broke to here! */ use_modes_pointer(); /* leaving the modes_pointer uninitialized */ } Peter van der Linden Expert C Programming

if ((err = ReadyHash(&SSLHashSHA1, &hashctx))!= 0) goto fail; if ((err = SSLHashSHA1.update(&hashCtx, &clientrandom))!= 0) goto fail; if ((err = SSLHashSHA1.update(&hashCtx, &serverrandom))!= 0) goto fail; if ((err = SSLHashSHA1.update(&hashCtx, &signedparams))!= 0) goto fail; goto fail; if ((err = SSLHashSHA1.final(&hashCtx, &hashout))!= 0) goto fail; Mike Bland "Goto Fail, Heartbleed, and Unit Testing Culture" https://martinfowler.com/articles/testing-culture.html

Goto Fail, Heartbleed, and Unit Testing Culture Mike Bland https://martinfowler.com/articles/testing-culture.html

These bugs are as instructive as they were devastating: They were rooted in the same programmer optimism, overconfidence, and haste that strike projects of all sizes and domains. Mike Bland https://martinfowler.com/articles/testing-culture.html

These bugs arouse my passion because I've seen and lived the benefits of unit testing, and this strongly-imprinted experience compels me to reflect on how unit testing approaches could prevent defects as highimpact and high-profile as these SSL bugs. Mike Bland https://martinfowler.com/articles/testing-culture.html

Test-Driven Development is not a testing technique, although you do write a lot of valuable automated tests. It is a way to solve programming problems. It helps software developers make good design decisions. Tests provide a clear warning when the solution takes a wrong path or breaks some forgotten constraint. Tests capture the production code s desired behavior.

TDD is fun! It s like a game where you navigate a maze of technical decisions that lead to highly robust software while avoiding the quagmire of long debug sessions. With each test there is a renewed sense of accomplishment and clear progress toward the goal. Automated tests record assumptions, capture decisions, and free the mind to focus on the next challenge.

TDD is fun!

The fastest I/O is no I/O. Nils-Peter Nelson

Command-line tools can be 235x faster than your Hadoop cluster Adam Drake http://aadrake.com/command-line-tools-can-be-235x-faster-than-your-hadoop-cluster.html

c

299792458

http://ithare.com/infographics-operation-costs-in-cpu-clock-cycles/

Low-level programming is good for the programmer's soul. John Carmack