C#

Math.Round

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);
}
}-1
1
0
-1
1

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);
}
}7.38905609893065
7.38905609893065

using System;
class Program
{
static void Main()
{
*// Part 1: use larger first number.*
double a = Math.**IEEERemainder**(4, 3);
Console.WriteLine(*"Part 1"*);
Console.WriteLine(a);
Console.WriteLine(4 % 3);
*// Part 2: use smaller first number.*
double b = Math.**IEEERemainder**(3, 4);
Console.WriteLine(*"Part 2"*);
Console.WriteLine(b);
Console.WriteLine(3 % 4);
}
}Part 1
1
1
Part 2
-1
3

Multiply

OverflowException

Int, uint

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);
}
}4611686014132420609
1

out

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);
}
}3
100

Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.

Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.

This page was last updated on Dec 9, 2021 (image).