CISC 101 Week 9 - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

CISC 101 Week 9

Description:

1. review primitive & reference data types. list all the integer ... exact binary representation of ... range of representable values. don't forget ... – PowerPoint PPT presentation

Number of Views:122
Avg rating:3.0/5.0
Slides: 35
Provided by: jamesr59
Category:

less

Transcript and Presenter's Notes

Title: CISC 101 Week 9


1
CISC 101 Week 9
  • Number Types
  • Representations Implications

2
Number Representation
  • 1. review primitive reference data types
  • list all the integer floating point number
    types
  • plus boolean char types
  • plus arrays strings
  • 2. discuss representation of number types
  • integers the 2's complement representation
  • exact binary representation of decimal integral
    values
  • real numbers floating point representation
  • uses scientific notation, partitions the
    allocated memory
  • range and, critically, precision are limited
  • by the amount of memory for the mantissa
    exponent

3
Number Representation
  • 3. show implications problems
  • issues that are related to number representation
  • A. type conversions
  • mixed mode arithmetic
  • automatic type conversions
  • casting forced type conversion
  • B. problems with integers
  • overflow, wraparound
  • C. problems with floating point numbers
  • limited precision, normalization errors

4
Primitive Types Numbers
  • Java primitive types for numbers
  • the default number types

5
Primitive Types Others
  • other primitive data types
  • boolean
  • is a non-numeric type for logical values of true
    false
  • only 2 possible values, literals are written as
    true false
  • char
  • individual characters (internally coded as ve
    integers)
  • Java chars are defined by the Unicode characters
    set
  • it has 65000 different characters, mostly not
    used by us
  • literal char values
  • are written as 1 character in single quotes 'a',
    'X'
  • boolean done false
  • char myInitial 'J'

6
Reference/Object Types
  • non-primitive types
  • terminology reference or class or object types
  • arrays typeName , typeName
  • ordered groups (lists, tables) of values, any
    base type
  • they are Java objects
  • with attribute variables (length) no methods
  • String a class or reference type
  • for sequences (or lists) of characters
  • in many ways, we use String objects
  • as we use primitive type values
  • in assignment, as parameters, ...
  • Java String objects have no variables, many
    methods
  • TextInputFile inFiles new TextInputFile10
  • String fileName "inData.txt"

7
Constants
  • Java's special declaration
  • for values that don't change
  • syntax final typeName constantName value
  • semantics
  • given a value only once, in the declaration
  • it's illegal to try to assign a new value
  • as a convention
  • constant identifiers use only UPPER_CASE
  • named constants
  • clarify the program, make changes easier
  • // declare a global int constant
  • public static final int MAX_NUM 100
  • // declare a global double constant
  • public static final double GST_RATE 0.05

8
Number Representation
  • internal representation of number types
  • all data values
  • are stored in computer memory
  • all representations in computer memory
  • are binary
  • binary representation means that
  • the smallest unit of memory has just 2 possible
    values
  • bit is the name for the basic unit of memory
  • each bit of memory can have 1 of 2 possible
    values
  • the values are 0 or 1 (off or on, low or high)

9
Number Representation
  • we begin with a brief review
  • of the representation of decimal numbers
  • numbers written in base 10
  • indicated by the subscript 10 after the number
  • in the familiar decimal representation of
    integers
  • each decimal digit has 10 possible values, 0 .. 9
  • each place value in decimal counting is a power
    of 10
  • Decimal Number Its Interpretation
  • 76310 gt 7 hundreds, 6 tens, 3 ones
  • 7 100 6 10 3 1
  • 7 102 6 101 3 100
  • 10110 gt 1 hundreds, 0 tens, 1 ones
  • 1 100 0 10 1 1
  • 1 102 0 101 1 100

10
Number Representation
  • binary representation of numbers (integers)
  • first, note that all integers (in a specified
    range)
  • can be represented exactly
  • binary (or base 2) representation
  • is just another way of writing the numbers
  • indicated by the subscript 2 after the number
  • each binary digit has 2 possible values, 0 or 1
  • so each place value in binary counting is a power
    of 2
  • therefore, 101 in binary counting is the number 5
  • Binary Number Its Interpretation
  • 1012 gt 1 fours, 0 twos, 1 ones
  • 1 4 0 2 1 1
  • 1 22 0 21 1 20
  • the integer number 5

11
Number Representation
  • binary representation of numbers (integers)
  • each Java primitive data type
  • including the integer types
  • is allocated some fixed number of bits
  • byte 8 bits, short 16 bits, int 32 bits, long
    64 bits
  • this number of bits
  • determines the range of representable values
  • don't forget that
  • we need to represent -ve as well as ve values

12
Number Representation
  • binary representation of numbers (integers)
  • Java other programming languages use
  • what is called twos complement integer
    representation
  • it's a little different from simple binary
    counting
  • in twos complement
  • the place value of the highest power (the
    leftmost bit)
  • is interpreted as negative
  • illustrated on the next slide, for a hypothetical
    4-bit type

13
Number Representation

14
Number Representation
  • binary representation of numbers
  • floating point representation for real numbers
  • the relationship between
  • the number and its representation
  • is more complicated than for integers
  • numbers are stored in scientific/exponential form
  • the memory bits for a number are split into 3
    parts
  • of course, the representation for each part is
    still binary
  • the parts for a floating point number are
  • the sign, the mantissa (0.xxx), the exponent
  • sign 0.xxxxx exponent

15
Number Representation
  • binary representation for floating point
  • the parts for a floating point number are
  • the sign, the mantissa (0.xxx), the exponent
  • sign 0.xxxxx exponent
  • the sign
  • ve -ve is coded using a single bit
  • the mantissa
  • uses multiple bits to code a binary fraction
  • each column is a negative power of 2
  • the exponent
  • uses multiple bits to code an integer power of 2

16
Number Representation
  • binary representation floating point
  • Java's double type uses 64 bits for each value
  • the 64 bits are allocated among the 3 parts as
    follows
  • 1 bit for the sign
  • 11 bits for the exponent
  • 52 bits for the mantissa
  • the allocation of bits has important implications
  • the number of bits for the exponent limits the
    range
  • the number of bits for the mantissa limits the
    precision
  • how many decimal (binary) places of accuracy it
    can store

17
Implications
  • these representations have implications
  • for us, as programmers
  • 1. the result types for arithmetic operators
  • when both operands are the same type
  • arithmetic with them produces a result of that
    type
  • for example 5 2 produces an integer result
  • but 5.5 2.2 produces a floating point result
  • when arithmetic mixes integer floating point
  • we know now
  • that the internal representations are very
    different
  • because of this
  • Java's arithmetic operators are for operands of
    one type

18
Implications
  • 1. result types in arithmetic
  • the solution for mixed-type arithmetic
  • is automatic type conversion
  • for example 5.5 2
  • integer types are "promoted" to floating point
    types
  • most commonly this happens when we do
  • intValue someOperator doubleValue
  • the int is promoted to a double
  • the result is a double
  • // Java arithmetic operators
  • // apply only to integer or to floating point
  • double num1 5.5 2 // promote 2 to 2.0, num1
    gets 11.0

19
Implications
  • 2. assignment type conversions
  • variable expression
  • sometimes variable expression
  • are different number types
  • when widening would occur
  • to a type with a bigger range
  • that is, a type conversion with no loss of
    accuracy
  • in other words, a conversion in this direction
  • byte ? short ? int ? long ? float ? double
  • most commonly for us int ? double
  • then there is automatic conversion
  • of the expression to the type of the variable
  • we saw that this applies to parameter passing too
  • the argument in a call may be promoted
  • to the parameter type of the method

20
Implications
  • 2. assignment type conversions
  • however, when narrowing would occur
  • with a possible loss of accuracy
  • in other words, a conversion in this direction
  • double ? float ? long ? int ? short ? byte
  • this happens most often from double to int
  • it is not automatic, generates an error message
  • since there could be a loss of accuracy

21
Implications
  • 3. casting to force a type conversion
  • for a narrowing conversion to be legal
  • requires explicit type conversion by casting
  • syntax variable (destination type) expression
  • casting truncates floating point values
  • it just discards any fractional part
  • so it does not do rounding
  • for example (int)Math.PI is 3, (int)0.99999 is 0

22
Implications
  • 3. explicit type conversion casting
  • versus automatic conversion
  • double x 2 2 // widening legal automatic
  • // public static native double pow(double a,
    double b)
  • double w Math.pow(x, 5) // widening legal
    automatic
  • int y 2.5 // narrowing illegal, error
  • int z (int) 2.5 // casting forced conversion
  • // truncates the FP value

23
Integer Issues
  • problems pitfalls with integer numbers
  • overflow
  • is an attempt to store a number that is too big
  • with respect to the representation
  • underflow
  • is an attempt to store a number that is too small
  • with respect to the representation
  • operations causing integer underflow or overflow
  • are not detected by Java
  • instead, the result is "wraparound"

24
Integer Issues
  • an example problem
  • assume a 4-bit, twos complement representation
  • like the example on the earlier slide
  • add 7 2
  • in the 4-bit representation this is 0111 0010
  • what is the result of the addition???
  • the result is 1001, or -7
  • for another example, test the following bit of
    code
  • int x 200
  • for(byte i 0 i lt x i)
  • System.out.println("byte variable i is"
    i)

25
Floating Point Issues
  • the floating point representation has
  • some important implications
  • first, the limited precision for a float
  • recall a float uses 32 bits, allocated 1 8
    23 bits
  • so there are 23 bits allocated to the mantissa
  • these 23 binary fraction bits correspond to
  • about 7 significant (decimal) digits of precision
  • by comparison, the precision for a double
  • that uses 64 bits, allocated 1 11 52 bits
  • so with 52 bits for the mantissa
  • so a double also has limited precision
  • about 15 significant (decimal) digits of
    precision

26
Floating Point Issues
  • the fixed number of bits for the mantissa
  • limits the precision of the representation
  • this can cause rounding and truncation errors
  • here are a couple simple examples
  • A. for repeating decimals
  • the value of 1/3 must be approximated
  • B. 12345678.9 as a float becomes 12345679.0
  • the 23 bits for a float mantissa cannot represent
  • the 9 significant decimal digits in this simple
    number

27
Floating Point Issues
  • limited precision, rounding/truncation errors
  • floating point mantissa fractions are binary
  • the place values correspond to -ve powers of 2
  • a question given this binary representation
  • how do we represent the number 1/10 (0.110)?
  • what sum of -ve powers of 2 is exactly 0.1?
  • here's an example
  • of a binary fraction the corresponding decimal
    fraction
  • Binary (base 2) Fraction Its Interpretation
  • 0.1012 gt 1 half, 0 quarters, 1 eighth
  • 1 2-1 0 2-2 1 2-3
  • 1/2 0/4 1/8
  • 5/8 or the decimal fraction 0.625

28
Floating Point Issues
  • all floating point numbers
  • are stored in what is called normalized form
  • the sign
  • the mantissa as a fraction with no leading 0's
  • the exponent
  • normalizing this way means there's
  • exactly 1 representation for any given number
  • which is generally a very good thing

29
Floating Point Issues
  • all floating point numbers
  • are stored in normalized form
  • so there's only 1 representation for any given
    number
  • the binary counting number 11001102
  • in floating point, normalizes to .110011 27
  • 110011 normalizes to .110011 26
  • 11001100 normalizes to .110011 28
  • unfortunately
  • sometimes normalizing can lead to errors

30
Floating Point Issues
  • problems from normalizing
  • when we add or subtract two numbers
  • that are far apart in magnitude
  • that is, with very different exponents
  • sometimes the normalized representation
  • can store each separately, precisely, with no
    error
  • but to add or subtract them
  • requires an un-normalizing step
  • in effect this step is necessary
  • in order to have digits of equal significance
    "line up"
  • they must line up to allow adding (or
    subtracting) them

31
Floating Point Issues
  • problems from normalizing
  • this example uses decimal notation for simplicity
  • exactly the same issue applies if the notation is
    binary
  • add 2 numbers very far apart in magnitude
  • 1E18 2.5
  • or 1000000000000000000.0 2.5
  • to store these internally
  • we normalize them 0.1 1019 and 0.25 101
  • in the un-normalizing step
  • we convert the smaller number to match the
    exponents
  • in order to "line up the decimal places"
  • 0.25 101 becomes 0.00000000000000000025 1019

32
Floating Point Issues
  • example problems from normalizing
  • example uses decimal notation for simplicity
  • add 1E18 2.5
  • or 1000000000000000000.0 2.5
  • normalized 0.1 1019 and 0.25 101
  • to add them un-normalize the smaller number
  • so that exponents match to "line up the decimal
    places"
  • 0.25 101 becomes 0.00000000000000000025 1019
  • assume there are 15 decimal digits of precision
  • mantissa digits beyond the 15th are shown in red
  • 0.10000000000000000000 1019
  • 0.00000000000000000025 1019
  • 0.10000000000000000025 gt 0.100000000000000
    1019

33
Floating Point Issues
  • problems from normalizing
  • in the example
  • we added 2 numbers very far apart in magnitude
  • with the result 1E18 2.5 0.1 1019
  • that is 1E18 2.5 1E18
  • the result we end up with
  • is the same number we started with
  • a b a
  • even though
  • a b are both non-zero
  • our representation can store each of them
    separately
  • How does this happen?
  • in the un-normalizing step
  • the smaller operand was truncated to 0
  • to do the arithmetic, its value was completely
    lost

34
Number Representation Issues
  • a cautionary tale
  • in case you think this is all just "theoretical"
  • On February 25, 1991, during the Gulf War, an
    American Patriot Missile battery in Dharan, Saudi
    Arabia, failed to intercept an incoming Iraqi
    Scud missile. The Scud struck an American Army
    barracks and killed 28 soldiers.
  • ...
  • Because of the way the Patriot computer performs
    its calculations and the fact that its registers
    are only 24 bits long, the conversion of time
    from an integer to a real number cannot be any
    more precise than 24 bits. This conversion
    results in a loss of precision causing a less
    accurate time calculation. The effect of this
    inaccuracy on the range gate's calculation is
    directly proportional to the target's velocity
    and the length of time the system has been
    running. Consequently, performing the conversion
    after the Patriot has been running continuously
    for extended periods causes the range gate to
    shift away from the center of the target, making
    it less likely that the target, in this case a
    Scud, will be successfully intercepted.
Write a Comment
User Comments (0)
About PowerShow.com