Home

How to Create a Basic Math Template in C++

|
Updated:  
2016-03-26 08:29:42
|
From The Book:  
C++ Essentials For Dummies
Explore Book
Buy On Amazon

With a math template, you usually need access to a wealth of calculations but may only use one or two of those calculations at a time. For example, if someone is calculating your mortgage, he or she doesn’t need to know the amortization calculation. However, the person might need the amortization calculation when working with the next customer.

In short, the calculations all have a purpose and you need them all, but you don’t need them all at the same time. Because of the way you use math templates, they work best as a series of function templates. The MathTemplate example shows how to create the series of functions.

#include <iostream>
#include <cmath>
using namespace std;
template<typename T>
T Area(T height, T length)
{
    return height * length;
}
const double PI = 4.0*atan(1.0);
template<typename T>
T CircleArea(T radius)
{
    double result;
    result = PI * radius * radius;
    // This version truncates the value.
    return (T)result;
}
template<typename T>
T TriangleArea(T base, T height)
{
    double result;
    result = base * height * 0.5;
    return (T)result;
}
int main()
{
    cout << "4 X 4 Areas:" << endl;
    cout << "Square: " << Area<int>(4, 4) << endl;
    cout << "Circle: " << CircleArea<int>(2) << endl;
    cout << "Triangle: " << TriangleArea<int>(4, 4) << endl;
    cout << "Using a value of pi of: " << PI << endl;
    return 0;
}

The calculations could consist of any math calculation — the point of the example is that using functions makes each of the calculations discrete, easy to use, and easy to manage. When you run this example, you see the following output:

4 X 4 Areas:
Square: 16
Circle: 12
Triangle: 8
Using a value of pi of: 3.14159

Note that CircleArea(2) uses half the value of the other calculations as input. That’s because you calculate the area of a circle using the equation pi x r2.

For consistency, you could change the circle equation to read like this:

radius = radius / 2;
result = PI * radius * radius;

Dividing the input by 2, essentially changing the diameter to a radius, means that you could call the equation using the same number as all the other area calculations: CircleArea(4). Whichever approach you choose, you need to document how the template works so that other developers know how to use it.

You should also note that the circle and triangle calculations perform a bit of type coercion to ensure that the user gets the expected results back by modifying the return statement to read return (T)result;. The type conversions are needed to keep your templates from generating warning messages. It’s important to note that the approach used in the example truncates the result when the template returns an int.

About This Article

This article is from the book: 

About the book author:

John Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 100 books and more than 600 articles to date. The topics range from networking to home security and from database management to heads-down programming. John has provided technical services to both Data Based Advisor and Coast Compute magazines.

Jeff Cogswell has been an application developer and trainer for 18 years, working with clients from startups to Fortune 500 companies. He has developed courses on C++ and other technologies.