Home  >  Article  >  Backend Development  >  What are the common methods in the go language math package?

What are the common methods in the go language math package?

王林
王林Original
2020-12-17 10:56:245596browse

Commonly used methods in the go language math package are: 1. Absolute value method Abs(); 2. Power method Pow(); 3. Square root method Sqrt(); 4. Cube root method Cbrt(); 5. Up rounding method Ceil(); 6. Down rounding method Floor().

What are the common methods in the go language math package?

The environment of this article: Windows 10 system, Go 1.11.2 version, this article is applicable to all brands of computers.

(Learning video sharing: Programming video)

Detailed introduction:

The commonly used methods in the math package are as follows:

package main

import (
    "fmt"
    "math"
)

func main() {
    /*
        取绝对值,函数签名如下:
            func Abs(x float64) float64
    */
    fmt.Printf("[-3.14]的绝对值为:[%.2f]\n", math.Abs(-3.14))

    /*
        取x的y次方,函数签名如下:
            func Pow(x, y float64) float64
    */
    fmt.Printf("[2]的16次方为:[%.f]\n", math.Pow(2, 16))

    /*
        取余数,函数签名如下:
            func Pow10(n int) float64
    */
    fmt.Printf("10的[3]次方为:[%.f]\n", math.Pow10(3))

    /*
        取x的开平方,函数签名如下:
            func Sqrt(x float64) float64
    */
    fmt.Printf("[64]的开平方为:[%.f]\n", math.Sqrt(64))

    /*
        取x的开立方,函数签名如下:
            func Cbrt(x float64) float64
    */
    fmt.Printf("[27]的开立方为:[%.f]\n", math.Cbrt(27))

    /*
        向上取整,函数签名如下:
            func Ceil(x float64) float64
    */
    fmt.Printf("[3.14]向上取整为:[%.f]\n", math.Ceil(3.14))

    /*
        向下取整,函数签名如下:
            func Floor(x float64) float64
    */
    fmt.Printf("[8.75]向下取整为:[%.f]\n", math.Floor(8.75))

    /*
        取余数,函数签名如下:
            func Floor(x float64) float64
    */
    fmt.Printf("[10/3]的余数为:[%.f]\n", math.Mod(10, 3))

    /*
        分别取整数和小数部分,函数签名如下:
            func Modf(f float64) (int float64, frac float64)
    */
    Integer, Decimal := math.Modf(3.14159265358979)
    fmt.Printf("[3.14159265358979]的整数部分为:[%.f],小数部分为:[%.14f]\n", Integer, Decimal)

}

Look at the picture:

What are the common methods in the go language math package?

Related recommendations: golang tutorial

The above is the detailed content of What are the common methods in the go language math package?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn