Hightlights C++ Template C++ Number Systems.

Slides:



Advertisements
Similar presentations
Goal When you have finished this presentation you should understand:
Advertisements

Introduction to Programming in C++ John Galletly.
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
College Algebra Fifth Edition
Mathematics By: Damarlyn Chivers Equations Definitions An equation is two things are the same, using mathematics symbols.
Classification of Numbers
12.1 – Simplifying Rational Expressions A rational expression is a quotient of polynomials. For any value or values of the variable that make the denominator.
Thinking Mathematically The Irrational Numbers. The set of irrational numbers is the set of number whose decimal representations are neither terminating.
Sullivan Algebra and Trigonometry: Section R.1 Real Numbers Objectives of this Section Classify Numbers Evaluate Numerical Expressions Work with Properties.
Simple Data Type Representation and conversion of numbers
Templates & Generic Programming Junaed Sattar November 12, 2008 Lecture 10.
Chapter 13. Binary Files In binary files we do not need to format data File streams include two member functions specifically designed to input and output.
Chapter 6: The Real Numbers and Their Representations
The root word of rational is ratio.
Copyright 2013, 2010, 2007, Pearson, Education, Inc. Section 5.3 The Rational Numbers.
Prime Factorization Prime Number - Has exactly 2 factors, 1 and the number itself Composite Number - Any whole number greater than 1 with more than 2 factors.
Chapter 3 Elementary Number Theory and Methods of Proof.
MATHS PROJECT NUMBER SYSTEMS BY BINDIYA GURUNG,CLASS IX.
Prerequisites: Fundamental Concepts of Algebra
Rational numbers. Whole numbers Whole numbers Rational numbers Whole numbers Natural numbers Integers / ¾ 18% A rational number.
Rational and Irrational Numbers. Standards: Use properties of rational and irrational numbers.  MGSE9–12.N.RN.2 Rewrite expressions involving radicals.
P.1 Real Numbers. 2 What You Should Learn Represent and classify real numbers. Order real numbers and use inequalities. Find the absolute values of real.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Fundamental Programming: Fundamental Programming Introduction to C++
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Copyright © 2009 Pearson Education, Inc. Chapter 5 Section 1 - Slide 1 Chapter 1 Number Theory and the Real Number System.
Rational Numbers Rational numbers are numbers that can be written as the quotient of two integers. In the form a/b , where a is any integer and b is.
Real Numbers Review #1. The numbers 4, 5, and 6 are called elements. S = {4, 5, 6} When we want to treat a collection of similar but distinct objects.
College Algebra Sixth Edition James Stewart Lothar Redlin Saleem Watson.
Chapter 6: The Real Numbers and Their Representations.
Properties for Real Numbers Rules that real numbers follow.
Chapter 2 Real Numbers and algebraic expressions ©2002 by R. Villar All Rights Reserved Re-engineered by Mistah Flynn 2015.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Copyright © Cengage Learning. All rights reserved. Fundamental Concepts of Algebra 1.1 Real Numbers.
4-8 Complex Numbers Today’s Objective: I can compute with complex numbers.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Number and Numerical Operations. Real Numbers Rational Numbers -Can be written as a fraction. -In decimal form, either terminates or repeats Examples:
Go Math Lesson 1.2A. Goal: 8.NS.1 Know that the numbers that are not rational are called irrational.
Templates יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום.
A Sample Program #include using namespace std; int main(void) { cout
Equivalent expressions are expressions that are the same, even though they may look a little different.
Lesson 5.3 The rational numbers. Rational numbers – set of all numbers which can be expressed in the form a/b, where a and b are integers and b is not.
Section 5.3 The Rational Numbers.
Appendix A Basic Algebra Review
1.1: Objectives Properties of Real Numbers
Exercise 1 – Datentypen & Variablen
Real Numbers Terms: Natural numbers: 1,2,3,4,…
Natural Numbers Natural numbers are counting numbers.
8.1 Multiplying and Dividing Rational Expressions
Chapter 6: The Real Numbers and Their Representations
School of EECS, Peking University
Subject : Algebra Std - 9th Subject- Algebra Topic- Real Numbers.
1-6 Real numbers and rational numbers
Pointers and Pointer-Based Strings
Rational & Irrational Numbers
Algebraic Fractions Dave Saha 15 Jan 99.
Precalculus Mathematics for Calculus Fifth Edition
Name: Rubaisha Rajpoot
The Real Numbers And Their Representations
Exponents & Radicals Day 3
Section 5.3 The Rational Numbers
The Real Numbers And Their Representations
Copyright © Cengage Learning. All rights reserved.
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
Terminating and Repeating Decimals
Arab Open University Faculty of Computer Studies Dr
Chapter Sections 1.1 – Study Skills for Success in Mathematics
Exercise Use long division to find the quotient. 180 ÷ 15.
UNIT 1 Chapter 1-1 Number Systems.
Presentation transcript:

Hightlights C++ Template C++ Number Systems

C++ Template June 12th, 2012

C++ Template Function templates are special functions that can operate with generic types. In C++ this can be achieved using template parameters This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

Template specialization C++ Template Function templates Class templates Template specialization

As you can see, the function template GetMax returns the greater of two parameters of this still-undefined type. Function Template // function template #include <iostream> using namespace std; template <class T>T GetMax (T a, T b) { T result; result = (a>b)? a : b; return (result); } int main () { int i=5, j=6, k; float l=10.2, m=5.6, n; k=GetMax<int>(i,j); n=GetMax<float>(l,m); cout << k << endl; cout << n << endl; return 0; } Here we have created a template function with T as its template parameter. This template parameter represents a type that has not yet been specified, but that can be used in the template function as if it were a regular type. As you can see, the function template GetMax returns the greater of two parameters of this still-undefined type.

As you can see, the function template GetMax returns the greater of two parameters of this still-undefined type. Function Template // function template #include <iostream> using namespace std; template <class T>T GetMax (T a, T b) { return (a>b? a : b); } int main () { int i=5, j=6, k; float l=10.2, m=5.6, n; k=GetMax(i,j); n=GetMax(l,m); cout << k << endl; cout << n << endl; return 0; } the compiler can find out automatically which data type has to instantiate without having to explicitly specify it within angle brackets

Class Template // class templates #include <iostream> As you can see, the function template GetMax returns the greater of two parameters of this still-undefined type. Class Template // class templates #include <iostream> using namespace std; template <class T>class mypair { T a, b; public: mypair (T first, T second) a=first; b=second; } T getmax (); }; template <class T> T mypair<T>::getmax () { T retval; retval = a>b? a : b; return retval; } int main () mypair <int> myobject (100, 75); cout << myobject.getmax(); return 0; We also have the possibility to write class templates, so that a class can have members that use template parameters as types.

Template Specialization As you can see, the function template GetMax returns the greater of two parameters of this still-undefined type. Template Specialization // class templates template <class T>class mycontainer { T element; public: mycontainer (T arg) {element=arg;} T increase () {return ++element;} }; //class template specialization: template <> class mycontainer <char> char element; mycontainer (char arg) {element=arg;} char uppercase () if ((element>='a')&&(element<='z')) element+='A'-'a'; return element; } }; int main () { mycontainer<int> myint (7); mycontainer<char> mychar ('j'); cout << myint.increase() << endl; cout << mychar.uppercase() << endl; return 0; } If we want to define a different implementation for a template when a specific type is passed as template parameter, we can declare a specialization of that template. For example, let's suppose that we have a very simple class called mycontainer that can store one element of any type and that it has just one member function called increase, which increases its value. But we find that when it stores an element of type char it would be more convenient to have a completely different implementation with a function member uppercase, so we decide to declare a class template specialization for that type.

C++ Alternative Numerical Systems

Outline CGAL LEDA RATIO EXPERIMENTS

CGAL Quotient Rational type Exactness Testing

CGAL MP_Float Represent a floating point value with arbitrary precision (Not exact) Additions, subtractions and multiplications (Exact) Division Testing Quadratic complexity for multiplications

LEDA Leda_rational Two integer numbers which represent the numerator and the denominator Same as Quotient type in CGAL

LEDA Leda_bigfloat In general a bigfloat is given by two integers s and e where s is the significant and e is the exponent. The tuple (s, e) represents the real number Support conversion to rational: rational x.to_rational() //converts x into a number of type rational. (nearest simple ratio algorithm)

LEDA Leda_real A real is represented by the expression which defines it and an interval inclusion I that contains the exact value of the real. Interval technique: int RN.sign() Two tests

Test_1 Whether a line intersects, touches, or misses a circle

Test_1 Whether a line intersects, touches, or misses a circle d d

Test_2 Intersection of two line segments

Outline CORE/MPFI/RS/LiBRA/…

Ratio

Introduction ‘Ratio’ is an infinite precision C++ number system implementation. It can represent any rational number. The set of rational numbers are closed under arithmetic operations. The above features guarantee that any geometric predicate implementation using only ‘Ratio’ numbers will be robust.

Rational Numbers Definition A rational number is any number that can be expressed as the quotient or fraction p/q of two integers, with the denominator q not equal to zero. A real number that is not rational is called irrational. The decimal expansion of an irrational number continues forever without repeating.

Rational Numbers From an implementation standpoint, we can achieve infinite precision for rational numbers if we can develop a system that can hold arbitrarily large integers. ‘Ratio’ is capable of storing arbitrarily long integers and defines operations on them.

Integer Explanation. Structure. Construction. Operation.

Comparison of Ratio, TTCalc and Floats. Experimental Results