Metodos Math
Metodos Math
Metodos Math
Sección: 1
Asignatura: Programación I
1
Métodos Math o Matemáticos en Microsoft Visual C#
Ejemplo:
// Math.Abs(Decimal) Method
using System;
class Geeks {
// Main Method
-17.47M, Decimal.MaxValue};
2
Console.WriteLine("Absolute value of {0} = {1}",
value,
Math.Abs(value));
Ejemplo:
// of Math.Acos() method
using System;
class Geeks {
// Main Method
double a = Math.PI;
Console.WriteLine(Math.Acos(a));
3
Console.WriteLine(Math.Acos(2));
Console.WriteLine(Math.Acos(0.3584));
double d = 0.0;
double e = -0.0;
// Output 1.5707963267949
// converting to degree
Console.WriteLine(rest);
// Output 1.5707963267949
Console.WriteLine(Math.Acos(e));
// input PositiveInfinity
4
// Output NaN
Console.WriteLine(Math.Acos(posi));
// input NegativeInfinity
// Output NaN
Console.WriteLine(Math.Acos(nega));
// input NaN
// Output NaN
Console.WriteLine(Math.Acos(nan));
Ejemplo:
// MathF.Acosh(Single) Method
using System;
class GFG {
// Main Method
5
{
Ejemplo:
// of Math.Asin() method
using System;
class Geeks {
// Main Method
6
public static void Main(String[] args)
double a = Math.PI;
Console.WriteLine(Math.Asin(a));
Console.WriteLine(Math.Asin(2));
Console.WriteLine(Math.Asin(0.3584));
double d = 0.0;
double e = -0.0;
// Output 0
// converting to degree
7
double rest = res * (180 / Math.PI);
Console.WriteLine(rest);
// Output 0
Console.WriteLine(Math.Asin(e));
// input PositiveInfinity
// Output NaN
Console.WriteLine(Math.Asin(posi));
// input NegativeInfinity
// Output NaN
Console.WriteLine(Math.Asin(nega));
// input NaN
// Output NaN
Console.WriteLine(Math.Asin(nan));
Math.Asinh(Double d)
Ejemplo:
8
// C# program to demonstrate the
// MathF.Asinh(Single) Method
using System;
class GFG {
// Main Method
9
Ejemplo
// of Math.Atan() method
using System;
class Geeks {
// Main Method
double a = Math.PI;
Console.WriteLine(Math.Atan(a));
double d = 0.0;
double e = -0.0;
Console.WriteLine(Math.Atan(1));
10
// Output positive zero
Console.WriteLine(Math.Atan(d));
Console.WriteLine(Math.Atan(e));
// input PositiveInfinity
// Output 1.5707963267949
Console.WriteLine(Math.Atan(posi));
// input NegativeInfinity
// Output -1.5707963267949
Console.WriteLine(Math.Atan(nega));
// input NaN
// Output NaN
Console.WriteLine(Math.Atan(nan));
Ejemplo:
11
// C# program to demonstrate the
using System;
class Geeks {
// Main method
Math.Atanh(Double d);
Ejemplo:
using System;
class Example {
Double d, result;
12
d = Double.NegativeInfinity;
result = Math.Atanh(d);
d = -5;
result = Math.Atanh(d);
d = -1;
result = Math.Atanh(d);
d = 0;
result = Math.Atanh(d);
d = 1;
result = Math.Atanh(d);
d = 5;
result = Math.Atanh(d);
13
d = Double.PositiveInfinity;
result = Math.Atanh(d);
Ejemplo:
// Math.BigMul() method
using System;
namespace ConsoleApplication1 {
class Geeks {
// Main Method
// System.Int32
Int32 x1 = 233232322;
14
Int32 x2 = 189222338;
Math.BitDecrement(Double x);
Ejemplo:
using System;
class Example {
Double x, result;
x = 12.56;
result = Math.BitDecrement(x);
15
Console.WriteLine($"BitDecrement({x}) = {result}");
x = 10;
result = Math.BitDecrement(x);
Console.WriteLine($"BitDecrement({x}) = {result}");
Math.BitIncrement(Double x);
Ejemplo:
using System;
class Example {
Double x, result;
x = 12.56;
result = Math.BitIncrement(x);
Console.WriteLine($"BitIncrement({x}) = {result}");
x = 10;
result = Math.BitIncrement(x);
Console.WriteLine($"BitIncrement({x}) = {result}");
16
}
Math.Cbrt(Double d);
Ejemplo:
using System;
class Example {
Double d, result;
d = 2;
result = Math.Cbrt(d);
Console.WriteLine($"Cbrt({d}) = {result}");
d = 3;
result = Math.Cbrt(d);
Console.WriteLine($"Cbrt({d}) = {result}");
d = 4;
result = Math.Cbrt(d);
Console.WriteLine($"Cbrt({d}) = {result}");
17
d = 27;
result = Math.Cbrt(d);
Console.WriteLine($"Cbrt({d}) = {result}");
d = 64;
result = Math.Cbrt(d);
Console.WriteLine($"Cbrt({d}) = {result}");
d = 0.008;
result = Math.Cbrt(d);
Console.WriteLine($"Cbrt({d}) = {result}");
d = -27;
result = Math.Cbrt(d);
Console.WriteLine($"Cbrt({d}) = {result}");
d = Double.NegativeInfinity;
result = Math.Cbrt(d);
Console.WriteLine($"Cbrt({d}) = {result}");
d = Double.PositiveInfinity;
result = Math.Cbrt(d);
Console.WriteLine($"Cbrt({d}) = {result}");
}
18
Método Math.Ceiling: Devuelve el menor valor integral mayor o igual
que el número especificado.
Ejemplo:
// Math.Ceiling(Decimal) function
using System;
class SudoPlacement {
// Main method
19
// Print First values and Ceiling
Ejemplo:
using System;
class MyProgram {
20
Console.WriteLine("Math.Clamp(25, 0, 50): "+
Math.Clamp(25, 0, 50));
Ejemplo:
using System;
class Example {
Double x, y, result;
x = 2;
y = -8;
x = 2;
y = 8;
21
Console.WriteLine($"CopySign({x}, {y}) = {result}");
x = -2;
y = 8;
Ejemplo:
// Math.Cos() method
using System;
class Geeks {
// Main Method
double a = 70;
22
double b = (a * (Math.PI)) / 180;
Console.WriteLine(Math.Cos(b));
a = 50;
b = (a * (Math.PI)) / 180;
Console.WriteLine(Math.Cos(b));
a = 73;
b = (a * (Math.PI)) / 180;
Console.WriteLine(Math.Cos(b));
a = 77;
b = (a * (Math.PI)) / 180;
23
Console.WriteLine(Math.Cos(b));
Ejemplo:
// Math.Cosh()
using System;
class GFG {
// Main Method
24
coshvalue = Math.Cosh(num2);
coshvalue = Math.Cosh(num3);
Ejemplo:
using System;
class DivRemGeeks {
// Main method
int divisor_A = 7;
25
// Input negative dividend and divisor number
int divisor_B = 2;
int result_1;
int result_2;
// Print Result
Console.WriteLine(quotient_ONE);
Console.WriteLine(result_1);
Console.WriteLine(quotient_TWO);
Console.WriteLine(result_2);
Ejemplo:
26
// C# Program to illustrate the
// Math.Exp(Double) Method
using System;
class Geeks {
// Main Method
Console.WriteLine(Math.Exp(10.0));
Console.WriteLine(Math.Exp(15.57));
Console.WriteLine(Math.Exp(529.548));
Console.WriteLine(Math.Exp(0.00));
Ejemplo:
// Math.Floor(Decimal) function
using System;
27
public class GFG {
// Main method
Console.WriteLine(Math.Floor(0.2018));
Console.WriteLine(Math.Floor(123.123));
Console.WriteLine(Math.Floor(-0.2));
Console.WriteLine(Math.Floor(0.0));
Console.WriteLine(Math.Floor(34.67M));
Ejemplo:
using System;
class Example {
28
Double x, y, z, result;
x = 2;
y = 3;
z = 1;
x = 10;
y = 5;
z = 3;
Ejemplo:
// Math.IEEERemainder() Method
using System;
class Geeks
29
{
// Main Method
30
// calling the method
DisplayRemainder(0, 1);
DisplayRemainder(-4, 8);
DisplayRemainder(1, 0);
DisplayRemainder(-1, -0);
DisplayRemainder(145, 7);
DisplayRemainder(18.52, 2);
DisplayRemainder(42.26, 4.2);
Math.ILogB(Double x);
Ejemplo:
using System;
class Example {
Double x;
Double result;
x = 2;
Console.WriteLine($"ILogB({x}) = {result}");
31
x = 3;
Console.WriteLine($"ILogB({x}) = {result}");
x = 4;
Console.WriteLine($"ILogB({x}) = {result}");
Ejemplo:
// of Math.Log(Double) method
using System;
class Geeks {
// Main Method
32
// double values whose logarithm
// to be calculated
double a = 4.55;
double b = 0;
double c = -2.45;
Console.WriteLine(Math.Log(a));
// will be -Infinity
Console.WriteLine(Math.Log(b));
// will be NaN
Console.WriteLine(Math.Log(c));
// will be NaN
Console.WriteLine(Math.Log(nan));
33
// Input is PositiveInfinity so output
// will be Infinity
Console.WriteLine(Math.Log(positiveInfinity));
// will be NaN
Console.WriteLine(Math.Log(negativeInfinity));
Ejemplo:
// of Math.Log10(Double) method
using System;
class Geeks {
// Main Method
34
// double values whose logarithm
// to be calculated
double a = 4.55;
double b = 0;
double c = -2.45;
Console.WriteLine(Math.Log10(a));
// will be -Infinity
Console.WriteLine(Math.Log10(b));
// will be NaN
Console.WriteLine(Math.Log10(c));
// will be NaN
Console.WriteLine(Math.Log10(nan));
35
// Input is PositiveInfinity so output
// will be Infinity
Console.WriteLine(Math.Log10(positiveInfinity));
// will be NaN
Console.WriteLine(Math.Log10(negativeInfinity));
Ejemplo:
using System;
class MyProgram {
Console.WriteLine("Math.Log2(0) = "
+ Math.Log2(0));
Console.WriteLine("Math.Log2(10) = "
+ Math.Log2(10));
Console.WriteLine("Math.Log2(50) = "
+ Math.Log2(50));
36
Console.WriteLine("Math.Log2(Double.NaN) = "
+ Math.Log2(Double.NaN));
Console.WriteLine("Math.Log2(Double.NegativeInfinity) = "
+ Math.Log2(Double.NegativeInfinity));
Console.WriteLine("Math.Log2(Double.PositiveInfinity) = "
+ Math.Log2(Double.PositiveInfinity));
Ejemplo:
// Math.Max() method
using System;
class GFG {
// Main Method
37
// decimal data type
// displaying result
39
Console.WriteLine("Math.Max Method (UInt32, UInt32) = " +
Math.Max(un1, un2));
Ejemplo:
using System;
class Educative
40
}
Ejemplo:
// Math.Min() method
using System;
class GFG {
// Main Method
41
// Int16 data type
// displaying result
}
43
}
Ejemplo:
// Math.Min() method
using System;
class GFG {
// Main Method
44
// Int16 data type
45
// displaying result
46
Método Math.Pow: Devuelve un número especificado elevado a la
potencia especificada.
Ejemplo:
// Math.Pow() function
using System;
class GFG {
// Main Method
Console.WriteLine(pow_ab);
47
// Print the result
Console.WriteLine(pow_tt);
Console.WriteLine(pow_t);
Ejemplo:
// Computes an approximate of 1 / x
48
// New overloads
// Min, Max, Abs, Clamp and Sign supports nint and nuint
Ejemplo
// Computes an approximate of 1 / x
49
double recEst = Math.ReciprocalEstimate(5);
// New overloads
// Min, Max, Abs, Clamp and Sign supports nint and nuint
// Output:
// Sin = 0.9999996829318346
50
// Cos = 0.0007963267107331026
// Min = 5
// Max = 10
// Abs = 5
// Clamp = 5
// Sign = 1
// Quotient = 0
// Remainder = 2
Ejemplo:
// Math.Round(Double) method
using System;
class Geeks {
// Main method
51
// Case-1
// consecutive integers
// Case-2
// consecutive integers
52
Ejemplo:
using System;
class MyProgram {
Console.WriteLine("Math.ScaleB(2.55, 4) = "
+ Math.ScaleB(2.55, 4));
Console.WriteLine("Math.ScaleB(10, 3) = "
+ Math.ScaleB(10, 3));
Console.WriteLine("Math.ScaleB(Double.NaN, 3) = "
+ Math.ScaleB(Double.NaN, 3));
Console.WriteLine("Math.ScaleB(Double.PositiveInfinity, 3) = "
+ Math.ScaleB(Double.PositiveInfinity, 3));
Ejemplo:
// Math.Sign() method
using System;
class GFG {
53
// Main Method
Decimal de = 150M;
Double d = 34.5432d;
short sh = 0;
int i = -5678;
long l = 598964564;
sbyte sb = -34;
float f = 56.89f;
54
// displaying result
if (compare == 0)
else
55
return "greater than zero";
Ejemplo:
// Math.Sin() method
using System;
class Geeks {
// Main Method
double a = 30;
Console.WriteLine(Math.Sin(b));
a = 45;
56
// converting value to radians
b = (a * (Math.PI)) / 180;
Console.WriteLine(Math.Sin(b));
a = 60;
b = (a * (Math.PI)) / 180;
Console.WriteLine(Math.Sin(b));
a = 90;
b = (a * (Math.PI)) / 180;
Console.WriteLine(Math.Sin(b));
57
public static (double Sin, double Cos) SinCos (double x);
Ejemplo:
using System;/* ww w .d em o 2 s . c o m */
class SinCos
Console.WriteLine(
Console.WriteLine(
UseSineCosine(15.0);
UseSineCosine(30.0);
UseSineCosine(45.0);
Console.WriteLine(
58
"\nConvert selected values for X and Y to radians \n" +
UseTwoAngles(15.0, 30.0);
UseTwoAngles(30.0, 45.0);
Console.WriteLine(
UseCombinedSineCosine(15.0);
UseCombinedSineCosine(30.0);
UseCombinedSineCosine(45.0);
Console.WriteLine(
Console.WriteLine(
59
degrees );
Console.WriteLine(
Console.WriteLine(
Console.WriteLine(
Console.WriteLine(
Console.WriteLine(
60
" 2 * Math.Sin({0} deg) * Math.Cos({0} deg) == {1:E16}",
Console.WriteLine(
Console.WriteLine(
Console.WriteLine(
Math.Cos(angleX) * Math.Sin(angleY));
Console.WriteLine(
61
// Evaluate cos(X + Y) == cos(X) * cos(Y) - sin(X) * sin(Y).
Console.WriteLine(
Math.Sin(angleX) * Math.Sin(angleY));
Console.WriteLine(
Ejemplo:
// Math.Sinh()
using System;
class GFG {
// Main Method
62
double num1 = 78.8, num2 = 0.0, num3 = 1.0;
// angle in radian
sinhvalue = Math.Sinh(num2);
sinhvalue = Math.Sinh(num3);
Ejemplo:
using System;
class Educative
63
Double result = Math.Sqrt(-0.16);
Ejemplo:
// Math.Tan() method
using System;
64
class Geeks {
// Main Method
double a = 12;
Console.WriteLine(Math.Tan(b));
a = 63;
b = (a * (Math.PI)) / 180;
Console.WriteLine(Math.Tan(b));
a = 187;
b = (a * (Math.PI)) / 180;
65
// using method and displaying result
Console.WriteLine(Math.Tan(b));
a = 45;
b = (a * (Math.PI)) / 180;
Console.WriteLine(Math.Tan(b));
Math.Tanh(Double value);
Ejemplo:
using System;
class Example {
result = Math.Tanh(value);
66
value = Math.PI; // PI radians or 180 degrees
result = Math.Tanh(value);
result = Math.Tanh(value);
value = 0; // 0 degrees
result = Math.Tanh(value);
value = Double.PositiveInfinity;
result = Math.Tanh(value);
value = Double.NegativeInfinity;
result = Math.Tanh(value);
// Math.Truncate(Decimal) Method
using System;
class Geeks {
// Main Method
Console.WriteLine(Math.Truncate(dec));
Console.WriteLine(Math.Truncate(dec2));
68