Download presentation
Presentation is loading. Please wait.
1
Hightlights C++ Template C++ Number Systems
2
C++ Template June 12th, 2012
3
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.
4
Template specialization
C++ Template Function templates Class templates Template specialization
5
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.
6
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
7
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.
8
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.
9
C++ Alternative Numerical Systems
10
Outline CGAL LEDA RATIO EXPERIMENTS
11
CGAL Quotient Rational type Exactness Testing
12
CGAL MP_Float Represent a floating point value with arbitrary precision (Not exact) Additions, subtractions and multiplications (Exact) Division Testing Quadratic complexity for multiplications
13
LEDA Leda_rational Two integer numbers which represent the numerator and the denominator Same as Quotient type in CGAL
14
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)
15
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
16
Test_1 Whether a line intersects, touches, or misses a circle
17
Test_1 Whether a line intersects, touches, or misses a circle d d
18
Test_2 Intersection of two line segments
19
Outline CORE/MPFI/RS/LiBRA/…
20
Ratio
21
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.
22
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.
23
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.
24
Integer Explanation. Structure. Construction. Operation.
25
Comparison of Ratio, TTCalc and Floats.
Experimental Results
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.