# C# Math: Sign Exp IEEERemainder, BigMul and DivRem

Use the Math type. This type offers many mathematical functions ready to deploy.**Math** is a class in the System namespace. The .NET Framework provides many built-in mathematical methods. These are easier to use than custom expressions.

**These methods** are tested and easy to access. This page contains information on some less-common math methods like Sign and DivRem.

**Sign.** Is a number positive or negative? The Math.Sign method will help you determine this. Math.Sign accepts many numeric types as arguments. It returns one of three values: -1, 0 or 1.

**Info:** The result -1 means negative. Zero means the number is zero. And 1 means it is positive.

**Next:** This program demonstrates the Math.Sign method with ints and doubles as arguments.

**C# program that uses Math.Sign method**
using System;
class Program
{
static void Main()
{
int a = __Math.Sign__(-5);
int b = Math.Sign(5);
int c = Math.Sign(0);
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
a = Math.Sign(-5.5);
b = Math.Sign(5.5);
Console.WriteLine(a);
Console.WriteLine(b);
}
}
**Output**
-1
1
0
-1
1

**Sign, implementation.** Sign is implemented with branch statements that test the parameter against zero. Sign returns 0 when the argument is 0—zero is neither positive nor negative.

**Exp.** Math.Exp is the exponential function. To apply the exponential function, we either use Math.Pow to raise E to a specified power, or call Math.Exp with the exponent as an argument.

**Here:** This program calls the Math.Exp method and then calls the Math.Pow method such that they have equivalent results.

**Note:** The Math.Exp always uses Math.E (e) as the specified number. The results of these two method calls are the same.

**C# program that uses Exp method**
using System;
class Program
{
static void Main()
{*
// Compute e ^ 2.
*double a = __Math.Exp__(2);
double b = Math.Pow(Math.E, 2);*
// Write results.
*Console.WriteLine(a);
Console.WriteLine(b);
}
}
**Output**
7.38905609893065
7.38905609893065

**IEEERemainder.** The IEEERemainder method returns a remainder of 1 for the arguments 4, 3. This is the same result as the modulo expression.

**But:** With the arguments 3 and 4, it returns a remainder of -1. The modulo expression meanwhile returns the value 3.

**Here:** We see that for IEEERemainder, two positive numbers can have a negative remainder.

**Tip:** This occurs when the first number is smaller than the second number. In many cases, the modulo operator returns the same result.

**C# program that uses IEEERemainder**
using System;
class Program
{
static void Main()
{
double a = Math.__IEEERemainder__(4, 3);
Console.WriteLine(*"Part 1"*);
Console.WriteLine(a);
Console.WriteLine(4 % 3);
double b = Math.__IEEERemainder__(3, 4);
Console.WriteLine(*"Part 2"*);
Console.WriteLine(b);
Console.WriteLine(3 % 4);
}
}
**Output**
Part 1
1
1
Part 2
-1
3

**BigMul.** This multiplies two integers. With regular multiplication, as with the star operator, the result may overflow. BigMul avoids this problem. It returns a long.

**Here:** This program's goal is to multiply the int.MaxValue value (2147483647) by itself. If we call Math.BigMul, the result will be correct.

**C# program that calls BigMul**
using System;
class Program
{
static void Main()
{*
// Call BigMul.
*long product1 = __Math.BigMul__(int.MaxValue, int.MaxValue);*
// Use multiplication operator.
*long product2 = unchecked(int.MaxValue * int.MaxValue);*
// Display values.
*Console.WriteLine(product1);
Console.WriteLine(product2);
}
}
**Output**
4611686014132420609
1

**BigMul, notes.** When you multiply two large numbers like 2147483647 you should not get 1. This makes no sense. Instead, you should get a larger number.

**Note:** If you query Google for 2147483647 squared, it will give you the correct result.

**So:** You should probably use BigMul anywhere you are multiplying large integers.

MultiplyOverflowExceptionInt, uint

**DivRem.** Math.DivRem divides two numbers and returns the remainder. With the division operator we do not get the remainder in a separate variable. But with DivRem we get both parts.

**Here:** This program divides the value 1000 by the value 300. Because the 300 can go into the 1000 three times, you get the result of 3.

**However:** There is 100 left over. This is returned by the out parameter in the Math.DivRem method.

**Tip:** The out parameter in the DivRem method is always assigned when DivRem returns. It will be ready to use.

Out**C# program that calls DivRem method**
using System;
class Program
{
static void Main()
{
const int a = 1000;
const int b = 300;
int result;
int quotient = __Math.DivRem__(a, b, out result);
Console.WriteLine(quotient);
Console.WriteLine(result);
}
}
**Output**
3
100

**A summary.** Instead of implementing these functions yourself, I suggest using the Math type. It has many public static methods. You may be able to improve the quality of your code.

© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.