Open In App

C# | Math Class

Improve
Improve
Like Article
Like
Save
Share
Report

In C#, Math class comes under the System namespace. It is used to provide static methods and constants for logarithmic, trigonometric, and other useful mathematical functions. It is a static class and inherits the object class.

public static class Math

Fields

A field is a variable that is declared in a class or struct. These are considered as the members of their containing type. Fields may be static or instance fields. Math class contains the two fields i.e. E and PI.

  1. Math.E Field: This field represents the natural logarithmic base, specified by the constant, e.
  2. Math.PI Field: It represents the ratio of the circumference of a circle to its diameter, specified by the constant, PI(π).

Example:




// C# program to demonstrate the
// value of Math Class Fields
using System;
  
class GFG {
  
    // Main method
    static void Main()
    {
  
        // To find E constant values
        double e = Math.E;
  
        // Print result
        Console.WriteLine("Math.E = " + e);
  
        // To find PI constant values
        double pi_value = Math.PI;
  
        // Print result
        Console.WriteLine("Math.PI = " + pi_value);
    }
}


Output:

Math.E = 2.71828182845905
Math.PI = 3.14159265358979

You can read more about fields from C# | Math Class Fields with Examples.

Methods

Method Description
Abs() Returns the absolute value of a specified number.
Acos() Returns the angle whose cosine is the specified number.
Acosh() Returns the Inverse hyperbolic cosine of the specified number.
Asin() Returns the angle whose sine is the specified number.
Asinh() Returns the Inverse hyperbolic sine of the specified number.
Atan() Returns the angle whose tangent is the specified number.
Atan2() Returns the angle whose tangent is the quotient of two specified numbers.
Atanh() Returns the Inverse hyperbolic tangent of the specified number.
BigMul() Produces the full product of two 32-bit numbers.
Cbrt() Returns the cube root of a specified value.
Ceiling() Returns the smallest integral value greater than or equal to the specified number.
Clamp() It is used to restrict a value to a given range.
Cos() Returns the cosine of the specified angle.
Cosh() Returns the hyperbolic cosine of the specified angle.
DivRem() Calculates the quotient of two numbers and also returns the remainder in an output parameter.
Exp() Returns e raised to the specified power.
Floor() Returns the largest integral value less than or equal to the specified number.
IEEERemainder() Returns the remainder resulting from the division of a specified number by another specified number.
Log() Returns the logarithm of a specified number.
Log10() Returns the base 10 logarithm of a specified number.
Max() Returns the larger of two specified numbers.
Min() Returns the smaller of two numbers.
Pow() Returns a specified number raised to the specified power.
Round() Rounds a value to the nearest integer or to the specified number of fractional digits.
Sign() Returns an integer that indicates the sign of a number.
Sin() Returns the sine of the specified angle.
Sinh() Returns the hyperbolic sine of the specified angle.
Sqrt() Returns the square root of a specified number.
Tan() Returns the tangent of the specified angle.
Tanh() Returns the hyperbolic tangent of the specified angle.
Truncate() Calculates the integral part of a number.

Example:




// C# program to illustrate the
// Math class methods
using System;
  
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // using Floor() Method
        Console.WriteLine("Floor value of 123.123: "
                             + Math.Floor(123.123));
  
        // using Asin() Method
        Console.WriteLine("Asin value of 0.35: "
                             + Math.Asin(0.35));
  
        // using Sqrt() Method
        Console.WriteLine("Square Root of 81: "
                              + Math.Sqrt(81));
  
        // using Round() Method
        Console.WriteLine("Round value of 14.6534: "
                             + Math.Round(14.6534));
    }
}


Output:

Floor value of 123.123: 123
Asin value of 0.35: 0.35757110364551
Square Root of 81: 9
Round value of 14.6534: 15


Last Updated : 05 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads