代写 math python COMP1730COMP6730 Programming for Scientists

COMP1730COMP6730 Programming for Scientists
Floating point numbers

Announcements
Marks for homework 2 available on Wattle.
Read the news and discussion forums!
Questions about code: use forums or ask tutors! instead of emailing comp1730anu…
Feedback survey https: www.surveymonkey.comrBPRK9ZK by course reps Anni, Max Denzil.

Outline
Numbers in binary and other bases Floating point numbers
Error analysis

Representing Integers

Sequential encoding
A sequential encoding system represents each item words, numbers, etc by a sequence of symbols; the order position of a symbol in the sequence carries meaning, as much as the symbol itself.
For example,
representation interpret as one 007 700

Positional number system
The position of a digit is the power of the base that it adds to the number.
For example, in base 10:
1864
1 thousand 8 hundreds 6 tens 4 ones 1103 8102 6101 4100
The position of the least significant digit is 0. b0 1 for any base b.
The representation of any nonnegative integer number is unique, except for leading zeros.

We can count in any base
For example, in base 3: 21200013
236
135 234 033 032 031 130
27292432811
1864
Eachdigitisoneof0,…,b1.
nnnnb means a number in base b.

Ancient Babylonians ca 2,000 BC counted in base 60.
31601 4600 1864
However, they did not have a symbol for 0: can mean 1, 60, 3600, 160, etc.

Binary numbers
Binary numbers are simply numbers in base 2.
111010010002 1210
129 128 027 126 025
024 123 022 021 020
1024512256648
1864

Arithmetic
Long multidigit addition, subtraction, multiplication, division and comparison of nonnegative numbers work the same way in any base.
111 01012
01112 11002
10012 1012 10012 000002 1001002 1011012
02 02 02 02 12 12 12 02 12 12 12 102

Bits and bytes
In the electronic computer, a single binary digit bit is represented by the presence or absence of current in a circuit element.
8 bits make an octet, or byte.
Digital hardware works with fixedwidth number representations words.
Common word sizes: 32bit, 64bit.
Note: Python allows arbitrarily large integers!

Floating point numbers

Representing fractional numbers
Extend the number system to negative positions; decimal point marks position zero.
0.2510
0100 2101 5102
012110 51100
0.012
020 021 122
01012 114
0.2510

Not every fraction has a finite decimal expansion in a given base.
For example,
13 0.3333… in base 10
15 0.001100110011 . . . in base 2 13 0.1inbase3.
Because digital computers work with numbers of fixed width, decimal representation of fractions have finite precision.

Floating point representation
A floating point number in base b, x m be
consists of three components: the sign or ;
the significand m; the exponent e;
The number is normalised iff 1 m b. Is 0 a normalised number?

Compact small representation of numbers far from the decimal point.
9
9
1.08 10 1 080000000 .0 7
7
6.4410 0.000000644
1.00000012 2111102 30

1 0000001000000000000000000000002

Floating point types, as implemented in computers, use fixedwidth binary integer representation of the significand and exponent.
In a normalised binary number the first digit is 1, so only the fraction is represented m 1.f .
The exponent is biased by a negative constant.
IEEE standard formats:
single: 23bit fraction, 8bit exponent.
double: 52bit fraction, 11bit exponent.
Standard also specifies how to represent 0, , and nan not a number.

x 1s 1.f 2 2e127
10 1.012 2011111002127
1 1 22 264321684127
1.2523 1.258 0.15625
Image from wikipedia.org

Type float can represent infinity: 1 1e320
inf
Most math functions raise an error rather than return inf.
For example, 1 0, or math.log0.
nan not a number is a special value used to
indicate errors or undefined results.
1 1e320 1 1e320
nan
math.isinf and math.isnan functions.

Floating point number systems
A floating point number system b, p, L, U is defined by four parameters:
the base b;
the precision: number of digits in the fraction
of the significand p; and
the lower L and upper U limit of the
exponent.
IEEEdoubleprecisionis2,52,1023,1024 with some tweaks.

The numbers that can be represented exactly in a floating point number system are not evenly distributed on the real line.
E.g.,2,2,2,1:
E.g., in a 2, 52, 1023, 1024 system,
the smallest number 0 is 21023 10308, Actual IEEE double standard can represent
numbers down to 4 10324.
the smallest number 1 is 1 252
1 2 1016.
Rounding the significand to p 1 digits causes
a discrepancy, called the rounding error.

Because of rounding, mathematical laws do not always hold for floating point arithmetic.
a 11111113.0
b 11111111.0
c 7.51111111
a b c a b c
False
a b c a b c
4.488374116817795e10
Example from Punch Enbody Almost never compare floats with .

Error analysis
Let x be the true value and x the approximate measured or representable number.
The absolute error is x x x. The relative error is x x x .
Rounding to p 1 digits in base b,
the absolute error is 12bp be, and the relative error is 12bp.
x x

Error propagation
The absolute error fx fx is approximately
proportional to df xx x. dx

IEEE standard specifies that floating point arithmetic operations and some other math functions, e.g., are exact, except for the rounding error in the result.
This does not mean errors do not propagate. Ify x1 x2,theny x1 x2
Also if either x1 or x2 is negative.
Ify xi x2,theny x2 x1 x1 x2 x1 x2.

Example, continued:
a 1.1111113 107, b 1.1111111 107
and c 7.51111111.
y b c 1.111111851111111 107.
y 253 107 1.1 109 assuming double
precision and no error other than rounding.
abcayy plusroundingerror.
When adding floating point numbers, the absolute rounding error is proportional to the magnitude of the largest number that is rounded.
Takehome: Computer numbers ! math. numbers