# 30 Days Of .NET 6 - Day 5 - Mathematics API Additions

**[C#, 30 Days Of .NET 6]**

It may surprise you to learn that even the Math API, which generally does not change all that much, has some additions for those who do high performance trigonometric functions.

Suppose you need the cosine of an angle.

```
var oldCos = Math.Cos(35);
```

Then suppose you also need the sine of an angle

```
var oldSin = Math.Sin(35);
```

If you happen to need both in a computation, this is already pretty straightforward.

However there is an optimization that can be realized here - you can compute them both simultaneously - the new Math.SinCos method.

```
var result = Math.SinCos(35);
Console.WriteLine($"The Sin is {result.Sin} and the Cosine is {result.Cos}");
```

This should print for you the following:

```
The Sin is -0.428182669496151 and the Cosine is -0.9036922050915059
```

**HOWEVER**

I tried to benchmark the new method vs just getting the sin and cos independently as before.

At least as of release 7, Math.SinCos seems to be **orders of magnitude slower**.

Here is the code for the benchmark (I will upload the benchmark tests to Github)

```
const double angle = 35;
[Benchmark]
public (double, double) GetIndividually()
{
var sin = Math.Sin(angle);
var cos = Math.Cos(angle);
return (sin, cos);
}
[Benchmark]
public (double, double) GetSimultaneously()
{
var result = Math.SinCos(angle);
return (result.Sin, result.Cos);
}
```

And here are the results I got

```
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19043.1165 (21H1/May2021Update)
Intel Core i5-9300H CPU 2.40GHz, 1 CPU, 8 logical and 4 physical cores
.NET SDK=6.0.100-preview.7.21379.14
[Host] : .NET 6.0.0 (6.0.21.37719), X64 RyuJIT
DefaultJob : .NET 6.0.0 (6.0.21.37719), X64 RyuJIT
```

Method | Mean | Error | StdDev |
---|---|---|---|

GetIndividually | 0.5548 ns | 0.0435 ns | 0.0806 ns |

GetSimultaneously | 19.4477 ns | 0.3351 ns | 0.2616 ns |

This has already been logged but it seems it wonâ€™t be addressed until .NET version 7.

There is also a new method for computing the reciprocal estimate of a number, Math.ReciprocalEstimate

```
var reciprocalEstimate = Math.ReciprocalEstimate(90);
Console.WriteLine(reciprocalEstimate);
```

There is another new method for computing the reciprocal of the square root of a number, Math.ReciprocalSqrtEstimate

```
var reciprocalRootEstimate = Math.ReciprocalSqrtEstimate(90);
Console.WriteLine(reciprocalRootEstimate);
```

Finally, there is a a new method to assist when you are doing integer division and are interested in the remainder.

Most people would do it like so:

```
var quotient = 10 / 3;
var remainder = 10 % 3;
Console.WriteLine($"The quotient is {quotient} and the remainder is {remainder}");
```

The Math class has for a long time had a method that makes such code cleaner - Math.DivRem:

```
int remainder;
var quotient = Math.DivRem(10, 3, out remainder);
Console.WriteLine($"The quotient is {quotient} and the remainder is {remainder}");
```

This has been further improved in .NET 6 - rather than return the remainder as an output parameter, the method returns a tuple of the quotient and the remainder.

```
var result = Math.DivRem(10, 3);
Console.WriteLine($"The quotient is {result.Quotient} and the remainder is {result.Remainder}");
```

# Thoughts

For those who do a lot of mathematical computations, these new methods may offer performance and coding improvements.

The code is in my Github.

# TLDR

Math class now has additional methods for some target demographics.

**This is Day 5 of the 30 Days Of .NET 6 where every day I will attempt to explain one new / improved thing in the upcoming release of .NET 6.**

Happy hacking!