搜尋
首頁後端開發Golanggo語言math套件中有哪些常用方法

go語言math套件中有哪些常用方法

Dec 17, 2020 am 10:56 AM
go語言

go語言math套件中的常用方法有:1、取絕對值方法Abs();2、冪次方方法Pow();3、開平方方法Sqrt();4、開立方方法Cbrt();5、向上取整方法Ceil();6、向下取整方法Floor()。

go語言math套件中有哪些常用方法

本文環境:windows10系統、Go 1.11.2版本,本文適用於所有品牌的電腦。

(學習影片分享:程式設計影片

詳細介紹:

math套件中常用的方法如下所示:

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)

}

看圖:

go語言math套件中有哪些常用方法

相關推薦:golang教學

以上是go語言math套件中有哪些常用方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
去其他語言:比較分析去其他語言:比較分析Apr 28, 2025 am 12:17 AM

goisastrongchoiceforprojectsneedingsimplicity,績效和引發性,butitmaylackinadvancedfeatures and ecosystemmaturity.1)

比較以其他語言的靜態初始化器中的初始化功能比較以其他語言的靜態初始化器中的初始化功能Apr 28, 2025 am 12:16 AM

Go'sinitfunctionandJava'sstaticinitializersbothservetosetupenvironmentsbeforethemainfunction,buttheydifferinexecutionandcontrol.Go'sinitissimpleandautomatic,suitableforbasicsetupsbutcanleadtocomplexityifoverused.Java'sstaticinitializersoffermorecontr

GO中初始功能的常見用例GO中初始功能的常見用例Apr 28, 2025 am 12:13 AM

thecommonusecasesfortheinitfunctionoare:1)加載configurationfilesbeforeThemainProgramStarts,2)初始化的globalvariables和3)runningpre-checkSorvalidationsbeforEtheprofforeTheProgrecce.TheInitFunctionIsautefunctionIsautomentycalomationalmatomatimationalycalmatemationalcalledbebeforethemainfuniinfuninfuntuntion

GO中的頻道:掌握際際交流GO中的頻道:掌握際際交流Apr 28, 2025 am 12:04 AM

ChannelsarecrucialingoforenablingsafeandefficityCommunicationBetnewengoroutines.theyfacilitateSynChronizationAndManageGoroutIneLifeCycle,EssentialforConcurrentProgramming.ChannelSallSallSallSallSallowSallowsAllowsEnderDendingAndReceivingValues,ActassignalsignalsforsynChronization,and actassignalsynChronization and andsupppor

包裝錯誤:將上下文添加到錯誤鏈中包裝錯誤:將上下文添加到錯誤鏈中Apr 28, 2025 am 12:02 AM

在Go中,可以通過errors.Wrap和errors.Unwrap方法來包裝錯誤並添加上下文。 1)使用errors包的新功能,可以在錯誤傳播過程中添加上下文信息。 2)通過fmt.Errorf和%w包裝錯誤,幫助定位問題。 3)自定義錯誤類型可以創建更具語義化的錯誤,增強錯誤處理的表達能力。

使用GO開發時的安全考慮使用GO開發時的安全考慮Apr 27, 2025 am 12:18 AM

Gooffersrobustfeaturesforsecurecoding,butdevelopersmustimplementsecuritybestpracticeseffectively.1)UseGo'scryptopackageforsecuredatahandling.2)Manageconcurrencywithsynchronizationprimitivestopreventraceconditions.3)SanitizeexternalinputstoavoidSQLinj

了解GO的錯誤接口了解GO的錯誤接口Apr 27, 2025 am 12:16 AM

Go的錯誤接口定義為typeerrorinterface{Error()string},允許任何實現Error()方法的類型被視為錯誤。使用步驟如下:1.基本檢查和記錄錯誤,例如iferr!=nil{log.Printf("Anerroroccurred:%v",err)return}。 2.創建自定義錯誤類型以提供更多信息,如typeMyErrorstruct{MsgstringDetailstring}。 3.使用錯誤包裝(自Go1.13起)來添加上下文而不丟失原始錯誤信息,

並發程序中的錯誤處理並發程序中的錯誤處理Apr 27, 2025 am 12:13 AM

對效率的Handleerrorsinconcurrentgopragrs,UsechannelstocommunicateErrors,enplionErrorWatchers,Instertimeout,UsebufferedChannels和Provideclearrormessages.1)USEchannelelStopassErtopassErrorsErtopassErrorsErrorsErrorsFromGoroutInestOthemainFunction.2)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版