# C++ Valarray Library - tan Function

#### Complete Python Prime Pack for 2023

9 Courses     2 eBooks

#### Artificial Intelligence & Machine Learning Prime Pack

6 Courses     1 eBooks

#### Java Prime Pack 2023

8 Courses     2 eBooks

## Description

It returns a valarray containing the tangents of all the elements of x, in the same order.

## Declaration

Following is the declaration for std::tan function.

```template<class T> valarray<T> tan (const valarray<T>& x);
```

### C++11

```template<class T> valarray<T> tan (const valarray<T>& x);
```

## Parameters

x − It is containing elements of a type for which the unary function abs is defined.

## Return Value

It returns a valarray containing the tangents of all the elements of x, in the same order.

## Exceptions

Basic guarantee − if any operation performed on the elements throws an exception.

## Data races

All elements effectively copied are accessed.

## Example

In below example explains about std::tan function.

```#include <iostream>
#include <cstddef>
#include <cmath>
#include <valarray>

int main () {
double val[] = {1.2, 3.14, 5.0};
std::valarray<double> foo (val,3);

std::valarray<double> bar = tan (foo);

std::cout << "foo:";
for (std::size_t i=0; i<foo.size(); ++i)
std::cout << ' ' << foo[i];
std::cout << '\n';

std::cout << "bar:";
for (std::size_t i=0; i<bar.size(); ++i)
std::cout << ' ' << bar[i];
std::cout << '\n';

return 0;
}
```

Let us compile and run the above program, this will produce the following result −

```foo: 1.2 3.14 5
bar: 2.57215 -0.00159265 -3.38052
```
valarray.htm