S.Ducasse Stéphane Ducasse 1 Numbers.

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

Evaluating the floating point in Java Virtual Machine Bruno José Torres Fernandes Renato Viana Ferreira Marcília Andrade Campos {bjtf, rvf,
2009 Spring Errors & Source of Errors SpringBIL108E Errors in Computing Several causes for malfunction in computer systems. –Hardware fails –Critical.
Fixed Point Numbers The binary integer arithmetic you are used to is known by the more general term of Fixed Point arithmetic. Fixed Point means that we.
Maths & Trig, Statistical functions. ABS Returns the absolute value of a number The absolute value of a number is the number without its sign Syntax ◦
Fabián E. Bustamante, Spring 2007 Floating point Today IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Next time.
CENG536 Computer Engineering department Çankaya University.
Pemrograman Dasar - Data Types1 OPERATOR. Pemrograman Dasar - Data Types2 Arithmetic operator  + - * /  / operator denotes integer division if both.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Stéphane Ducasse«ChapterNr».1 Abstract Classes Should not be instantiated (abstract in Java) But can defined complete methods Defines a protocol common.
Round-Off and Truncation Errors
Major Numeric Data Types Unsigned Integers Signed Integer Alphanumeric Data – ASCII & UNICODE Floating Point Numbers.
Stéphane Ducasse 1 The Taste of Smalltalk.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
Stéphane Ducasse 1 Objects to the Roots: Learning from beauty.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
String Escape Sequences
Floating Point Numbers.  Floating point numbers are real numbers.  In Java, this just means any numbers that aren’t integers (whole numbers)  For example…
Numerical Expressions
Python Programming: An Introduction to Computer Science
Floating point numbers in Python Floats in Python are platform dependent, but usually equivalent to an IEEE bit C “double” However, because the significand.
Simple Data Type Representation and conversion of numbers
Vahé Karamian Python Programming CS-110 CHAPTER 3 Computing with Numbers.
Binary Real Numbers. Introduction Computers must be able to represent real numbers (numbers w/ fractions) Two different ways:  Fixed-point  Floating-point.
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Fixed-Point Arithmetics: Part II
Introduction to Numerical Analysis I
Computer Architecture
Floating Point Numbers Topics –IEEE Floating Point Standard –Rounding –Floating Point Operations –Mathematical properties.
Lecture Set 4 Data Types and Variables Part A – Introduction Numeric Data Types.
S.Ducasse Stéphane Ducasse 1 Processes and Concurrency in VW.
Round-off Errors.
Round-off Errors and Computer Arithmetic. The arithmetic performed by a calculator or computer is different from the arithmetic in algebra and calculus.
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
NUMBER SYSTEMS. Objectives  Identify some different number systems  Round-up numbers and correct them to significant figures  Carry out calculations.
Object-oriented Programming and Design 1 Polymorphism Poly => many Morph => shape Variables take on many shapes, or many classes of objects.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
The Real Number System Real Numbers Irrational Numbers
Simplified Radical Form Objective: 1)Describe simplified Radical Form 2)Simplify radical expressions by a) Factoring out perfect squares b) Combine Like.
B121 Chapter 5 Working with Numbers. Number representation ThousandHundredsTensUnits Natural numbers: 1,2,3,4,5……… Integers: Natural numbers.
Slide Copyright © 2009 Pearson Education, Inc. Unit 1 Number Theory MM-150 SURVEY OF MATHEMATICS – Jody Harris.
3C-1 Identifiers Variables Literal Objects  Numbers  Characters  Strings Control Structure  Blocks  Conditionals  Loops Messages Defining New Classes.
Negative binary numbers 1 Computer Architectures M.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
S.Ducasse Stéphane Ducasse 1 Smalltalk in a Nutshell.
Stéphane Ducasse 1 Abstract Classes.
DATA REPRESENTATION Data Types Complements Fixed Point Representations
COSC2410: LAB 2 BINARY ARITHMETIC SIGNED NUMBERS FLOATING POINT REPRESENTATION BOOLEAN ALGEBRA 1.
Floating Point Numbers
2.4. Floating Point Numbers
Number Systems and Binary Arithmetic
Chapter 4 – Fundamental Data Types
Topics IEEE Floating Point Standard Rounding Floating Point Operations
Tokens in C Keywords Identifiers Constants
Floating Point Numbers: x 10-18
CS 232: Computer Architecture II
IEEE floating point format
Chapter 6 Floating Point
Topic 3d Representation of Real Numbers
Lesson #1: Simplifying Radicals (For use with Sections 7-2 & 7-3)
CSCI206 - Computer Organization & Programming
Lectures on Numerical Methods
Data Types and Variables Part A – Introduction Numeric Data Types
Square Roots
Presentation transcript:

S.Ducasse Stéphane Ducasse 1 Numbers

S.Ducasse License: CC-Attribution-ShareAlike

S.Ducasse 3 Arithmetic 5 + 6, 5 - 6, 5 * 6, (division) 30 / 9, (integer division) 30 // 9, (modulo) 30 \\ 9, (square root) 9 sqrt, (square) 3 squared Rounding 3.8 ceiling -> 4, 3.8 floor -> 3, roundTo: > 3.81 Range 30 between: 5 and: 40 Basic 3

S.Ducasse 4 Basic (2) Tests 3.8 isInteger, 3.8 even, 3.8 odd Signs positive, negative, sign, negated Other min:, max:, cos, ln, log, log: arcSin, exp, **

S.Ducasse 5 Smalltalk offers: automatic coercion of numbers infinite number exact precision (7/11) + (4 /11) -> 1 SmallInteger maxValue Biggest small integer Smallest large number: SmallInteger maxValue + 1: Automatic Coercion

S.Ducasse 6 Large Numbers 1000 factorial (try it) factorial /999 factorial -> 1000

S.Ducasse 7 ArithmeticValue>>coerce: aNumber "Answer a number representing the argument, aNumber, that is the same kind of Number as the receiver. Must be defined by all Number classes." ^ self subclassResponsibility ArithmicValue>>generality "Answer the number representing the ordering of the receiver in the generality hierarchy. A number in this hierarchy coerces to numbers higher in hierarchy (i.e., with larger generality numbers)." ^ self subclassResponsibility In VW Coercion & Generality

S.Ducasse 8 In VW Coercion & Generality Integer>>coerce: aNumber "Convert a number to a compatible form aNumber asInteger Integer>>generality ^40 Generality: SmallInt 20, Integer 40, Fraction 60, FixedPoint 70, Float 80, Double 90

S.Ducasse 9 ArithmeticValue>>sumFromInteger: anInteger "The argument anInteger, known to be a kind of integer, encountered a problem on addition. Retry by coercing either anInteger or self, whichever is the less general arithmetic value." ^anInteger retry: #+ coercing: self ArithmeticValue>>retry: aSymbol coercing: aNumber "Arithmetic represented by the symbol, aSymbol, could not be performed with the receiver and the argument, aNumber, because of the differences in representation. Coerce either the receiver or the argument, depending on which has higher generality, and try again. If the generalities are the same, then this message should not have been sent so an error notification is provided." self generality < aNumber generality ifTrue: [^(aNumber coerce: self) perform: aSymbol with: aNumber]. self generality > aNumber generality ifTrue: [^self perform: aSymbol with: (self coerce: aNumber)]. self error: 'coercion attempt failed' #retry:coercing: in VW

S.Ducasse 10 adaptToInteger:... in Squeak Integer>>+ aNumber "Refer to the comment in Number + " aNumber isInteger ifTrue: [self negative == aNumber negative ifTrue: [^ (self digitAdd: aNumber) normalize] ifFalse: [^ self digitSubtract: aNumber]]. ^ aNumber adaptToInteger: self andSend: #+ Number>>adaptToInteger: rcvr andSend: selector "If I am involved in arithmetic with a Integer, convert us and evaluate exprBlock." ^ self subclassResponsibility Float>>adaptToInteger: rcvr andSend: selector "If I am involved in arithmetic with an Integer, convert it to a Float." ^ rcvr asFloat perform: selector with: self Fraction>>adaptToInteger: rcvr andSend: selector "If I am involved in arithmetic with an Integer, convert it to a Fraction." ^ rcvr asFraction perform: selector with: self

S.Ducasse 11 asFloat Float>>asFloat "Answer the receiver itself." ^self Fraction>>asFloat "Answer a Float that closely approximates the value of the receiver. Ideally, answer the Float that most closely approximates the receiver.".... Integer>>asFloat "Answer a Float that represents the value of the receiver. Optimized to process only the significant digits of a LargeInteger”....

S.Ducasse 12 asFraction Fraction>>asFraction "Answer the receiver itself." ^self Integer>>asFraction "Answer a Fraction that represents value of the the receiver." ^Fraction numerator: self denominator: Float>>+ aNumber "Primitive. Answer the sum of the receiver and aNumber. Essential. Fail if the argument is not a Float. See Object documentation whatIsAPrimitive." ^ aNumber adaptToFloat: self andSend: #+

S.Ducasse 13 SmallInteger are objects in Smalltalk No primitives types But heavily optimized by the virtual machine tagged integer for integers and other primitive types one bit indicate that their value is coded differently than normal objects About Pure OO

S.Ducasse 14 Summary Automatic coercion Number kind addition