Binary numeral system, floating point Programmazione...
Transcript of Binary numeral system, floating point Programmazione...
Programmazione I
Binary numeral system, floating point
Fabrizio Messina [email protected]
www.dmi.unict.it/~messina
Numbers representation.
(N) Natural numbers: 1, 2, 3, ..., N, ....(N_0) Whole numbers: 0,1,2, ..., N, ...(Z) Integers: ...-N...3,2,1,0,1,2,3,...N,...(Q) Rational numbers: all numbers as p/q (R) Real numbers: it includes Z, Q plus irrational numbers as SQRT(2), PI, Euler’s number
Numbers representation.
N, Q, Z, R are infinite sets.
BUT The amount of memory to represent numbers on a computer is fixed.
Therefore, we can represent only finite sets of numbers in a computer (finite-precision numbers)
Numbers representation.
Example (finite precision numbers): Representing numbers with only 3 decimal digits: 000 ... 999We cannot represent numbers such that: - larger than 999 or negative - fractions (as 3/4)- irrational numbers (as PI)
Numbers representation.
Overflow and underflow errors. Finite-precision numbers are not closed with respect to operations {-,+,/,*}, for example: 800+700 = 1500 (overflow!)100*70 = 7000 (overflow!)005-007=-2 (overflow!)007/002=3.5 (not an integer - underflow!)
Numbers representation.
Numbers representation.
Generally, any number can be expressed in any base b.
The base is also said radix.
With decimal base/notation, we use 10 digits: 0 1 2 ... 9
With binary base/notation, we use 2 digits: 0 1 With octal base/notation, we use 8 digits: 0 1 2 ... 7
Numbers representation.
Numbers representation.
Numbers representation from decimal to binary
Numbers representation from binary to decimal
Numbers representation Negative numbers
With k bit one can represent 2^k numbers.
- For positive numbers and k=8 bit, 256 numbers, from 0 to 255.
- For positive plus negative numbers, from -127 to + 127 (zero included) - Sign (+/-) must be represented.
Numbers representation Negative numbers
Ways to represent a negative number: - signed magnitude (modulo e segno)- one’s complement (complemento a uno)- two’s complement (complemento a due)- excess 2^(m-1) (m is the number of bits used for
representation)
Excess 2^(m-1): for any number n, it will store n+2^(m-1).
Es: Number to store: -3. Will store the representation of (-3 + 128)
Numbers representation floating point
9 x 10^-282 x 10^33
The problem is that in order to represent these two numbers, we need 62 significant digits in the results.- it will waste memory- We need a system to represent all these numbers such that
the range of expressible numbers will be independent of the number of significant digits used.
Numbers representation floating point
Numbers representation floating point
Regions 1 and 7 OVERFLOW Regions 2 and 6 (ROUNDING)Regions 3 and 5 UNDERFLOW
n= f x 10^e, 0.100 <= |f| <= 0.999, -99 <= e <= +99
Numbers representation floating point
Real numbers: for any x,y there will be z = (x+y)/2 (density) - infinite real numbers in the line.
Floating point: -0.999x10^99 to 0.999x10^99: only 358.201 numbers!
Numbers representation floating point (IEEE 754)
Numbers representation floating point (IEEE 754)
Numbers representation floating point (IEEE 754)
EX: Number 4.35 (See Horstmann and C++ code)