# Number.Tanh

## Understanding the Hyperbolic Tangent Function

Before diving into the M code for the Number.Tanh function, it is important to understand what the hyperbolic tangent function is and how it works. The hyperbolic tangent function, or tanh, is a mathematical function that maps real numbers to the interval (-1, 1). It is defined as the ratio of the hyperbolic sine function to the hyperbolic cosine function:

tanh(x) = sinh(x) / cosh(x)

The hyperbolic sine and cosine functions are defined as:

sinh(x) = (e^x – e^-x) / 2

cosh(x) = (e^x + e^-x) / 2

where e is the base of the natural logarithm.

The tanh function has several properties that make it useful in data analysis. First, it is an odd function, which means that tanh(-x) = -tanh(x). Second, it is continuous and differentiable on its entire domain. This makes it useful for curve fitting and optimization tasks.

## The M Code for the Number.Tanh Function

Now that we have a basic understanding of the tanh function, let’s take a look at the M code for the Number.Tanh function in Power Query. The function takes a single argument, a number, and returns the hyperbolic tangent of that number.

(Number as nullable number) as nullable number =>

if Number = null then null else if Number = 0 then 0 else

let

d = Exp(-2 Number),

n = 1 – d,

t = n / (1 + d)

in

if Number < 0 then -t else t

``` The M code for the Number.Tanh function is a bit more complex than the mathematical definition we described earlier. Let's break it down line by line. ```

(Number as nullable number) as nullable number =>

``` This line defines the function and its input parameter. The Number parameter is defined as a nullable number, which means it can be either a number or null. ```

if Number = null then null else if Number = 0 then 0 else

``` This line checks if the input number is null or zero. If it is, the function returns null or zero, respectively. ```

let

d = Exp(-2 Number),

n = 1 – d,

t = n / (1 + d)

in

``` This block of code calculates the hyperbolic tangent of the input number. It uses the Exp function, which returns the exponential value of a number, to calculate the hyperbolic sine and cosine of the input number. The variable d is assigned the value of e^-2x, and n is assigned the value of 1 - e^-2x. The variable t is then calculated as n / (1 + e^-2x). ```

if Number < 0 then -t else t

``` Finally, this line checks if the input number is negative. If it is, the function returns the negative value of t. If not, it returns t. Using the Number.Tanh Function in Power Query Now that we have a good understanding of the M code behind the Number.Tanh function, let's look at some examples of how it can be used in Power Query. Suppose we have a dataset that contains a column of numbers. We can use the Number.Tanh function to calculate the hyperbolic tangent of each number in the column by creating a custom column with the following formula: ```

= Number.Tanh([Number])

``` This will create a new column with the hyperbolic tangent value for each corresponding value in the Number column. We can also use the Number.Tanh function in combination with other functions to perform more complex calculations. For example, suppose we have a dataset that contains a column of temperatures in Celsius. We can use the following formula to convert the temperatures to Fahrenheit and then calculate the hyperbolic tangent of the Fahrenheit temperature: ```

= Number.Tanh((([Temperature] 9/5) + 32))

``` This formula first multiplies the Celsius temperature by 9/5, adds 32 to convert it to Fahrenheit, and then calculates the hyperbolic tangent of the resulting temperature. The Number.Tanh function is a powerful tool for data analysis in Power Query. Its M code implementation is complex but provides a fast and efficient way to calculate the hyperbolic tangent of a number. By understanding the M code behind this function, we can use it to manipulate data in a variety of ways and expand our data analysis capabilities in Power Query. Power Query and M Training Courses by G Com Solutions (0800 998 9248) Power Query and M Intensive Training Course £1,050.00 – £26,550.00 Select optionsContinue Loading Done Power Query and M Introduction £395.00 – £9,750.00 Select optionsContinue Loading Done Power Query and M Intermediate £395.00 – £9,750.00 Select optionsContinue Loading Done Power Query and M Advanced £395.00 – £9,750.00 Select optionsContinue Loading Done Upcoming Courses Contact Us Subject Your Name (required) Company/Organisation Email (required) Telephone Training Course(s) Power BI Intensive TrainingPower BI introduction Power BI IntermediatePower BI AdvancedDAXPower Query MPower BI CertificationPower BI AdministrationPower PlatformPower AutomatePower AppsOTHER Your Message Upload Example Document(s) (Zip multiple files) ```
``` ```
``` ```
``` ```
``` ```
``` ```