SoFunction
Updated on 2025-04-14

Detailed functions and example code of math module in Go language standard library

Go languagemathThe module provides a wealth of mathematical functions and constants, covering basic operations, trigonometric functions, exponents, logarithms, rounding, special values, etc. The following are core methods and examples:

1. Basic mathematical operations

Take the absolute value (onlyfloat64)。

((-3.5))   // Output: 3.5((4))      // mistake!  Required (4.0)

and

  • Mod: Take the remainder (the symbol is the same as the divisor).
  • Remainder: IEEE 754 Standard remainder (the symbol is the same as the dividend).
((10, 3))        // Output: 1((10, 3))  // Output: 1((-10, 3))       // Output: -1((-10, 3)) // Output: -1

and

Cube root and square root.

((16))  // Output: 4((27))  // Output: 3

2. Trigonometric function

All function parameters are radians (non-angle).

、、

rad :=  / 2
((rad))  // Output: 1 (approximate value)((rad))  // Output: 6.123e-17 (close to 0)

、、

Inverse trigonometric function (returns to radians).

((1))  // Output: 1.5708 (π/2)

3. Index and Logarithm

and

Natural index and logarithm.

((2))     // Output: ~7.389 (e²)((7.389)) // Output: ~2

and math.Pow10

Power operation.

((2, 3))    // Output: 8(math.Pow10(3))     // Output: 1000

math.Log10 and math.Log2

Logarithm with base 10 or 2.

(math.Log10(1000))  // Output: 3(math.Log2(8))      // Output: 3

4. Rounding and Cutoff

and

Round up and down.

((3.2))   // Output: 4((3.9))  // Output: 3

Directly cut off the decimal part.

((3.9))  // Output: 3

and

rounding(RoundToEvenRound to the nearest even number).

((2.5))          // Output: 3((2.5))    // Output: 2

5. Maximum and Minimum Values

and

Return twofloat64The maximum or minimum value of the value.

((3, 5))  // Output: 5((3, 5))  // Output: 3

6. Special value processing

and

Check whether it is non-numeric or infinity.

val := (-1)  // Generate NaN((val))  // Output: true((1/(1), 0)) // Check if it is infinite

and

Generates infinity or non-numeric values.

posInf := (1)  // It's endlessnegInf := (-1) // Negative Infinitynan := ()

7. Special functions

Calculate the bevel length of a right triangle.

((3, 4))  // Output: 5

and

Gamma function and error function.

((5))  // Output: 24 (4!)((1))    // Output: ~0.8427

8. Mathematical Constants

Commonly used constants

()     // Output: 3.141592653589793()      // Output: 2.718281828459045()    // Output: 1.618033988749895 (Golden Partition)

Summarize

  • Core functions
    • Basic operationsAbsModSqrtPow
    • Trigonometric functionSinCosAsin
    • Exponent logarithmExpLogLog10
    • Rounding processingCeilFloorRound
    • Special valueNaNInfIsNaN
    • constantPiE
  • Things to note
    • All function operationsfloat64Type (other types need to be explicitly converted).
    • The trigonometric function parameter is radian (the angle needs to be converted to radians:rad = degrees * π / 180)。
    • Handle special values ​​(such asNaN) is requiredJudgment cannot be compared directly.

This is the article about the detailed function introduction and sample code of the math module in the Go language standard library. For more information about the math module introduction of the Go language standard library, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!