How to use math functions and constants in VB.NET

1 Answer

0 votes
' Program Title: VB.NET Math Functions Demonstration

Imports System

Module Program
    Sub Main()

        ' ============================
        ' Constants
        ' ============================
        Dim pi As Double = Math.PI        ' π constant
        Dim e As Double = Math.E          ' Euler's number

        Console.WriteLine("Constants:")
        Console.WriteLine("  pi = " & pi)
        Console.WriteLine("  e  = " & e)
        Console.WriteLine()


        ' ============================
        ' Core Functions
        ' ============================
        Dim x As Double = 2.5             ' sample value

        Console.WriteLine("Core Functions:")
        Console.WriteLine("  Sqrt(2.5) = " & Math.Sqrt(x))     ' square root
        Console.WriteLine("  Exp(2.5)  = " & Math.Exp(x))      ' e^x
        Console.WriteLine("  Log(2.5)  = " & Math.Log(x))      ' natural log
        Console.WriteLine("  Log10(2.5)= " & Math.Log10(x))    ' base-10 log
        Console.WriteLine("  Pow(2.5,3)= " & Math.Pow(x, 3))   ' x^3
        Console.WriteLine()


        ' ============================
        ' Trigonometric Functions
        ' ============================
        Dim angle As Double = Math.PI / 4 ' 45 degrees in radians

        Console.WriteLine("Trigonometric Functions:")
        Console.WriteLine("  Sin(pi/4) = " & Math.Sin(angle))  ' sine
        Console.WriteLine("  Cos(pi/4) = " & Math.Cos(angle))  ' cosine
        Console.WriteLine("  Tan(pi/4) = " & Math.Tan(angle))  ' tangent
        Console.WriteLine("  Asin(0.5) = " & Math.Asin(0.5))   ' arcsine
        Console.WriteLine("  Acos(0.5) = " & Math.Acos(0.5))   ' arccosine
        Console.WriteLine("  Atan(1.0) = " & Math.Atan(1.0))   ' arctangent
        Console.WriteLine()


        ' ============================
        ' Hyperbolic Functions
        ' ============================
        Console.WriteLine("Hyperbolic Functions:")
        Console.WriteLine("  Sinh(1) = " & Math.Sinh(1))      ' hyperbolic sine
        Console.WriteLine("  Cosh(1) = " & Math.Cosh(1))      ' hyperbolic cosine
        Console.WriteLine("  Tanh(1) = " & Math.Tanh(1))      ' hyperbolic tangent
        Console.WriteLine()


        ' ============================
        ' Rounding Functions
        ' ============================
        Dim y As Double = -2.7            ' negative number for rounding tests

        Console.WriteLine("Rounding Functions:")
        Console.WriteLine("  Floor(-2.7) = " & Math.Floor(y)) ' round down
        Console.WriteLine("  Ceiling(-2.7)= " & Math.Ceiling(y)) ' round up
        Console.WriteLine("  Round(-2.7) = " & Math.Round(y)) ' nearest integer
        Console.WriteLine("  Truncate(-2.7) = " & Math.Truncate(y)) ' remove decimals
        Console.WriteLine()


        ' ============================
        ' Min / Max / Clamp
        ' ============================
        Dim m As Double = 10
        Dim n As Double = 20
        Dim value As Double = 15

        Console.WriteLine("Min/Max/Clamp:")
        Console.WriteLine("  Min(10,20) = " & Math.Min(m, n)) ' smaller of two
        Console.WriteLine("  Max(10,20) = " & Math.Max(m, n)) ' larger of two

        ' clamp logic using Math.Min/Max
        Dim clamped As Double = Math.Max(0, Math.Min(10, value)) ' clamp 15 to range 0–10
        Console.WriteLine("  Clamp(15,0,10) = " & clamped)
        Console.WriteLine()


        ' ============================
        ' Bitwise Math (Integers)
        ' ============================
        Dim A As Byte = &HAA               ' 10101010 in hex
        Dim B As Byte = &HCC               ' 11001100 in hex

        Console.WriteLine("Bitwise Math:")
        Console.WriteLine("  A AND B = 0x" & (A And B).ToString("X2")) ' bitwise AND
        Console.WriteLine("  A OR B  = 0x" & (A Or B).ToString("X2"))  ' bitwise OR
        Console.WriteLine("  A XOR B = 0x" & (A Xor B).ToString("X2")) ' bitwise XOR
        Console.WriteLine("  NOT A   = 0x" & (Not A).ToString("X2"))   ' bitwise NOT
        Console.WriteLine("  A << 2  = 0x" & (A << 2).ToString("X2"))  ' left shift
        Console.WriteLine("  B >> 3  = 0x" & (B >> 3).ToString("X2"))  ' right shift
        Console.WriteLine()


        ' ============================
        ' Additional Useful Math
        ' ============================
        Console.WriteLine("Additional Math:")
        Console.WriteLine("  Abs(-3.14) = " & Math.Abs(-3.14)) ' absolute value
        Console.WriteLine("  IEEERemainder(10,3) = " & Math.IEEERemainder(10, 3)) ' IEEE remainder
        Console.WriteLine("  Sqrt(3^2 + 4^2) = " & Math.Sqrt(3 * 3 + 4 * 4)) ' manual hypot
        Console.WriteLine("  Exp(4) = " & Math.Exp(4)) ' e^4

    End Sub
End Module


' run:
'
' Constants:
'  pi = 3.14159265358979
'  e  = 2.71828182845905
'
' Core Functions:
'  Sqrt(2.5) = 1.58113883008419
'  Exp(2.5)  = 12.1824939607035
'  Log(2.5)  = 0.916290731874155
'  Log10(2.5)= 0.397940008672038
'  Pow(2.5,3)= 15.625
'
' Trigonometric Functions:
'  Sin(pi/4) = 0.707106781186548
'  Cos(pi/4) = 0.707106781186548
'  Tan(pi/4) = 1
'  Asin(0.5) = 0.523598775598299
'  Acos(0.5) = 1.0471975511966
'  Atan(1.0) = 0.785398163397448
'
' Hyperbolic Functions:
'  Sinh(1) = 1.1752011936438
'  Cosh(1) = 1.54308063481524
'  Tanh(1) = 0.761594155955765
'
' Rounding Functions:
'  Floor(-2.7) = -3
'  Ceiling(-2.7)= -2
'  Round(-2.7) = -3
'  Truncate(-2.7) = -2
'
' Min/Max/Clamp:
'  Min(10,20) = 10
'  Max(10,20) = 20
'  Clamp(15,0,10) = 10
'
' Bitwise Math:
'  A AND B = 0x88
'  A OR B  = 0xEE
'  A XOR B = 0x66
'  NOT A   = 0x55
'  A << 2  = 0xA8
'  B >> 3  = 0x19
'
' Additional Math:
'  Abs(-3.14) = 3.14
'  IEEERemainder(10,3) = 1
'  Sqrt(3^2 + 4^2) = 5
'  Exp(4) = 54.5981500331442
'

 



answered Apr 30 by avibootz
edited Apr 30 by avibootz
...