 Math Functions

 The Math Libraries

 Introduction
 The controls on your applications will receive strings of various kinds either supplied by the user or gotten from other controls. Some of the values on these controls will be involved in mathematical operations. The C++ language provides a rich set of functions to help you quickly perform different types of calculations. The functions range from arithmetic to geometry, from trigonometry to algebra, etc. To compensate for the areas where C++ does not expand, instead of writing your own functions, The Visual Component Library (VCL) is equipped with various functions that, besides geometry and algebra, deal with finance, statistics, random number generation, etc. Because there are so many of these functions and they get added with each new release of the library, we will review only the most common used.

By default, the content of a text control, such as an edit box, is a string, which is an array of characters. If you want the value or content of such a control to participate in a mathematical operation, you must first convert such a value to a mathematically compatible value.

 String to Integer Conversion

If you want to use the value of a string as an integer, you can use the AnsiString::ToInt() method. Its syntax is:

`int __fastcall ToInt() const;`

This member function converts an AnsiString variable to a valid integer. In the following example, the contents of two edit boxes are converted to integers and a subtraction is performed:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int Number1 = Edit1->Text.ToInt();
int Number2 = Edit2->Text.ToInt();
int Subtract = Number1 - Number2;

Edit3->Text = Subtract;
}
//---------------------------------------------------------------------------```

If the control whose string needs to be converted is displaying an invalid integer, the program would throw an error. The AnsiString provides a viable alternative. The AnsiString::ToIntDef() method allows you to supply a default value if the conversion fails. Here is an example that uses it:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int Value1 = Edit1->Text.ToIntDef(0);
int Value2 = Edit2->Text.ToIntDef(1);

int Remainder = Value1 % Value2;
Edit3->Text = Remainder;
}
//---------------------------------------------------------------------------```

A function used to convert a string is the StrToInt() function. Its syntax is:

`int __fastcall StrToInt(const AnsiString S);`

This function takes as an argument the string that you are trying to convert. In the following example, the strings of two edit boxes are converted to integers and an addition is performed on their values:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int Number1 = StrToInt(Edit1->Text);
int Number2 = StrToInt(Edit2->Text);

int Addition = Number1 + Number2;
}
//---------------------------------------------------------------------------```
 Integer to String Conversion

To convert an integer to an AnsiString, you can use the IntToStr() function. Its syntax is

`AnsiString __fastcall IntToStr(int Value);`

or

`AnsiString __fastcall IntToStr(__int64 Value);`

This function takes an integer as the argument and returns a string.

 String to Floating-Point Conversion

To convert the value of a string to floating number, use the AnsiString::ToDouble() method. Its syntax is:

`double __fastcall ToDouble() const;`

Here is an example:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
double Value1 = Edit1->Text.ToDouble();
double Value2 = Edit2->Text.ToDouble();

double Value3 = Value1 + Value2;
Edit3->Text = Value3;
}
//---------------------------------------------------------------------------```

Another function used to convert a string to a floating-point value is the StrToFloat() function whose syntax is:

`Extended __fastcall StrToFloat(const AnsiString S);`

This function takes one argument, which is the string to convert. The function returns a long double-precision value. Here is an example:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
double Value1 = StrToFloat(Edit1->Text);
double Value2 = StrToFloat(Edit2->Text);

double Value3 = Value1 + Value2;
Edit3->Text = Value3;
}
//---------------------------------------------------------------------------```
 Number Formatting to Display Decimals

The AnsiString class is equipped with a method used to format a floating-point number and specify the number of decimal places. Actually, the AnsiString::sprintf() function imitates the C’s printf() string system of displaying strings, integers, and floating variables. In mathematical operations, you can use it to control how to display a decimal number. The syntax of the method is:

`AnsiString& __cdecl sprintf(const char* format, ...);`

Following the C system of passing arguments, this member function can at least display a string. Here is an example:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Edit2->Text = Edit2->Text.sprintf(L"The best movie of the year");
}
//---------------------------------------------------------------------------```

On the other hand, you can use this function to format a floating-point number. In the following example, the values of the dimensions of a sphere are calculated and display in appropriate edit boxes:

```//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalculateClick(TObject *Sender)
{
double Radius, Diameter, Circumference, Area, Volume;

Circumference = Radius * 2 * M_PI;

edtDiameter->Text = Diameter;
edtCircumference->Text = Circumference;
edtArea->Text = Area;
edtVolume->Text = Volume;
}
//---------------------------------------------------------------------------```  When the same values are configured using the AnsiString::sprintf() method, the diameter and the circumference can be set to display two decimal values while the area and the volume display three, as follows:

```//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalculateClick(TObject *Sender)
{
double Radius, Diameter, Circumference, Area, Volume;

Circumference = Radius * 2 * M_PI;

edtDiameter->Text = edtDiameter->Text.sprintf(L"%.2f", Diameter);
edtCircumference->Text = edtCircumference->Text.sprintf(L"%.2f", Circumference);
edtArea->Text = edtArea->Text.sprintf(L"%.3f", Area);
edtVolume->Text = edtVolume->Text.sprintf(L"%.3f", Volume);
}
//---------------------------------------------------------------------------```
 Arithmetic Functions

 Absolute Values

 The abs Function

The decimal numeric system counts from minus infinity to infinity. This means that numbers are usually negative or positive, depending on their position from 0, which is considered as neutral. In some operations, the number considered will need to be only positive even if it is provided in a negative format. The absolute value of a number x is x if the number is (already) positive. If the number is negative, its absolute value is its positive equivalent. For example, the absolute value of 12 is 12, while the absolute value of –12 is 12.

To get the absolute value of a number, you can use one of the C/C++ abs() function. Its syntax is:

`int abs(int x);`

This function takes an integer as the argument and returns its absolute value equivalent. Here is an example:

```//---------------------------------------------------------------------------
void __fastcall TForm1::btnAbsoluteClick(TObject *Sender)
{
int Number = Edit1->Text.ToInt();

Edit2->Text = abs(Number);
}
//---------------------------------------------------------------------------```
 The labs Function

If you want to find the absolute value of a number that is larger than the regular integer, you can use the labs() function. Its syntax is:

`long labs(long int x);`

This function takes a long integer as argument and returns its equivalent absolute value:

```//---------------------------------------------------------------------------
void __fastcall TForm1::btnLongAbsoluteClick(TObject *Sender)
{
int Longer = StrToInt(edtNumber->Text);

edtResult->Text = labs(Longer);
}
//---------------------------------------------------------------------------```
 The Ceiling of a Number

Consider a floating number such as 12.155. As you can see, this number is between integer 12 and integer 13 In the same way, consider a number such as –24.06. As this number is negative, it is between –24 and –25, with –24 being greater.

In arithmetic, the ceiling of a number is the closest integer that is greater or higher than the number considered. In the first case, the ceiling of 12.155 is 13 because 13 is the closest integer greater than or equal to 12.155. The ceiling of –24.06 is –24.

 The ceil() Function

In C++, the function used to obtain the ceiling of a number uses the following syntax:

`double ceil(double Value);`

The function takes one argument, which is the floating number to be evaluated, and the function returns a double-precision number that is the integer that is greater than or equal to Value. Here is an example:

```//---------------------------------------------------------------------------
#include <iostream>
using namespace std;

#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsusedint main(int argc, char* argv[])
{
double Value1 = 155.55; double Value2 = -24.06;

cout << "The ceiling of " << Value1 << " is " << ceil(Value1) << endl;
cout << "The ceiling of " << Value2 << " is " << ceil(Value2) << endl;

cout << "\nPress any key to continue...";
getchar();
return 0;
}
//---------------------------------------------------------------------------```

This would produce:

```The ceiling of -24.06 is -24
Press any key to continue...```
 The Ceil() Function

In C++ Builder, the function used to get the ceiling of a number is:

`int __fastcall Ceil(Extended Value);`

The Ceil() function takes an argument that represents a long double value. The function returns the greater or equal integer of Value. To use the Ceil() function, include the math.hpp header to your program. Here is an example:

```//---------------------------------------------------------------------------
#include <iostream.h>
#include <math.hpp>

#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused

int main(int argc, char* argv[])
{
Extended Value1 = 312.44;
Extended Value2 = -4002.35;

cout << "The ceiling of " << Value1 << " is" << Ceil(Value1) << endl;
cout << "The ceiling of " << Value2 << " is" << Ceil(Value2);

cout << "\n\nPress any key to continue...";
getchar();
return 0;
}
//---------------------------------------------------------------------------```

This would produce:

```The ceiling of 312.44 is 313
The ceiling of -4002.35 is -4002

Press any key to continue...```
 The Floor of a Number

Consider two floating numbers such as 128.44 and -36.72. The number 128.44 is between 128 and 129 with 128 being the lower. The number –36.72 is between –37 and –36 with –37 being the lower. The lowest but closest integer value of a number is referred to as its floor. Based on this, the floor of 128.44 is 128. The floor of –36.72 is –37.

 The floor() Function

In C++, to obtain the floor of a number, use the following function:

`double floor(double Value);`

The floor() function takes the considered value as the argument and returns the integer that is less than or equal to Value. Here is an example:

```double Value1 = 1540.25;
double Value2 = -360.04;

cout << "The floor of " << Value1 << " is " << floor(Value1) << endl;
cout << "The floor of " << Value2 << " is " << floor(Value2) << endl;```

This would produce:

```The floor of 1540.25 is 1540
The floor of -360.04 is -361

Press any key to continue...```
 The Floor() Function

When using C++ Builder, you can use the Floor() function to find the floor of a number. The syntax of the function is:

`int __fastcall Floor(Extended Value);`

The Value argument of the function represents the number that is being considered. The function returns the integer that is less than or equal to Value. Here is an example:

```Extended Value1 = 312.44;
Extended Value2 = -4002.35;

cout << "The floor of " << Value1 << " is " << Floor(Value1) << endl;
cout << "The floor of " << Value2 << " is " << Floor(Value2) << endl;```

This would produce:

```The floor of 312.44 is 312
The floor of -4002.35 is -4003

Press any key to continue...```
 The Exponent of a Number

 The frexp() and the frexpl() Functions
```double frexp(double Number, int *Exp);
long double frexpl(long double Number, int *Exp);```

The C++ frexp() and frexpl() functions are used to get the mantissa and the exponent portions of a floating-point number. Each of these functions takes two arguments. The Number argument represents the value that will be examined. For the frexp() function, this value is a double-precision number. If the number is larger, then use the frexpl() version whose argument is a long double. The Exp argument is passed as a pointer to an integer. This allows the function to return a second value.

After execution, the function returns the mantissa such that:

`Mantissa = frexp(Number, Exp);`

The result returned, Mantissa, is a double (frexp) or a long double (frexpl) number in the range 0.5 (included) to 1 (excluded). The Exp argument, passed as a pointer, is returned as

`Number = Mantissa * 2Exp`

For the following example, a form is equipped with three Edit controls named edtNumber, edtMantissa, and edtExponent. It also has a Button control named btnCalculate with the Default property set to true. The user must type a number in the Number edit box and press Enter. Then the OnClick event of the button executes to perform the frexp() function which leads to displaying the results in the appropriate edit boxes :

```//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalculateClick(TObject *Sender)
{
double Mant, Number = edtNumber->Text.ToDouble();
int Exp; Mant = frexp(Number, &Exp);

edtMantissa->Text = Mant;
edtExponent->Text = Exp;
}//---------------------------------------------------------------------------```  The Frexp() Function
`void __fastcall Frexp(Extended Number, Extended &Mantissa, int &Exp);`

The Frexp() function is the VCL’s version of the frexp() function. This function takes three arguments. The number to be examined is the Number argument passed as a long double. The number to be returned, also a long double, is the Mnatissa argument, also passed by reference. The Exp argument, also passed as a reference, is returned as the exponent value. The numbers are dealt with according to the formula:

`Number = Mantissa * 2Exp`
```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Extended Number, Mant;
int Exp;

Number = StrToFloat(Edit1->Text);
Frexp(Number, Mant, Exp);

Edit2->Text = FloatToStr(Mant);
Edit3->Text = Exp;
}
//---------------------------------------------------------------------------```
 The Power of a Number

 The C++ pow() Functions
```double pow(double Source, double Raise);
long double powl(long double Source, long double Raise);```

The pow() function is used to calculate the value of one number or expression raised to the power of another number. This follows the formula:

`ReturnValue = xy`

The pow() function takes two required arguments. The first argument, x, is used as the base number to be evaluated. The second argument, y, also called the exponent, will raise x to this value. The powl() function performs the same calculation on long double numbers and returns a long double.

In the following example, a form is equipped with a Button control and an Edit control. When the user clicks the button, the constant 205.38 is raised to the power of 4.12. The result displays in the edit box:

```//---------------------------------------------------------------------------
#include <vcl.h>
#include <math.h>

#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------

#pragma package(smart_init)
#pragma resource "*.dfm"

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
const double Source = 205.38;
const double Exp = 4.12;

double Result = pow(Source, Exp);
Edit1->Text = Result;
}
//---------------------------------------------------------------------------```
 The IntPower() function
`Extended __fastcall IntPower(Extended Base, int Exponent);`

The VCL’s IntPower() function is used to raise a number, Base, to the integral Exponent power. The first argument of this function, Base, can be an integer, a float, a double-precision number or a long double. The Exponent argument is the factor about which the Base number will be raised.

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Extended Number, Base;
int Exp;

Base   = StrToFloat(Edit1->Text);
Exp    = StrToInt(Edit2->Text);
Number = IntPower(Base, Exp);

Edit3->Text = FloatToStr(Number);
}
//---------------------------------------------------------------------------```
 The Power() Function
`Extended __fastcall Power(Extended Base, Extended Exponent);`

The Power() function takes a number (any number, including integers, floating, double or long double-precision numbers) as the Base argument and raises it to the power of the Exponent argument, which also can be any number (int, float, double, long double).

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int Source = 205;
float Exp = 5.25;

double Result = Power(Source, Exp);
Edit1->Text = Result;
}
//---------------------------------------------------------------------------```
 The Exponential
`double exp(double x);`

The exp() function calculates the exponential value of a number. The argument, a double-precision value, represents the number to be evaluated.

If the value of x is less than -708.395996093 (approximately), the result is reset to 0 and qualifies as underflow. If the value of the argument x is greater than 709.78222656 (approximately), the result is INF and qualified as overflow:

```//---------------------------------------------------------------------------
#include <iostream.h>

#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsusedint main(int argc, char* argv[])
{
cout << "\nThe exponential of " << 709.78222656 << " is " << exp(709.78222656);

cout << "\n\nPress any key to continue...";
getchar();
return 0;
}
//---------------------------------------------------------------------------```

Therefore, the value of the argument should be between these two extremes. For a larger number, use the expl() function:

`long double expl(long double x);`

As opposed to an 8-byte value, this version of the function takes a 10-byte variable, calculates its exponent, and returns a long double.

 The ldexp Function
```double ldexp(double x, int y);
long double ldexpl(long double x, int y);```

The C/C++ ldexp() function takes the mantissa and the exponent numbers and returns a floating number. The function uses the formula:

`Result = x * 2y`

Here is an example:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
double x, Result;
int y;

x = StrToFloat(Edit1->Text);
y = StrToInt(Edit2->Text);
Result = ldexp(x, y);

Edit3->Text = FloatToStr(Result);
}
//---------------------------------------------------------------------------```

The ldexp() function works on double-precision numbers while the ldexpl() uses long doubles.

 The Ldexp() Function
`Extended __fastcall Ldexp(Extended X, int P);`

The VCL’s Ldexp() function is used to calculate a number that is derived from a known mantissa and an exponent numbers. To perform this calculation, the function uses the formula:

`Result = X * 2P`
```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
float Source = 450.04;
float Exp = 10.25;

double Result = Ldexp(Source, Exp);
Edit1->Text = Result;
}
//---------------------------------------------------------------------------```
 LnXP1

The LnXP1() function is used to calculate the natural logarithm of a number that is being incremented to 1. The syntax of this function is

`Extended __fastcall LnXP1(Extended X);`

When executing, this function takes one argument, X, adds 1 to X, and then calculates the natural logarithm, also called the Napierian logarithm, of the new number. The formula used is

`Result = ln(X+1)`

Here is an example:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Extended X, Result;

X = StrToFloat(Edit1->Text);
Result = LnXP1(X);
Edit2->Text = FloatToStr(Result);
}
//---------------------------------------------------------------------------```
 Log10

The Log10() function calculates the base 10 logarithm of a number. The syntax of this function is:

`Extended __fastcall Log10(Extended X);`

The number to be evaluated is passed as the argument X. The function returns the logarithm on base 10 using the formula:

`y = log10x`

which is equivalent to

`x = 10y`

Here is an example:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Extended X, Result;

X = StrToFloat(Edit1->Text);
Result = Log10(X);
Edit2->Text = FloatToStr(Result);
}
//---------------------------------------------------------------------------```
 Log2

The Log2() function is used to calculate the logarithm of a number on base 2. The syntax of the function is:

`Extended __fastcall Log2(Extended X);`

The variable whose logarithmic value will be calculated is passed as argument X to the function. The function uses the formula:

`Y = log2x`

This is the same as

```x = 2y

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Extended X, Result;

X = StrToFloat(Edit1->Text);
Result = Log2(X);
Edit2->Text = FloatToStr(Result);
}
//---------------------------------------------------------------------------```
 LogN

The LogN() function is used to calculate the logarithmic value of a number to the desired base. Its syntax is:

`Extended __fastcall LogN(Extended Base, Extended Number);`

This function takes two arguments. The second, Number, is the variable whose value will be evaluated. The first argument, Base, is used as the base of the logarithm. The formula used by this function is:

`y = logbx`

which is the same as

`x = by`
```For the LogN() function, this formula would be:

LogN(Base, Number) = NumberBase;

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Extended Base, Number, Result;

Base = StrToFloat(Edit1->Text);
Number = StrToFloat(Edit2->Text);
Result = LogN(Base, Number);

Edit3->Text = FloatToStr(Result);
}
//---------------------------------------------------------------------------```
 The Square Root

There are two forms of calculating the square root of a real positive number. When using any of these functions, make sure you include the math.h header file to your project.

The sqrt() function is used to calculate the square root of a double-precision number. Its syntax is:

`double sqrt(double x);`

This function takes one argument as a positive floating number. After the calculation, the function returns the square root of x:

```//---------------------------------------------------------------------------
#include <vcl.h>
#include <math.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
double Number = Edit1->Text.ToDouble();
double Result = sqrt(Number);

Edit2->Text = Result;
}
//---------------------------------------------------------------------------``` For a large or larger number, you can use the sqrtl() function. Its syntax is:

`long double sqrtl(long double x);`

This second form takes a long double number as a variable and returns a long double number as the square root of x.

After the calculation, if the function succeeds, it would return the square root. If it fails, it would throw an error.

 Introduction

An asset is an object of value. It could be a person, a car, a piece of jewelry, or a refrigerator, etc. Anything that has a value is an asset. In the accounting world, an asset is a piece of/or property whose life span can be projected, estimated, or evaluated. As days, months or years go by, the value of such an asset degrades.

When an item is acquired for the first time as “brand new”, the value of the asset is referred to as its Cost. The declining value of an asset is referred to as its Depreciation. At one time, the item will completely lose its worth or productive value. Nevertheless, the value that an asset has after it has lost all its value is referred to its Salvage Value. At any time, between the purchase value and the salvage value, accountants estimate the value of an item based on various factors including its original value, its lifetime, its usefulness (how the item is being used), etc.

 Double Declining Balance

The Double Declining Balance is a technique used to calculate the depreciating value of an asset. The function used to perform this calculation is the DoubleDecliningBalance() and its syntax is:

```Extended __fastcall DoubleDecliningBalance(Extended Cost,
Extended Salvage,
int Life,
int Period);```

The first parameter, Cost, represents the initial value of the item. The Salvage parameter is the estimated value of the asset when it will have lost all its productive value. The Cost and the Salvage values must be given in a monetary value. The value of Life is the length of the lifetime of the item; this could be the number of months for a car or the number of years for a house, for example. The Period is a factor for which the depreciation is calculated. For the DoubleDecliningBalance, this Period argument is usually 2.

In the following example, a form is equipped with five TEdit controls named edtCost, edtSalvage, edtLife, edtPeriod, and edtDepreciation. After entering the necessay values and pressing Enter, the OnClick event of the Calculate button retrieves the values and calls the DoubleDecliningBalnace() function to calculate the Depreciation and display it the appropriate edit box:  ```//---------------------------------------------------------------------------
#include <vcl.h>
#include <math.hpp>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------

#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{

}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalculateClick(TObject *Sender)
{
Extended Cost = StrToFloat(edtCost->Text);
Extended Salvage = StrToFloat(edtSalvage->Text);
Integer Life = StrToInt(edtLife->Text);
Integer Period = StrToInt(edtPeriod->Text);

Extended Depreciation = DoubleDecliningBalance(Cost, Salvage, Life, Period);

edtDepreciation->Text = FloatToStr(Depreciation);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnExitClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------```
 Straight Line Depreciation

The VCL provides another function used to calculate the depreciation of an item. This time, the depreciation is considered on one period of the life of the item. The function used, SLNDepreciation(), is:

```Extended __fastcall SLNDepreciation(const Extended Cost,
const Extended Salvage,
int Life);```

The Cost argument is the original amount paid for an item (refrigerator, mechanics toolbox, high-volume printer, etc). The Life parameter represents the period during which the asset is (or was) useful; it is usually measured in years. The Salvage parameter, also called the scrap value, is the value that the item will have (or is having) at the end of Life.

To perform this operation, the VCL uses the following formula:  ```//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalculateClick(TObject *Sender)
{
Extended Depreciation, Cost, Salvage;
int Life;

Cost = StrToFloat(edtCost->Text);
Salvage = StrToFloat(edtSalvage->Text);
Life = StrToInt(edtLife->Text);
Depreciation = SLNDepreciation(Cost, Salvage, Life);

edtDepreciation->Text = FloatToStrF(Depreciation, ffCurrency, 8, 2);
}
//---------------------------------------------------------------------------```
 Sum of the Year Digits Depreciation

The Sum-Of-The-Years’-Digits provides another technique for calculating the depreciation of an item. Imagine that a restaurant bought a commercial refrigerator (“cold chamber”) for \$18,000 and wants to estimate its depreciation after 5 years using the Sum-Of-Years’-Digits technique. Each year is assigned a number, also called a tag, using a consecutive count. This means that the first year is appended 1, the second is 2, etc. This way, the depreciation is not uniformly applied to all years.

`Year => 1, 2, 3, 4, and 5.`

The total count is made for these tags. For our refrigerator example, this would be

`Sum = 1 + 2 + 3 + 4 + 5 = 15`

Each year is divided by this Sum, also called the sum of years, used as the common denominator: This is equivalent to 1. As you can see, the first year would have the lowest dividend (1/15 ≈ 0.0067) and the last year would have the highest (5/15 ≈ 0.33).

To calculate the depreciation for each year, the fractions (1/15 + 2/15 + 3/15 + 4/15 + 5/15) are reversed so that the depreciation of the first year is calculated based on the last fraction (the last year divided by the common denominator). Then the new fraction for each year is multiplied by the original price of the asset. This would produce:

 Year Fraction * Amount = Depreciation 1 5/15 * \$18,000.00 = \$6,000.00 2 4/15 * \$18,000.00 = \$4,800.00 3 3/15 * \$18,000.00 = \$3,600.00 4 2/15 * \$18,000.00 = \$2,400.00 5 1/15 * \$18,000.00 = \$1,200.00 Total Depreciation = \$18,000.00

The VCL function used to calculate the depreciation of an asset using the sum of the years is called SYDDepreciation() and its syntax is:

```Extended __fastcall SYDDepreciation(constExtended Cost,
const Extended Salvage,
int Life,
int Period);```

The Cost parameter is the original value of the item. In our example, this would be \$18,000. The Salvage parameter is the value the asset would have (or has) at the end of its useful life. The Life is the number of years of the asset would have a useful life. The Period is the particular period or rank of a Life portion; for example, if the Life of the depreciation is set to 5 (years), the Period could be any number between 1 and 5. If set to 1, the depreciation would be calculated for the first year. If the Period is set to 4, the depreciation would be calculated for the 4th year. You can also set the Period to a value higher than Life. For example, if Life is set to 5 but you pass 8 for the Period, the depreciation would be calculated for the 8th year. If the asset is worthless in the 8th year, the depreciation would be 0.

Here is an example: ```//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalculateClick(TObject *Sender)
{
Extended DeprecYear1, DeprecYearN, Cost, Salvage;
int Life;

Cost = StrToFloat(edtCost->Text);
Salvage = StrToFloat(edtSalvage->Text);
Life = StrToInt(edtLife->Text);
DeprecYear1 = SYDDepreciation(Cost, Salvage, Life, 1);
DeprecYearN = SYDDepreciation(Cost, Salvage, Life, Life);

edtYear1->Text = FloatToStrF(DeprecYear1, ffCurrency, 8, 2);
edtYearN->Text = FloatToStrF(DeprecYearN, ffCurrency, 8, 2);
}
//---------------------------------------------------------------------------```
 Finance Functions

 Introduction

The Visual Component Library provides a series of functions destined to perform various types of financially related operations. These functions use common factors depending on the value that is being calculated. Many of these functions deal with investments or loan financing.

The Present Value is the current value of an investment or a loan. For a savings account, a customer could pledge to make a set amount of deposit on a bank account every month. The initial value that the customer deposits or has in the account is the PresentValue as referenced in the VCL functions. The sign of the variable, when passed to a function, depends on the position of the customer. If the customer is making deposits, this value must be negative. If the customer is receiving money (lottery installment, family inheritance, etc), this value should be positive.

The Number Of Periods is the number of periods that make up a full cycle of a loan or an investment. This period could be the number of months of a year, which is 12; but it could be another length. This variable is passed as NPeriods. Suppose a customer is getting a car loan that would be financed in 5 years. This is equivalent to 5 * 12 = 60 months. In the same way, a cash loan can stretch from 0 to 18 months, a carpenter truck loan can have a life financing of 40 months, and a condominium can be financed for 15 years of 12 months plus an additional 8 months; this is equivalent to (15 * 12) + 8 = 188 months.

The Interest Rate is a fixed percent value applied during the life of the loan or the investment. The rate does not change during the length of the NPeriods. For deposits made in a savings account, because their payments are made monthly, the rate is divided by the number of periods (the NPeriods) of a year, which is 12. If an investment has an interest rate set at 14.50%, the Rate would be 14.50/12 = 1.208. Because the Rate is a percentage value, its actual value must be divided by 100 before passing it to the function. For a loan of 14.50% interest rate, this would be 14.50/12 = 1.208/100 = 0.012.

The Payment is the amount the customer will be paying. For a savings account where a customer has pledged to pay a certain amount in order to save a set (goal) amount, this would be the amount the customer would pay every month. If the customer is making payments (car loan, mortgage, deposits to a savings account), this value must be negative. If the customer is receiving money (lottery installment or annuity, family inheritance, etc), this value must be positive.

The Payment Time specifies whether the payment is made at the beginning or the end of the period. For a monthly payment, this could be the beginning or end of every month. The PaymentTime uses one of the values of the TPaymentTime enumerator. When passing this variable, select one of the members of the enumerator:

`enum TPaymentTime { ptEndOfPeriod, ptStartOfPeriod };`
 The Future Value of an Investment

The FutureValue() function is used to calculate the future value of an investment. The syntax of this function is:

```Extended __fastcall FutureValue(Extended Rate,
int NPeriods,
Extended Payment,
Extended PresentValue,
TPaymentTime PaymentTime);```

Here is an example: ```//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalculateClick(TObject *Sender)
{
Extended Present, Future, Payment, TheRate;
int Period;

Present = StrToFloat(edtPresent->Text);
Payment = StrToFloat(edtPayment->Text);
Period = StrToInt(edtPeriod->Text);
TheRate = StrToFloat(edtRate->Text) / 12;
double Rate = TheRate /100;
Future = FutureValue(Rate, Period, Payment, Present, ptEndOfPeriod);

edtFuture->Text = FloatToStrF(Future, ffCurrency, 8, 2);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnExitClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------```
 The Number of Periods of an Investment

The NumberOfPeriods() function calculates the number of periodic payments of an investment. Its syntax is:

```Extended __fastcall PeriodPayment(const Extended Rate,
int Period,
int NPeriods,
const Extended PresentValue,
const Extended FutureValue,
TPaymentTime PaymentTime);```

Here is an example: ```//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalculateClick(TObject *Sender)
{
Extended Present, Future, TheRate, Payments, NPeriod;

Present = StrToFloat(edtLoan->Text);
Future = StrToFloat(edtFuture->Text);
Payments = StrToFloat(edtPayments->Text);
TheRate = StrToFloat(edtRate->Text) / 12;

double Rate = TheRate / 100;
// Apply the function
NPeriod = NumberOfPeriods(Rate, -Payments, -Present,
Future, ptStartOfPeriod);
// Since the number of periods is really an integer, find its ceiling
Extended Actual = Ceil(NPeriod);
// Display the number of periods
edtNPeriods->Text = FloatToStr(Actual);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnExitClick(TObject *Sender)
{
PostQuitMessage(0);
}
//---------------------------------------------------------------------------```
 Making an Investment or Paying a Loan

The Payment() function is used to calculate the regular payment of an investment. Its syntax is:

```Extended __fastcall Payment(Extended Rate,
int NPeriods,
constExtended PresentValue,
const Extended FutureValue,
TPaymentTime PaymentTime);```

In the following examples, a customer is applying for a car loan. The car costs \$15500. It will be financed at 8.75% for 5 years. The dealer estimates that the car will have a value of \$2500 when it is paid off. The dialog box is used to calculate the monthly payment (the Payments edit box) that the customer will make every month: ```//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalculateClick(TObject *Sender)
{
Extended Present, Future, TheRate, Payments, NPeriod;

Present = StrToFloat(edtLoan->Text);
Future = StrToFloat(edtFuture->Text);
TheRate = StrToFloat(edtRate->Text) / 12;
NPeriod = StrToFloat(edtNPeriods->Text);

double Rate = TheRate / 100;
// Apply the function
Payments = Payment(Rate, NPeriod, -Present,
Future, ptStartOfPeriod);
// Display the payments
edtPayments->Text = FloatToStrF(Payments, ffCurrency, 8, 2);
}
//---------------------------------------------------------------------------```
 The Amount Paid as Principal

While the InterestPayment() function calculates the amount paid as interest for a loan, the PeriodPayment() function calculates the actual amount that applies to the balance of the loan. This is referred to as the principal. Its syntax is:

```Extended __fastcall PeriodPayment(const Extended Rate,
int Period,
int NPeriods,
const Extended PresentValue,
const Extended FutureValue,
TPaymentTime PaymentTime);```

Here is an example: ```//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalculateClick(TObject *Sender)
{
Extended Present, Future, TheRate, PPayment;
int Periods, NPeriod;

Present = StrToFloat(edtLoan->Text);
Future = StrToFloat(edtFuture->Text);
TheRate = StrToFloat(edtRate->Text) / 12;
Periods = StrToInt(edtPeriod->Text);
NPeriod = StrToInt(edtNPeriods->Text);
double Rate = TheRate / 100;

// Apply the function
PPayment = PeriodPayment(Rate, Periods, NPeriod,
-Present,
Future,
ptStartOfPeriod);
// Display the payment
edtPPMT->Text = FloatToStrF(PPayment, ffCurrency, 8, 2);
}
//---------------------------------------------------------------------------```
 The Present Value of an Investment

The PresentValue() function calculates the total amount that future investments are worth currently. Its syntax is:

```Extended __fastcall PresentValue(const Extended Rate,
int NPeriods,
const Extended Payment,
const Extended FutureValue,
TPaymentTime PaymentTime);```

Here is an example: ```//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalculateClick(TObject *Sender)
{
Extended Present, Future, TheRate, Payments;
int NPeriod;

Future = StrToFloat(edtFuture->Text);
Payments = StrToFloat(edtPayments->Text);
TheRate = StrToFloat(edtRate->Text) / 12;
NPeriod = StrToInt(edtNPeriods->Text);

double Rate = TheRate / 100;
// Apply the function
Present = PresentValue(Rate, NPeriod,
-Payments, -Future, ptStartOfPeriod);
// Display the payment
edtPresent->Text = FloatToStrF(Present, ffCurrency, 8, 2);
}
//---------------------------------------------------------------------------```
 The Amount Paid as Interest

The InterestPayment() function is used to calculate the amount paid as interest for a loan. Its syntax is:

```Extended __fastcall InterestPayment(const Extended Rate,
int Period,
int NPeriods,
const Extended PresentValue,
const Extended FutureValue,
TPaymentTime PaymentTime);```

The PresentValue parameter is the current value of the item. It could be the marked value of the car, the current mortgage value of a house, or the cash amount that a bank is lending. The NPeriods is the number of periods that occur during a yearly cycle of the loan. The Rate argument is a fixed percent value applied during the life of the loan. The Period argument represents the payment period. The FutureValue is the total amount that the customer will have paid when the loan is paid off. The PaymentTime specifies whether the periodic (such as monthly) payment of the loan is made at the beginning or end of the period.

Here is an example: ```//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalculateClick(TObject *Sender)
{
Extended Present, Future, Payment, TheRate;
int Periods, NPeriod;

Present = StrToFloat(edtPresent->Text);
Future = StrToFloat(edtFuture->Text);
Periods = StrToInt(edtPeriod->Text);
NPeriod = StrToInt(edtNPeriods->Text);
TheRate = StrToFloat(edtRate->Text) / 12;
double Rate = TheRate /100;

Payment = InterestPayment(Rate,
Periods,
NPeriod,
Present,
Future,
ptEndOfPeriod);
edtPayments->Text = FloatToStrF(Payment,
ffCurrency, 8, 2);
}
//---------------------------------------------------------------------------```
 The Interest Rate

The InterestRate() function is used to find the interest applied to a loan. Its syntax is:

```Extended __fastcall InterestRate(int NPeriods,
const Extended Payment,
const Extended PresentValue,
const Extended FutureValue,
TPaymentTime PaymentTime);```

All of the arguments are the same as described for the InterestPayment() function. Here is an example: ```//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalculateClick(TObject *Sender)
{
Extended Present, Future, Payments, Rate;
int NPeriod;
Present = StrToFloat(edtPresent->Text);
Future = StrToFloat(edtFuture->Text);
Payments = StrToFloat(edtPayments->Text);
NPeriod = StrToInt(edtNPeriods->Text);

Rate = InterestRate(NPeriod,
Payments,
Present,
Future,
ptEndOfPeriod) * 12 * 100;
AnsiString Value = FloatToStrF(Rate, ffGeneral, 3, 2);
edtRate->Text = Value + "%";
}
//---------------------------------------------------------------------------```
 The Internal Rate of Return

The InternalRateOfReturn() function is used to calculate an internal rate of return based on a series of investments. Its syntax is:

```Extended __fastcall InternalRateOfReturn(const Extended Guess,
const double * CashFlows,
const int CashFlows_Size);```

The CashFlows is an array of cash amounts that a customer has made on an investment. For example, a customer could make monthly deposits in a savings or credit union accounts. Another customer could be running a business and receiving different amounts of money as the business is flowing (or losing money). The cash flows do not have to be the same at different intervals but they should (or must) occur at regular intervals such as weekly (amount cut from a paycheck), bi-weekly (401k directly cut from paycheck, monthly (regular investment), or yearly (income). The CashFlows argument must be passed as an array and not an amount; otherwise you would receive an error.

The Guess is an estimate interest rate of return of the investment.

The CashFlow_Size is the dimension of the array – 1.

Here is an example: ```//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalculateClick(TObject *Sender)
{
double Goal;
double Month1, Month2, Month3, Month4, Month5, Month6;
Extended InterestGuess;
int Periods;

// Retrieve the estimate financial goal to achieve
Goal = edtGoal->Text.ToDouble();
// Retrieve the monthly investments
Month1 = edtMonth1->Text.ToDouble();
Month2 = edtMonth2->Text.ToDouble();
Month3 = edtMonth3->Text.ToDouble();
Month4 = edtMonth4->Text.ToDouble();
Month5 = edtMonth5->Text.ToDouble();
Month6 = edtMonth6->Text.ToDouble();
// Guess how much percentage
InterestGuess = StrToFloat(edtGuess->Text) / 100;

double Months[] = {-Goal,Month1,Month2,Month3,Month4,Month5,Month6};
Periods = (sizeof(Months) / sizeof(double)) - 1;
double IRR = InternalRateOfReturn(-InterestGuess,
Months, Periods) * 100;

// Format the number to display only two decimals
AnsiString Value = FloatToStrF(IRR, ffGeneral, 3, 2);
// Display the result with a percent sign
edtIRR->Text = Value + "%";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnExitClick(TObject *Sender)
{
exit(0);
}
//---------------------------------------------------------------------------```
 The Net Present Value

The NetPresentValue() function uses a series of cash flows to calculate the present value of an investment. Its syntax is:

```Extended __fastcall NetPresentValue(const Extended Rate,
const double * CashFlows,
const int CashFlows_Size,
TPaymentTime PaymentTime);```

The CashFlows is an array of payments made on an investment. Because it uses a series of payments, any payment made in the past should have a positive value (because it was made already). Any future payment should have a negative value (because it has not been made yet). The CashFlows should be passed as an array. The CashFlows_Size is the number of payments – 1, which is also the dimension of the array –1.

The Rate is the rate of discount during one period of the investment.

The PaymentTime specifies whether the payment occurs at the beginning or end of the period. It uses the TPaymentTime enumerator.

Here is an example: ```//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalculateClick(TObject *Sender)
{
double Goal;
double Month1, Month2, Month3, Month4, Month5, Month6;
Extended Rate;
int Periods;

// Retrieve the estimate financial goal to achieve
Goal = edtGoal->Text.ToDouble();
// Retrieve the monthly investments
Month1 = edtMonth1->Text.ToDouble();
Month2 = edtMonth2->Text.ToDouble();
Month3 = edtMonth3->Text.ToDouble();
Month4 = edtMonth4->Text.ToDouble();
Month5 = edtMonth5->Text.ToDouble();
Month6 = edtMonth6->Text.ToDouble();
Rate = StrToFloat(edtRate->Text) / 100;

double Months[] = { Month1, Month2, Month3, Month4, Month5, Month6 };
Periods = (sizeof(Months) / sizeof(double)) - 1;
double NPV = NetPresentValue(Rate,
Months,
Periods,
ptEndOfPeriod) - Goal;

// Format the number to display as currency
edtNPV->Text = FloatToStrF(NPV, ffCurrency, 8, 2);
}
//---------------------------------------------------------------------------```
 Measure-Based Functions

 Introduction A circle is a group or series of distinct points drawn at an exact same distance from another point referred to as the center. The distance from the center C to one of these equidistant points is called the radius, R. The line that connects all of the points that are equidistant to the center is called the circumference of the circle. The diameter is the distance between two points of the circumference to the center; in other words, a diameter is double the radius.

To manage the measurements and other related operations, the circumference is divided into 360 portions. Each of these portions is called a degree. The unit used to represent the degree is the degree, written as ˚. Therefore, a circle contains 360 degrees, that is 360˚. The measurement of two points A and D of the circumference could have 15 portions of the circumference. In this case, this measurement would be represents as 15˚.

The distance between two equidistant points A and B is a round shape geometrically defined as an arc. An angle, ө, is the ratio of the distance between two points A and B of the circumference divided by the radius R. This can be written as: Therefore, an angle is the ratio of an arc over the radius. Because an angle is a ratio and not a “physical” measurement, which means an angle is not a dimension, it is independent of the size of a circle. Obviously this angle represents the number of portions included by the three points. A better unit used to measure an angle is the radian or rad. A cycle is a measurement of the rotation around the circle. Since the rotation is not necessarily complete, depending on the scenario, a measure is made based on the angle that was covered during the rotation. A cycle could cover part of the circle in which case the rotation would not have been completed. A cycle could also cover the whole 360˚ of the circle and continue there after. A cycle is equivalent to the radian divided by 2 * Pi.

The VCL ships with functions used to perform conversions of values between different units. To use any of these functions, you must include the VCL math header file as:

`#include <math.hpp>`
 The Pi Constant

The word п, also written as Pi, is a constant number used in various mathematical calculations. Its approximate value is 3.1415926535897932. The calculator of Windows represents it as 3.1415926535897932384626433832795. Borland had included its value in the math.h library as M_PI 3.14159265358979323846.

A diameter is two times the radius. In geometry, it is written as 2R. In C++, it is written as 2 * R or R * 2 (because the multiplication is symmetric). The circumference of a circle is calculated by multiplying the diameter to Pi, which is 2Rп, or 2 * R * п or 2 * R * Pi.

To perform conversions between the degree and the radian, you can use the formula:

360˚ = 2п rad which is equivalent to 1 rad = 360˚ / 2п = 57.3˚

`Extended __fastcall CycleToRad(Extended Cycles);`

The CycleToRad() function is used to convert the measurement of an angle from radians to cycles. This function is equivalent to using the formula Radian = 2Pi * Cycle.

Here is an example:

```//---------------------------------------------------------------------------
#include <MATH.HPP>
#include <vcl.h>

#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------

#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{

}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnConversionClick(TObject *Sender)
{
Extended Cyc = StrToFloat(edtCycle->Text);

}
//---------------------------------------------------------------------------``` `Extended __fastcall DegToRad(Extended Degrees);`

The DegToRad() function is used to calculate the equivalent value of an angle from degrees to radians. This function follows the formula:

`2Pi rad = 360˚`

which is

`1 rad = 360˚ / 2Pi = 180˚ / Pi = 57.3˚`

Here is an example:

```//---------------------------------------------------------------------------
void __fastcall TForm1::btnConversionClick(TObject *Sender)
{
Extended Deg = StrToFloat(edtDegrees->Text);

}
//---------------------------------------------------------------------------``` `Extended __fastcall RadToCycle(Extended Radians);`

The RadToCycle() function is used to convert the measurement of an angle from radians to cycles. This function is equivalent to using the formula Cycle = Radian / 2Pi.

Here is an example:

```//---------------------------------------------------------------------------
void __fastcall TForm1::btnConversionClick(TObject *Sender)
{

edtCycle->Text = FloatToStr(Cyc);
}
//---------------------------------------------------------------------------``` `Extended __fastcall RadToDeg(Extended Radians);`

The RadToDeg() function is used to calculate the equivalent value of an angle from radians to degrees. This function applies the formula:

`360˚ = 2 * Pi`

which is

`1˚ = 2 * Pi`

Here is an example:

```//---------------------------------------------------------------------------
void __fastcall TForm1::btnConversionClick(TObject *Sender)
{

edtDegrees->Text = FloatToStr(Deg);
}
//---------------------------------------------------------------------------``` Statistics

 The Maximum Integer Value of a Series
`int __fastcall MaxIntValue(const int * Data, const int Data_Size);`

The MaxIntValue() function calculates the maximum value of an array of integers. The first parameter of the function, Data, represents the name of the array. The second argument, Data_Size is the number-1 of members of the array.

To get the maximum value of a group of integers, declare an integral array of numbers. You can initialize such a variable or request the values of its members from the user. The value of the Data_Size argument must be 1 less than the total number of the array members.

Here is an example that uses the MaxIntValue() function:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Integer n, MaxInteger;
Integer Numbers[] = { 15, 408, 72, 995, 32 };

MaxInteger = MaxIntValue(Numbers, 4);
Edit1->Text = IntToStr(MaxInteger);
}
//---------------------------------------------------------------------------```
 The Maximum Value of a Series
`double __fastcall MaxValue(const double * Data, const int Data_Size);`

The MaxValue() function is a numeric value that represents the maximum number of items of an array. This function takes two arguments. The first argument, Data, represents an array of integers or double-precision numbers. The second argument is the number-1 of the items of the array; for example, if the considered array has 4 members, the Data_Size argument would be 3.

To use the MaxValue() function, declare an array that involves the necessary numbers. You can initialize such a variable or request the numbers from the user. To calculate the maximum value of a range, supply the array and its size. If you do not know the dimension of the array, you can use the sizeof operator to find it out. Here is an example of using the MaxValue() function:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
double Values[] = { 12.55, 10.15, 980.22, 50.50, 280.12 };
int Size = (sizeof(Values)/sizeof(double)) - 1;

double Maximum = MaxValue(Values,Size);

Edit1->Text = Maximum;
}
//---------------------------------------------------------------------------```
 The Mean or Average Value of a Series
`Extended __fastcall Mean(const double * Data, const int Data_Size);`

The Mean() function considers an array of numbers and calculates the average value of those numbers. The function takes two arguments. The first, Data, is the name of the array of numbers. These number could integers or floating numbers. The second argument, Data_Size represents the number-1 of members of the array. You can type an integral number as the Data_Size or you can use the sizeof operator to get the dimension of the array and subtract 1 from it. After the calculation, the function returns a long double-precision number as the average of the numbers.

Here is an example:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
double Values[] = { 12.55, 10.15, 980.22, 50.50, 280.12 };
int Size = (sizeof(Values) / sizeof(double)) - 1;

double Average = Mean(Values, Size);

Edit1->Text = Average;
}
//---------------------------------------------------------------------------```
 The Minimum Integral Value of a Series
`int __fastcall MinIntValue(const int * Data, const int Data_Size);`

The MinIntValue() function calculates the minimum value of an array of integers. The Data argument of the function is the name of the array. The second argument, Data_Size is the number-1 of members of the array.

To get the minimum value of a group of integers, declare an integral array of numbers. You can initialize the variable or request the values of its members from the user. The value of the Data_Size argument must be 1 less than the total number of members.

Here is an example that uses the MaxIntValue() function:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int Numbers[] = { 15, 408, 72, 995, 32 };
int Size = (sizeof(Numbers)/sizeof(double)) - 1;

double MinInteger = MinIntValue(Numbers, Size);

Edit1->Text = MinInteger;
}
//---------------------------------------------------------------------------```
 The Minimum Value of a Series
`double __fastcall MinValue(const double * Data, const int Data_Size);`

The MinValue() function gets a numeric value that represents the minimum value of the items of an array. This function takes two arguments. The first argument, Data, represents an array of integers or double-precision numbers. The second argument is the number-1 of the items of the array; for example, if the considered array has 4 members, the Data_Size argument would be 3.

To use the MinValue() function, declare an array that involves the necessary numbers. You can initialize such a variable or request the values from the user. To calculate the minimum value of a range, supply the array and its size. If you do not know the dimension of the array, you can use the sizeof operator to find it out. Here is an example that uses the MinValue() function:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
double Values[] = { 12.55, 10.15, 980.22, 50.50, 280.12 };
int Size = (sizeof(Values)/sizeof(double)) - 1;

double Minimum = MinValue(Values,Size);
Edit1->Text = Minimum;
}
//---------------------------------------------------------------------------```
 The Sum of Values of a Series
`Extended __fastcall Sum(const double * Data, const int Data_Size);`

The Sum() function is used to calculate the sum value of a group of numbers. The first argument of this function, Data, is the name of an array that holds the numbers considered. The Data_Size argument is the dimension of the array minus 1.

To get the sum of a group of numbers, declare an array to hold the necessary numbers. The numbers can be integers or double precision values. You can initialize the array with these numbers or request their values from the user.

Here is an example of calculating a total number of grades of a student:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Double Grades[] = { 12.50, 14.00, 16.00, 15.50,
12.00, 10.50, 14.50, 17.50 };
int Size = (sizeof(Grades)/sizeof(double)) - 1;

Edit1->Text = FloatToStr(Total);
}
//---------------------------------------------------------------------------```
 The Sum of Integers of a Series
`int __fastcall SumInt(const int * Data, const int Data_Size);`

The SumInt() function is used to calculate the total of a group of integral numbers. This function takes two arguments. The first, Data, is the name of the array that holds the numbers. The numbers must be integers. If you want to calculate a total of floating-point numbers, use the Sum() function. The second argument, Data_Size is the size of the array minus one.

Here is an example that simulates a company inventory to count business assets:

```//---------------------------------------------------------------------------
#include <iostream.h>
#include <math.hpp>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused

int main(int argc, char* argv[])
{
int Tables, Chairs, BookShelves, TrashCans,
Desktops, Laptops, Printers, FaxMachines,
Books, Pens, Pencils, Others;

cout << "Company Inventory\nType the number of items of each category\n";
cout << "Desktops: "; cin >> Desktops;
cout << "Laptops: "; cin >> Laptops;
cout << "Printers: "; cin >> Printers;
cout << "Fax Machines: "; cin >> FaxMachines;
cout << "Chairs: "; cin >> Chairs;
cout << "Tables: "; cin >> Tables;
cout << "Book Shelves: "; cin >> BookShelves;
cout << "Books: "; cin >> Books;
cout << "Trash Cans: "; cin >> TrashCans;
cout << "Pens: "; cin >> Pens;
cout << "Pencils: "; cin >> Pencils;
cout << "Others: "; cin >> Others;

int Assets[] = { Tables, Chairs, BookShelves, TrashCans,
Desktops, Laptops, Printers, FaxMachines,
Books, Pens, Pencils, Others };
int Items = (sizeof(Assets)/sizeof(int)) - 1;
int AllAssets = SumInt(Assets, Items);

cout << "\nTotal Number of Items: " << AllAssets;

cout << "\n\nPress Enter to send the inventory...";
getchar();
return 0;
}
//---------------------------------------------------------------------------```
```Company Inventory
Type the number of items of each category
Desktops: 12
Laptops: 2
Printers: 8
Fax Machines: 2
Chairs: 18
Tables: 14
Book Shelves: 10
Books: 20
Trash Cans: 15
Pens: 120
Pencils: 144
Others: 212

Total Number of Items: 577

Press Enter to send the inventory...```
 The Sum Of Squares of a Series
`Extended __fastcall SumOfSquares(const double * Data, const int Data_Size);`

The SumOfSquares() function performs a double operation on an array. First it calculates the square S of each member of the array; then it calculates the total of the individual S values. The first argument of the function, Data, is the name of the array, the second argument, Data_Size represents the dimension of the array minus 1.

To calculate the total of the squares, declare an array variable. You can initialize the array by providing the necessary list of numbers. Otherwise, request the different numbers from the user. The function will take care of the rest.

Here is example:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Double Grades[] = { 12.50, 14.00, 16.00, 15.50,
12.00, 10.50, 14.50, 17.50 };
int Size = (sizeof(Grades)/sizeof(double)) - 1;

Edit1->Text = FloatToStr(Total);
}
//---------------------------------------------------------------------------```
 The Sums and Squares of a Series
```void __fastcall SumsAndSquares(const double * Data,
const int Data_Size,
Extended &Sum,
Extended &SumOfSquares);```

The SumsAndSquares() function performs two operations and returns two values. Using a group of numbers, the function calculates their total, Sum. It also calculates the square S of each number then calculates the total of the S values, which produces a SumOfSquares. The first argument, Data, is the array of the numbers considered. The Data_Size argument is the number of items in the array minus 1. The Sum and the SumOfSquares arguments are passed by reference, which allows the function to return these last two values.

Here is an example:

```//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Extended Total, TotalOfSquares;
int Size;
double Values[] = { 32.12, 208.45, 14.80, 95.25,
30.32, 102.55, 88.20, 100.05 };

Size = (sizeof(Values)/sizeof(double)) - 1;
SumsAndSquares(Values, Size, Total, TotalOfSquares);

Edit1->Text = FloatToStr(Total);
Edit2->Text = FloatToStr(TotalOfSquares);
}
//---------------------------------------------------------------------------```
 Trigonometric Functions The Cosine of a Value

Cosine Functions

```double cos(double x);
long double cosl(long double x);```

The cos() function calculates the cosine of a number.

Consider AB the length of A to B, also referred to as the hypotenuse. Also consider AC the length of A to C which is the side adjacent to point A. The cosine of the angle at point A is the ratio AC/AB. That is, the ratio of the adjacent length, AC, over the length of the hypotenuse, AB: The returned value, the ratio, is a double-precision number between –1 and 1.

Example: A form contains an Edit control named edtValue. After the user has typed a value and presses Enter, the OnKeyPress event retrieves the number typed in the edit box, calculates its cosine and displays it in the same Edit control:

```//---------------------------------------------------------------------------
void __fastcall TForm1::edtValueKeyPress(TObject *Sender, char &Key)
{
if( Key == VK_RETURN )
{
double Value = edtValue->Text.ToDouble();
double Cosinus = cos(Value);

edtValue->Text = Cosinus;
}
}
//---------------------------------------------------------------------------```  The Sine of a Value
```double sin(double x);
long double sinl(long double x);```

The sin() function calculates the sine of a number.

Consider AB the length of A to B, also called the hypotenuse to point A. Also consider CB the length of C to B, which is the opposite side to point A. The sine represents the ratio of CB/AB; that is, the ratio of the opposite side, CB over the hypotenuse AB.

The sin() function takes a double-precision number and returns one between –1 and 1. The sinl() function is used for 10-byte values.

Example: A form contains an Edit control named edtValue. After the user has typed a value and presses Enter, the OnKeyPress event retrieves the number typed in the edit box, calculates its sine and displays the result in the same Edit control:

```//---------------------------------------------------------------------------
void __fastcall TForm1::edtValueKeyPress(TObject *Sender, char &Key)
{
if( Key == VK_RETURN )
{
double Value = edtValue->Text.ToDouble();
double Sinus = sin(Value);

edtValue->Text = Sinus;
}
}
//---------------------------------------------------------------------------```  Tangents

 The C/C++ tan Functions
```double tan(double x);
long double tanl(long double x);```

The tan() function calculates the tangent of a number.

In geometry, consider AC the length of A to C. Also consider BC the length of B to C. The tangent is the result of BC/AC; that is, the ratio of BC over AC.

Example: A form contains an Edit control named edtValue. After the user has typed a value and presses Enter, the OnKeyPress event retrieves the number typed in the edit box, calculates its tangent and displays the result in the same Edit control:

```//---------------------------------------------------------------------------
void __fastcall TForm1::edtValueKeyPress(TObject *Sender, char &Key)
{
if( Key == VK_RETURN )
{
double Value = edtValue->Text.ToDouble();
double Tangent = tan(Value);

edtValue->Text = Tangent;
}
}
//---------------------------------------------------------------------------```  The Arc Tangent Functions
`double atan(double x);`

The atan() function is used to calculate the arc tangent of a number. In geometry, consider BC the length of B to C. Also consider AC the length of A to C. The arc tangent is the ratio of BC/AC.

The atan() function takes one argument, x, that represents the angle BA AC. After the evaluation, the function returns a double-precision number between –PI/2 and PI/2.

If the number to be evaluated is larger than a double, use the atanl() function:

`long double atanl(long double x);`

This function takes a long double argument and returns a long double.