Number Representation & Conversion

Similar documents
Floating Point Arithmetic

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

Java Classes: Math, Integer A C S L E C T U R E 8

COMP2611: Computer Organization. Data Representation

More Programming Constructs -- Introduction

Declaration and Memory

COMP Primitive and Class Types. Yi Hong May 14, 2015

Zheng-Liang Lu Java Programming 45 / 79

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

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

Introduction to Computer Science and Object-Oriented Programming

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Computer System and programming in C

Operators and Expressions

Java Primer 1: Types, Classes and Operators

Up next. Midterm. Today s lecture. To follow

Datatypes, Variables, and Operations

Values, Variables, Types & Arithmetic Expressions. Agenda

Floating Point January 24, 2008

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Foundations of Computer Systems

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

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

COMP6700/2140 Data and Types

CIS133J. Working with Numbers in Java

Finite arithmetic and error analysis

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

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

These are reserved words of the C language. For example int, float, if, else, for, while etc.

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Data Representation Floating Point

Programming in C++ 6. Floating point data types

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

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

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

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

Data Representation Floating Point

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

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

Bits, Words, and Integers

System Programming CISC 360. Floating Point September 16, 2008

int: integers, no fractional part double: floating-point numbers (double precision) 1, -4, 0 0.5, , 4.3E24, 1E-14

CS111: PROGRAMMING LANGUAGE II

ECE232: Hardware Organization and Design

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

ENCE 688R Civil Information Systems. The Java Language. Mark Austin.

Expressions and Casting

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Program Fundamentals

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

CS2141 Software Development using C/C++ C++ Basics

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

Floating Point Numbers

Floating Point Numbers

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

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

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

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Introduction to Programming Using Java (98-388)

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Giving credit where credit is due

Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

Data Representation Floating Point

Giving credit where credit is due

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Computer Organization: A Programmer's Perspective

Java Fall 2018 Margaret Reid-Miller

Using the um-fpu with the Javelin Stamp

CS367 Test 1 Review Guide

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

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

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

VARIABLES AND TYPES CITS1001

VB FUNCTIONS AND OPERATORS

World Inside a Computer is Binary

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Inf2C - Computer Systems Lecture 2 Data Representation

CEN 414 Java Programming

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

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

Introduction to Computers and Programming. Numeric Values

Formatting Output & Enumerated Types & Wrapper Classes

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

MODULE 02: BASIC COMPUTATION IN JAVA

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

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

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

CS429: Computer Organization and Architecture

Number Systems. Both numbers are positive

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

CHAPTER 3 Expressions, Functions, Output

The type of all data used in a C (or C++) program must be specified

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Transcription:

Number Representation & Conversion Chapter 4 Under the covers of numbers in Java 1

How (Unsigned) Integers Work Base 10 Decimal (People) Base 2 Binary (Computer) 10 2 10 1 10 0 2 3 4 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 1 1 1 0 1 0 1 0 234 = 2 10 2 + 3 10 1 + 4 10 0 234 = 1 2 7 + 1 2 6 + 1 2 5 + 0 2 4 + 1 2 3 + 0 2 2 + 1 2 1 + 0 2 0

Signed (Two s Complement) Numbers If left-most bit is 1, interpret bits as unsigned, but subtract 2 7+1 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 1 1 1 0 1 0 1 0 1 2 7 + 1 2 6 + 1 2 5 + 0 2 4 + 1 2 3 + 0 2 2 + 1 2 1 + 0 2 0 = 234 234 2 8 = 234 256 = 22

Integer Division and Truncation Division of int and long discards remainders: 23 / 4 is 5, but 4*5 is 20! (23 % 4 is 3). System.out.println(4000000000L / 1234567); // 3240 System.out.println(3240 * 1234567L); // 399997080 System.out.println(4000000000L % 1234567); // 2920 System.out.println(4000000000L / 1234567.0); // 3240.0023652017267 4

IEEE Floating Point Standard (32 bit) First normalize the number to the form: value = 1 S SIG 2 exp S = 0 (positive) or 1 (negative) 1 SIG < 2 (expressed in 24 bit precision) 127 exp 127 - -10 38-1 0 +1 +10 38 + -2 128-2 -127 +2-127 +2 128

IEEE 754 32 bit float Value Representation: Decimal: [+/-]<digit>.<fraction> x 10 <exponent> e.g. 6.022 x 10 23 Binary: [+/-]1.<fraction> x 2 <exponent> e.g 1.11111110000101 x 2 78 Special case for 0, +/- (INFINITY), Not a Number (NAN) Bit Representation (float) S EXP FRAC b 31 b 30 b 29 b 28 b 27 b 26 b 25 b 24 b 23 b 22 b 21 b 20 b 19 b 18 b 17 b 16 b 15 b 14 b 13 b 12 b 11 b 10 b 9 b 8 b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 see: Class Web Page Examples: Number String Conversion

Floating Point Approximation 4.35 has an infinite binary expansion that is truncated 01000000100010110011001100110011 (float) 0100000000010001011001100110011001100110011001100110011001100110 (double) 4.3499999999999993 through 4.35 all give the same double Weird effects of approximation: 4.35F*100 prints as 435.0 4.35*100 prints as 434.99999999999994 4.05F*100 prints as 405.00003 7

Truncation and Rounding System.out.println(4.35*100); 434.99999999999994 System.out.println((int)4.35*100); 400 System.out.println((int)(4.35*100)); 434 System.out.println(Math.round(4.35*100)); 435 (Note this is of type long ) 8

Range v. Precision v. Space Type Range Precision Space boolean true/false Exact 8 bits byte +/- 127 Exact 8 bits short +/- ~32K Exact 16 bits int +/- ~2M Exact 32 bits float +/- ~ 10 38 ~15 digits 32 bits long +/- ~ 10 18 Exact 64 bits double +/- ~10 308 ~23 digits 64 bits 9

Java Automatic Conversion int i = (int)12.5f; // Casting conversion float to int; compile error without cast System.out.println("(int)12.5f==" + i); // Convert i to string float f = i; // int to float, widening conversion System.out.println("after float widening: " + f); // Convert f to string System.out.print(f); f = f * i; // Convert i to float - operation is float*float System.out.println("*" + i + "==" + f); // Two strings: i and f: double d = Math.sin(f); // float to double, Math.sin needs a double argument System.out.println("Math.sin(" + f + ")==" + d); // Two strings: f and d 10

Widening Primitive Conversions byte -> short/char -> int -> long -> float -> double Allowed because the result is always correct, no information lost except for loss of precision from integer to floating point Integers: Sign-extend on left to get greater width Floating point: add.0 and find closest floating point value 11

Narrowing Primitive Conversions double -> float -> long -> int -> char/short -> byte Restricted may lose information Compile error without explicit cast No runtime error if information lost Integer: Truncate bits from left Float-> Integer: drop fractional values 12

The Integer Class Static fields: BYTES, MAX_VALUE, MIN_VALUE, SIZE, TYPE Single dynamic field: value Used when we need a simple object objnum static methods for integer utilities Dynamic methods for info about value Integers are immutable! Integer value 12 Integer objnum = new Integer(12); // Or, new version Integer objnum = Integer.valueOf(12); // cache & reuse obj 13

Primitive Boxing Each primitive type maps to an Object type boolean -> Boolean char -> Character byte, short, int -> Integer float -> Float double -> Double The object is like a box around the value Sect. 7.7.4 14

Boxing and Unboxing Conversions Java often infers from the context that boxing or unboxing is required Boxing required when a reference to an object is needed Unboxing required when a primitive value is needed width Integer width = 10; Integer height = 12; Integer area = width * height; height Integer value 10 Integer value 12 area Integer value 120 15

String Conversion Values that appear in a String context are converted to String Primitives: First boxed Objects: Use tostring method to determine the string representation Note: There is a default tostring method if you don t code one returns [package.]class@address e.g. conv.conv@17d99928 16

Declaring Constants When working with numbers in programs it is of huge benefit to give names to constants By introducing named constants, code becomes more transparent to readers and if a change is needed, the change is only made in one place. Section 4.1.2 Example in final double QUARTER_VALUE = 0.25; similarly DIME_VALUE, NICKEL_VALUE, PENNY_VALUE 17

Constants in Library In Math: public static final double E = 2.7182818284590452354; public static final double PI = 3.14159265358979323846; In the default srgb space public final static Color yellow = new Color(255,255,0); In the default srgb space since 1.4 public final static Color YELLOW = yellow; 18