# Ada Programming/Mathematical calculations

Ada is very well suited for all kinds of calculations. You can define your own fixed point and floating point types and — with the aid of generic packages call all the mathematical functions you need. In that respect Ada is on par with Fortran. This module will show you how to use them and while we progress we create a simple RPN calculator.

## Simple calculations
### Addition

Additions can be done using the predefined operator +. The operator is predefined for all numeric types and the following, working code, demonstrates its use:

### Subtraction

Subtractions can be done using the predefined operator -. The following extended demo shows the use of + and - operator together:
Purists might be surprised about the use of goto — but some people prefer the use of goto over the use of multiple return statements if inside functions — given that, the opinions on this topic vary strongly. See the isn't goto evil article.

### Multiplication

Multiplication can be done using the predefined operator *. For a demo see the next chapter about Division.

### Division

Divisions can be done using the predefined operators /, mod, rem. The operator / performs a normal division, mod returns a modulus division and rem returns the remainder of the modulus division.
The following extended demo shows the use of the +, -, * and / operators together as well as the use of a four number wide stack to store intermediate results:
The operators mod and rem are not part of the demonstration as they are only defined for integer types.

## Exponential calculations

All exponential functions are defined inside the generic package Ada.Numerics.Generic_Elementary_Functions.

### Power of

Calculation of the form $x^{y}$ are performed by the operator **. Beware: There are two versions of this operator. The predefined operator ** allows only for Standard.Integer to be used as exponent. If you need to use a floating point type as exponent you need to use the ** defined in Ada.Numerics.Generic_Elementary_Functions.

### Root

The square root ${\sqrt {x}}$ is calculated with the function Sqrt. There is no function defined to calculate an arbitrary root ${\sqrt[{n}]{x}}$. However you can use logarithms to calculate an arbitrary root using the mathematical identity: ${\sqrt[{b}]{a}}=e^{\log _{e}(a)/b}$ which will become root := Exp (Log (a) / b) in Ada. Alternatively, use ${\sqrt[{b}]{a}}=a^{\frac {1}{b}}$ which, in Ada, is root := a**(1.0/b).

### Logarithm

Ada.Numerics.Generic_Elementary_Functions defines a function for both the arbitrary logarithm $log_{n}(x)$ and the natural logarithm $log_{e}(x)$, both of which have the same name Log distinguished by the number of parameters.

### Demonstration

The following extended demo shows the how to use the exponential functions in Ada. The new demo also uses Unbounded_String instead of Strings which make the comparisons easier.
Please note that from now on we won't copy the full sources any more. Do follow the download links to see the full program.

## Higher math
### Trigonometric calculations

The full set of trigonometric functions are defined inside the generic package Ada.Numerics.Generic_Elementary_Functions. All functions are defined for 2π and an arbitrary cycle value (a full cycle of revolution).
Please note the difference of calling the Arctan function.
The Demo also contains an improved numeric output which behaves more like a normal calculator.

### Hyperbolic calculations

You guessed it: The full set of hyperbolic functions is defined inside the generic package Ada.Numerics.Generic_Elementary_Functions.
As added bonus this version supports error handling and therefore won't just crash when an illegal calculation is performed.

### Complex arithmethic

For complex arithmetic Ada provides the package Ada.Numerics.Generic_Complex_Types. This package is part of the "special need Annexes" which means it is optional. The open source Ada compiler GNAT implements all "special need Annexes" and therefore has complex arithmetic available.
Since Ada supports user defined operators, all (+, -, *) operators have their usual meaning as soon as the package Ada.Numerics.Generic_Complex_Types has been instantiated (package ... is new ...) and the type has been made visible (use type ...)
Ada also provides the packages Ada.Text_IO.Complex_IO and Ada.Numerics.Generic_Complex_Elementary_Functions which provide similar functionality to their normal counterparts. But there are some differences:
* Ada.Numerics.Generic_Complex_Elementary_Functions supports only the exponential and trigonometric functions which make sense in complex arithmetic.
* Ada.Text_IO.Complex_IO is a child package of Ada.Text_IO and therefore needs its own with. Note: the Ada.Text_IO.Complex_IOGet function is pretty fault tolerant - if you forget the "," or the "" pair it will still parse the input correctly.
So, with only a very few modifications you can convert your "normals" calculator to a calculator for complex arithmetic:

### Vector and Matrix Arithmetic

Ada supports vector and matrix Arithmetic for both normal real types and complex types. For those, the generic packages Ada.Numerics.Generic_Real_Arrays and Ada.Numerics.Generic_Complex_Arrays are used. Both packages offer the usual set of operations, however there is no I/O package and understandably, no package for elementary functions.
Since there is no I/O package for vector and matrix I/O creating a demo is by far more complex — and hence not ready yet. You can have a look at the current progress which will be a universal calculator merging all feature.
Status: Stalled - for a Vector and Matrix stack we need Indefinite_Vectors — which are currently not part of GNAT/Pro. Well I could use the booch components ...

## See also
### Wikibook

* Ada Programming
* Ada Programming/Delimiters/-
* Ada Programming/Libraries/Ada.Numerics.Generic Complex Types
* Ada Programming/Libraries/Ada.Numerics.Generic Elementary Functions

### Ada 95 Reference Manual

* 4.4: Expressions
* A.5.1: Elementary Functions
* A.10.1: The Package Text_IO
* G.1: Complex Arithmetic
* G.3: Vector and Matrix Manipulation

### Ada 2005 Reference Manual

* 4.4: Expressions
* A.5.1: Elementary Functions
* A.10.1: The Package Text_IO
* G.1: Complex Arithmetic
* G.3: Vector and Matrix Manipulation