Skip to content

Commit 7037626

Browse files
Youssef1313mairaw
andcommitted
Update math-functions.md (#16436)
* Update math-functions.md * Add methods that are new in .NET Core and .NET Standard * Use Console.WriteLine * Fix table * Update math-functions.md * update ms.date Co-authored-by: Maira Wenzel <mairaw@microsoft.com>
1 parent 0cfcc69 commit 7037626

File tree

1 file changed

+184
-143
lines changed

1 file changed

+184
-143
lines changed
Lines changed: 184 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: "Math Functions"
3-
ms.date: 07/20/2015
2+
title: Math functions
3+
ms.date: 01/27/2020
44
helpviewer_keywords:
55
- "math functions, Visual Basic"
66
- "arithmetic operations, math functions"
@@ -9,161 +9,202 @@ helpviewer_keywords:
99
ms.assetid: 4d2d82e7-6924-42fe-a4a7-b4dd5bebbd0c
1010
---
1111
# Math Functions (Visual Basic)
12-
The methods of the <xref:System.Math?displayProperty=nameWithType> class provide trigonometric, logarithmic, and other common mathematical functions.
13-
14-
## Remarks
15-
The following table lists methods of the <xref:System.Math?displayProperty=nameWithType> class. You can use these in a Visual Basic program.
16-
17-
|.NET method|Description|
18-
|---------------------------|-----------------|
19-
|<xref:System.Math.Abs%2A>|Returns the absolute value of a number.|
20-
|<xref:System.Math.Acos%2A>|Returns the angle whose cosine is the specified number.|
21-
|<xref:System.Math.Asin%2A>|Returns the angle whose sine is the specified number.|
22-
|<xref:System.Math.Atan%2A>|Returns the angle whose tangent is the specified number.|
23-
|<xref:System.Math.Atan2%2A>|Returns the angle whose tangent is the quotient of two specified numbers.|
24-
|<xref:System.Math.BigMul%2A>|Returns the full product of two 32-bit numbers.|
25-
|<xref:System.Math.Ceiling%2A>|Returns the smallest integral value that's greater than or equal to the specified `Decimal` or `Double`.|
26-
|<xref:System.Math.Cos%2A>|Returns the cosine of the specified angle.|
27-
|<xref:System.Math.Cosh%2A>|Returns the hyperbolic cosine of the specified angle.|
28-
|<xref:System.Math.DivRem%2A>|Returns the quotient of two 32-bit or 64-bit signed integers, and also returns the remainder in an output parameter.|
29-
|<xref:System.Math.Exp%2A>|Returns e (the base of natural logarithms) raised to the specified power.|
30-
|<xref:System.Math.Floor%2A>|Returns the largest integer that's less than or equal to the specified `Decimal` or `Double` number.|
31-
|<xref:System.Math.IEEERemainder%2A>|Returns the remainder that results from the division of a specified number by another specified number.|
32-
|<xref:System.Math.Log%2A>|Returns the natural (base e) logarithm of a specified number or the logarithm of a specified number in a specified base.|
33-
|<xref:System.Math.Log10%2A>|Returns the base 10 logarithm of a specified number.|
34-
|<xref:System.Math.Max%2A>|Returns the larger of two numbers.|
35-
|<xref:System.Math.Min%2A>|Returns the smaller of two numbers.|
36-
|<xref:System.Math.Pow%2A>|Returns a specified number raised to the specified power.|
37-
|<xref:System.Math.Round%2A>|Returns a `Decimal` or `Double` value rounded to the nearest integral value or to a specified number of fractional digits.|
38-
|<xref:System.Math.Sign%2A>|Returns an `Integer` value indicating the sign of a number.|
39-
|<xref:System.Math.Sin%2A>|Returns the sine of the specified angle.|
40-
|<xref:System.Math.Sinh%2A>|Returns the hyperbolic sine of the specified angle.|
41-
|<xref:System.Math.Sqrt%2A>|Returns the square root of a specified number.|
42-
|<xref:System.Math.Tan%2A>|Returns the tangent of the specified angle.|
43-
|<xref:System.Math.Tanh%2A>|Returns the hyperbolic tangent of the specified angle.|
44-
|<xref:System.Math.Truncate%2A>|Calculates the integral part of a specified `Decimal` or `Double` number.|
45-
46-
To use these functions without qualification, import the <xref:System.Math?displayProperty=nameWithType> namespace into your project by adding the following code to the top of your source file:
47-
12+
13+
The methods of the <xref:System.Math?displayProperty=nameWithType> class provide trigonometric, logarithmic, and other common mathematical functions.
14+
15+
## Remarks
16+
17+
The following table lists methods of the <xref:System.Math?displayProperty=nameWithType> class. You can use these in a Visual Basic program:
18+
19+
|.NET method|Description|
20+
|---------------------------|-----------------|
21+
|<xref:System.Math.Abs%2A>|Returns the absolute value of a number.|
22+
|<xref:System.Math.Acos%2A>|Returns the angle whose cosine is the specified number.|
23+
|<xref:System.Math.Asin%2A>|Returns the angle whose sine is the specified number.|
24+
|<xref:System.Math.Atan%2A>|Returns the angle whose tangent is the specified number.|
25+
|<xref:System.Math.Atan2%2A>|Returns the angle whose tangent is the quotient of two specified numbers.|
26+
|<xref:System.Math.BigMul%2A>|Returns the full product of two 32-bit numbers.|
27+
|<xref:System.Math.Ceiling%2A>|Returns the smallest integral value that's greater than or equal to the specified `Decimal` or `Double`.|
28+
|<xref:System.Math.Cos%2A>|Returns the cosine of the specified angle.|
29+
|<xref:System.Math.Cosh%2A>|Returns the hyperbolic cosine of the specified angle.|
30+
|<xref:System.Math.DivRem%2A>|Returns the quotient of two 32-bit or 64-bit signed integers, and also returns the remainder in an output parameter.|
31+
|<xref:System.Math.Exp%2A>|Returns e (the base of natural logarithms) raised to the specified power.|
32+
|<xref:System.Math.Floor%2A>|Returns the largest integer that's less than or equal to the specified `Decimal` or `Double` number.|
33+
|<xref:System.Math.IEEERemainder%2A>|Returns the remainder that results from the division of a specified number by another specified number.|
34+
|<xref:System.Math.Log%2A>|Returns the natural (base e) logarithm of a specified number or the logarithm of a specified number in a specified base.|
35+
|<xref:System.Math.Log10%2A>|Returns the base 10 logarithm of a specified number.|
36+
|<xref:System.Math.Max%2A>|Returns the larger of two numbers.|
37+
|<xref:System.Math.Min%2A>|Returns the smaller of two numbers.|
38+
|<xref:System.Math.Pow%2A>|Returns a specified number raised to the specified power.|
39+
|<xref:System.Math.Round%2A>|Returns a `Decimal` or `Double` value rounded to the nearest integral value or to a specified number of fractional digits.|
40+
|<xref:System.Math.Sign%2A>|Returns an `Integer` value indicating the sign of a number.|
41+
|<xref:System.Math.Sin%2A>|Returns the sine of the specified angle.|
42+
|<xref:System.Math.Sinh%2A>|Returns the hyperbolic sine of the specified angle.|
43+
|<xref:System.Math.Sqrt%2A>|Returns the square root of a specified number.|
44+
|<xref:System.Math.Tan%2A>|Returns the tangent of the specified angle.|
45+
|<xref:System.Math.Tanh%2A>|Returns the hyperbolic tangent of the specified angle.|
46+
|<xref:System.Math.Truncate%2A>|Calculates the integral part of a specified `Decimal` or `Double` number.|
47+
48+
The following table lists methods of the <xref:System.Math?displayProperty=nameWithType> class that don't exist in .NET Framework but are added in .NET Standard or .NET Core:
49+
50+
|.NET method|Description|Available in|
51+
|---------------------------|-----------------|-----------|
52+
|<xref:System.Math.Acosh%2A>|Returns the angle whose hyperbolic cosine is the specified number.|Starting with .NET Core 2.1 and .NET Standard 2.1|
53+
|<xref:System.Math.Asinh%2A>|Returns the angle whose hyperbolic sine is the specified number.|Starting with .NET Core 2.1 and .NET Standard 2.1|
54+
|<xref:System.Math.Atanh%2A>|Returns the angle whose hyperbolic tangent is the specified number.|Starting with .NET Core 2.1 and .NET Standard 2.1|
55+
|<xref:System.Math.BitDecrement%2A>|Returns the next smallest value that compares less than `x`.|Starting with .NET Core 3.0|
56+
|<xref:System.Math.BitIncrement%2A>|Returns the next largest value that compares greater than `x`.|Starting with .NET Core 3.0|
57+
|<xref:System.Math.Cbrt%2A>|Returns the cube root of a specified number.|Starting with .NET Core 2.1 and .NET Standard 2.1|
58+
|<xref:System.Math.Clamp%2A>|Returns `value` clamped to the inclusive range of `min` and `max`.|Starting with .NET Core 2.0 and .NET Standard 2.1|
59+
|<xref:System.Math.CopySign%2A>|Returns a value with the magnitude of `x` and the sign of `y`.|Starting with .NET Core 3.0|
60+
|<xref:System.Math.FusedMultiplyAdd%2A>|Returns (x \* y) + z, rounded as one ternary operation.|Starting with .NET Core 3.0|
61+
|<xref:System.Math.ILogB%2A>|Returns the base 2 integer logarithm of a specified number.|Starting with .NET Core 3.0|
62+
|<xref:System.Math.Log2%2A>|Returns the base 2 logarithm of a specified number.|Starting with .NET Core 3.0|
63+
|<xref:System.Math.MaxMagnitude%2A>|Returns the larger magnitude of two double-precision floating-point numbers.|Starting with .NET Core 3.0|
64+
|<xref:System.Math.MinMagnitude%2A>|Returns the smaller magnitude of two double-precision floating-point numbers.|Starting with .NET Core 3.0|
65+
|<xref:System.Math.ScaleB%2A>|Returns x \* 2^n computed efficiently.|Starting with .NET Core 3.0|
66+
67+
To use these functions without qualification, import the <xref:System.Math?displayProperty=nameWithType> namespace into your project by adding the following code to the top of your source file:
68+
4869
```vb
49-
Imports System.Math
50-
```
51-
52-
## Example
53-
This example uses the <xref:System.Math.Abs%2A> method of the <xref:System.Math> class to compute the absolute value of a number.
54-
70+
Imports System.Math
71+
```
72+
73+
## Example - Abs
74+
75+
This example uses the <xref:System.Math.Abs%2A> method of the <xref:System.Math> class to compute the absolute value of a number.
76+
5577
```vb
56-
' Returns 50.3.
57-
Dim MyNumber1 As Double = Math.Abs(50.3)
58-
' Returns 50.3.
59-
Dim MyNumber2 As Double = Math.Abs(-50.3)
78+
Dim x As Double = Math.Abs(50.3)
79+
Dim y As Double = Math.Abs(-50.3)
80+
Console.WriteLine(x)
81+
Console.WriteLine(y)
82+
' This example produces the following output:
83+
' 50.3
84+
' 50.3
6085
```
61-
62-
## Example
63-
This example uses the <xref:System.Math.Atan%2A> method of the <xref:System.Math> class to calculate the value of pi.
64-
86+
87+
## Example - Atan
88+
89+
This example uses the <xref:System.Math.Atan%2A> method of the <xref:System.Math> class to calculate the value of pi.
90+
6591
```vb
66-
Public Function GetPi() As Double
67-
' Calculate the value of pi.
68-
Return 4.0 * Math.Atan(1.0)
69-
End Function
70-
```
71-
72-
## Example
73-
This example uses the <xref:System.Math.Cos%2A> method of the <xref:System.Math> class to return the cosine of an angle.
74-
92+
Public Function GetPi() As Double
93+
' Calculate the value of pi.
94+
Return 4.0 * Math.Atan(1.0)
95+
End Function
96+
```
97+
98+
> [!NOTE]
99+
> The <xref:System.Math?displayProperty=nameWithType> class contains <xref:System.Math.PI?displayProperty=nameWithType> constant field. You can use it rather than calculating it.
100+
101+
## Example - Cos
102+
103+
This example uses the <xref:System.Math.Cos%2A> method of the <xref:System.Math> class to return the cosine of an angle.
104+
75105
```vb
76-
Public Function Sec(ByVal angle As Double) As Double
77-
' Calculate the secant of angle, in radians.
78-
Return 1.0 / Math.Cos(angle)
79-
End Function
80-
```
81-
82-
## Example
83-
This example uses the <xref:System.Math.Exp%2A> method of the <xref:System.Math> class to return e raised to a power.
84-
106+
Public Function Sec(angle As Double) As Double
107+
' Calculate the secant of angle, in radians.
108+
Return 1.0 / Math.Cos(angle)
109+
End Function
110+
```
111+
112+
## Example - Exp
113+
114+
This example uses the <xref:System.Math.Exp%2A> method of the <xref:System.Math> class to return e raised to a power.
115+
85116
```vb
86-
Public Function Sinh(ByVal angle As Double) As Double
87-
' Calculate hyperbolic sine of an angle, in radians.
88-
Return (Math.Exp(angle) - Math.Exp(-angle)) / 2.0
89-
End Function
90-
```
91-
92-
## Example
93-
This example uses the <xref:System.Math.Log%2A> method of the <xref:System.Math> class to return the natural logarithm of a number.
94-
117+
Public Function Sinh(angle As Double) As Double
118+
' Calculate hyperbolic sine of an angle, in radians.
119+
Return (Math.Exp(angle) - Math.Exp(-angle)) / 2.0
120+
End Function
121+
```
122+
123+
## Example - Log
124+
125+
This example uses the <xref:System.Math.Log%2A> method of the <xref:System.Math> class to return the natural logarithm of a number.
126+
95127
```vb
96-
Public Function Asinh(ByVal value As Double) As Double
97-
' Calculate inverse hyperbolic sine, in radians.
98-
Return Math.Log(value + Math.Sqrt(value * value + 1.0))
99-
End Function
100-
```
101-
102-
## Example
103-
This example uses the <xref:System.Math.Round%2A> method of the <xref:System.Math> class to round a number to the nearest integer.
104-
128+
Public Function Asinh(value As Double) As Double
129+
' Calculate inverse hyperbolic sine, in radians.
130+
Return Math.Log(value + Math.Sqrt(value * value + 1.0))
131+
End Function
132+
```
133+
134+
## Example - Round
135+
136+
This example uses the <xref:System.Math.Round%2A> method of the <xref:System.Math> class to round a number to the nearest integer.
137+
105138
```vb
106-
' Returns 3.
107-
Dim MyVar2 As Double = Math.Round(2.8)
108-
```
109-
110-
## Example
111-
This example uses the <xref:System.Math.Sign%2A> method of the <xref:System.Math> class to determine the sign of a number.
112-
139+
Dim myVar2 As Double = Math.Round(2.8)
140+
Console.WriteLine(myVar2)
141+
' The code produces the following output:
142+
' 3
143+
```
144+
145+
## Example - Sign
146+
147+
This example uses the <xref:System.Math.Sign%2A> method of the <xref:System.Math> class to determine the sign of a number.
148+
113149
```vb
114-
' Returns 1.
115-
Dim MySign1 As Integer = Math.Sign(12)
116-
' Returns -1.
117-
Dim MySign2 As Integer = Math.Sign(-2.4)
118-
' Returns 0.
119-
Dim MySign3 As Integer = Math.Sign(0)
120-
```
121-
122-
## Example
123-
This example uses the <xref:System.Math.Sin%2A> method of the <xref:System.Math> class to return the sine of an angle.
124-
150+
Dim mySign1 As Integer = Math.Sign(12)
151+
Dim mySign2 As Integer = Math.Sign(-2.4)
152+
Dim mySign3 As Integer = Math.Sign(0)
153+
Console.WriteLine(mySign1)
154+
Console.WriteLine(mySign2)
155+
Console.WriteLine(mySign3)
156+
' The code produces the following output:
157+
' 1
158+
' -1
159+
' 0
160+
```
161+
162+
## Example - Sin
163+
164+
This example uses the <xref:System.Math.Sin%2A> method of the <xref:System.Math> class to return the sine of an angle.
165+
125166
```vb
126-
Public Function Csc(ByVal angle As Double) As Double
127-
' Calculate cosecant of an angle, in radians.
128-
Return 1.0 / Math.Sin(angle)
129-
End Function
130-
```
131-
132-
## Example
133-
This example uses the <xref:System.Math.Sqrt%2A> method of the <xref:System.Math> class to calculate the square root of a number.
134-
167+
Public Function Csc(angle As Double) As Double
168+
' Calculate cosecant of an angle, in radians.
169+
Return 1.0 / Math.Sin(angle)
170+
End Function
171+
```
172+
173+
## Example - Sqrt
174+
175+
This example uses the <xref:System.Math.Sqrt%2A> method of the <xref:System.Math> class to calculate the square root of a number.
176+
135177
```vb
136-
' Returns 2.
137-
Dim MySqr1 As Double = Math.Sqrt(4)
138-
' Returns 4.79583152331272.
139-
Dim MySqr2 As Double = Math.Sqrt(23)
140-
' Returns 0.
141-
Dim MySqr3 As Double = Math.Sqrt(0)
142-
' Returns NaN (not a number).
143-
Dim MySqr4 As Double = Math.Sqrt(-4)
144-
```
145-
146-
## Example
147-
This example uses the <xref:System.Math.Tan%2A> method of the <xref:System.Math> class to return the tangent of an angle.
148-
178+
Dim mySqrt1 As Double = Math.Sqrt(4)
179+
Dim mySqrt2 As Double = Math.Sqrt(23)
180+
Dim mySqrt3 As Double = Math.Sqrt(0)
181+
Dim mySqrt4 As Double = Math.Sqrt(-4)
182+
Console.WriteLine(mySqrt1)
183+
Console.WriteLine(mySqrt2)
184+
Console.WriteLine(mySqrt3)
185+
Console.WriteLine(mySqrt4)
186+
' The code produces the following output:
187+
' 2
188+
' 4.79583152331272
189+
' 0
190+
' NaN
191+
```
192+
193+
## Example - Tan
194+
195+
This example uses the <xref:System.Math.Tan%2A> method of the <xref:System.Math> class to return the tangent of an angle.
196+
149197
```vb
150-
Public Function Ctan(ByVal angle As Double) As Double
151-
' Calculate cotangent of an angle, in radians.
152-
Return 1.0 / Math.Tan(angle)
153-
End Function
154-
```
155-
156-
## Requirements
157-
**Class:** <xref:System.Math>
158-
159-
**Namespace:** <xref:System>
160-
161-
**Assembly:** mscorlib (in mscorlib.dll)
162-
198+
Public Function Ctan(angle As Double) As Double
199+
' Calculate cotangent of an angle, in radians.
200+
Return 1.0 / Math.Tan(angle)
201+
End Function
202+
```
203+
163204
## See also
164205

165206
- <xref:Microsoft.VisualBasic.VBMath.Rnd%2A>
166207
- <xref:Microsoft.VisualBasic.VBMath.Randomize%2A>
167208
- <xref:System.Double.NaN>
168-
- [Derived Math Functions](../../../visual-basic/language-reference/keywords/derived-math-functions.md)
169-
- [Arithmetic Operators](../../../visual-basic/language-reference/operators/arithmetic-operators.md)
209+
- [Derived Math Functions](../keywords/derived-math-functions.md)
210+
- [Arithmetic Operators](../operators/arithmetic-operators.md)

0 commit comments

Comments
 (0)