第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何將浮點(diǎn)數(shù)轉(zhuǎn)換為復(fù)數(shù)?

如何將浮點(diǎn)數(shù)轉(zhuǎn)換為復(fù)數(shù)?

Go
紫衣仙女 2021-10-18 14:11:24
使用非常簡(jiǎn)單的代碼:package mainimport (    "fmt"    "math"    "math/cmplx")func sqrt(x float64) string {    if x < 0 {        return fmt.Sprint(cmplx.Sqrt(complex128(x)))    }    return fmt.Sprint(math.Sqrt(x))}func main() {    fmt.Println(sqrt(2), sqrt(-4))}我收到以下錯(cuò)誤消息:main.go:11: cannot convert x (type float64) to type complex128我嘗試了不同的方法,但找不到如何將 a 轉(zhuǎn)換float64為complex128(只是為了能夠?qū)mplx.Sqrt()負(fù)數(shù)使用函數(shù))。處理這個(gè)問(wèn)題的正確方法是什么?
查看完整描述

1 回答

?
互換的青春

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊

您并不是真的想將 a 轉(zhuǎn)換為float64,complex128而是想構(gòu)造一個(gè)complex128指定實(shí)部的值。


為此可以使用內(nèi)置complex()函數(shù):


func complex(r, i FloatType) ComplexType

使用它你的sqrt()功能:


func sqrt(x float64) string {

    if x < 0 {

        return fmt.Sprint(cmplx.Sqrt(complex(x, 0)))

    }

    return fmt.Sprint(math.Sqrt(x))

}

在Go Playground上試一試。


筆記:


您可以在float不使用復(fù)數(shù)的情況下計(jì)算負(fù)數(shù)的平方根:它將是一個(gè)復(fù)數(shù)值,其實(shí)部為0虛部math.Sqrt(-x)i(因此結(jié)果:)(0+math.Sqrt(-x)i):


func sqrt2(x float64) string {

    if x < 0 {

        return fmt.Sprintf("(0+%.15fi)", math.Sqrt(-x))

    }

    return fmt.Sprint(math.Sqrt(x))

}


查看完整回答
反對(duì) 回復(fù) 2021-10-18
  • 1 回答
  • 0 關(guān)注
  • 246 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)