Numerical Functions & Tricks

Slides:



Advertisements
Similar presentations
Rational Numbers and Opposites Enter the fraction,, as a quotient, and choose to have your answer displayed as a fraction by selecting ► FRAC, option 1,
Advertisements

Types and Arithmetic Operators
DATA REPRESENTATION CONVERSION.
Arithmetic & Logic Unit Does the calculations Everything else in the computer is there to service this unit Handles integers May handle floating point.
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.
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
Connecting with Computer Science 2 Objectives Learn why numbering systems are important to understand Refresh your knowledge of powers of numbers Learn.
Storing data Getting data out Data types Ruby as a foundation Program Variables Click icon to hear comments (the download may take a minute)
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
Python Types Python values are of various “types” Ints, Floats, Strings, Characters, and more Two representations of numbers 1 vs 1.0.
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Computer Systems Architecture Copyright © Genetic Computer School 2008 CSA 1- 0 Lesson 1 Number System.
Numerical Functions & Tricks In today’s lesson we will look at: why we might want to use mathematical techniques some useful functions other mathematical.
Arithmetic Expressions Russell Taylor NC Computing Software Application Development.
Dividing a Decimal by a Decimal. Dividing Whole Numbers 12 ÷ 2 = 120 ÷ 20 = 1200 ÷ 200 = ÷ 2000 = Multiply both 12 and 2 by 10 Multiply.
B121 Chapter 5 Working with Numbers. Number representation ThousandHundredsTensUnits Natural numbers: 1,2,3,4,5……… Integers: Natural numbers.
Dr Mohamed Menacer College of Computer Science and Engineering Taibah University CE-321: Computer.
Doing math In java.
3 Basics © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Chapter 9 Computer Arithmetic
William Stallings Computer Organization and Architecture 8th Edition
Introduction To Number Systems
Unit 2.6 Data Representation Lesson 1 ‒ Numbers
Lecture 3: Logic Bryan Burlingame 06 Sept 2017.
Computer Science 210 Computer Organization
Imaginary & Complex Numbers
Section 2: Integer & Floating Point Numbers
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CSE 102 Introduction to Computer Engineering
2.0 COMPUTER SYSTEM 2.2 Number System and Representation
William Stallings Computer Organization and Architecture 7th Edition
Data Structures Mohammed Thajeel To the second year students
Computer Organization and ASSEMBLY LANGUAGE
Unit 42 : Spreadsheet Modelling
Math in C The math blocks you've used in Scratch can all be recreated in C!
Arithmetic operations, decisions and looping
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Modular Arithmetic In today’s lesson we will:
Unit 2.6 Data Representation Lesson 1 ‒ Numbers
Fundamentals of Programming I Number Systems
Computer Science 210 Computer Organization
Introduction to Java, and DrJava part 1
Arithmetic Expressions
Arithmetic Expressions & Data Conversions
ECEG-3202 Computer Architecture and Organization
Division Properties of Exponents
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Introduction to Java, and DrJava
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 8 Computer Arithmetic
CHAPTER 3: String And Numeric Data In Python
Fractions, Decimals and Percentages
Prof. Giancarlo Succi, Ph.D., P.Eng.
Introduction to Java, and DrJava
Introduction to Java, and DrJava part 1
Division Properties of Exponents
Class code for pythonroom.com cchsp2cs
Arithmetic Expressions & Data Conversions
Introduction to Python
Presentation transcript:

Numerical Functions & Tricks In today’s lesson we will look at: why we might want to use mathematical techniques some useful functions other mathematical ideas

Why Manipulate Numbers? Computers are better at handling whole numbers (integers), and you can easily get rounding errors when working with long decimal numbers. You might need to write a program that performs a calculation – e.g. for a bank or insurance company You can use mathematical techniques for more interesting things, such as motion, or for cycling through a limited number of colours (like on the Spots theme for the Ill Health Team web-site)

Useful Operations Modulo arithmetic: performs a division and gives you the remainder – e.g. 10 % 3 returns 1 Modulo arithmetic is useful if you want to cycle through a fixed number of options, e.g. colour = (“Red”, “White”, “Blue”) for n in range(20): print(str(n) + “ – “ + colour[n%3])

Useful Operations Powers/Indices: to raise a number to a power, we use the ** symbols For example, 4**3 would calculate 43 There is no native square root function, but if you remember your indices work from Maths, you can use negative numbers and fractions, e.g. 25**0.5 would give you the square root.

Useful Functions abs(): gives you the absolute value of a number - i.e. without the sign. For example, abs(-1) would return 1 float(): gives you the numerical value of a string, e.g. float(“2.5”) would return the value 2.5 as a float. int(): gives you the integer value of a string, e.g. int(“3”) would return the value 3 as an int. str(): turns a number into a string, e.g. str(10) would would return a str containing “10”. eval() evaluates the expression in a string, e.g. eval(“2+3”) would return 5 (as an int)

Rounding round() rounds a number in the same way that you would in Maths You can round to a specified number of decimal places – e.g. round(1.2345,0) would give 1 round(1.2345,1) would give 1.2 etc.

Tricks & Tips You can change the sign of a number by multiplying by -1 You can use this idea to make things bounce – e.g. multiply their speed by -1 so that they go the other way, Computers are much better at working with whole numbers, so for things like money it is better to work in pence, and just divide by 100 when you want to display the answer

Rounding Errors When performing floating point operations on computers, you quite often get strange results: http://www.advanced-ict.info/mathematics/sf_original.html This isn’t a bug in JavaScript – Python will do the same – it’s the way the computer handles floating point numbers. It’s better, therefore, to avoid floats where possible – e.g. if you are writing a program to handle money, do all of your calculations in pence rather than pounds.

Further Reading Boolean Operators – AND, OR, NOT, EOR Binary – representing numbers using only 0s and 1s Hexadecimal – used to describe colours Bitwise Boolean Operators: these take the Boolean Logical Operators and apply them to individual bits in the binary representation of numbers – this is how the binary flags page works.