golang:为什么我得到+ Inf而不是整数或浮点数?

我正在学习golang,研究货币时间价值


我试图计算要说的期间数,使您的钱增加一倍。我正在使用的公式是period = log(fv / pv)/ log(1 + i)。到目前为止,我有...


package main

import (

    "fmt"

    "math"

)

var (

    interest,

    futureValue,

    period,

    presentValue float64

)

var rate float64 = interest / 100 //converts interest into decimal... interest / 100

var ratex float64 = 1 + interest //used for (1 + i)


func main() {

    numPeriod()

}

func numPeriod() {

    fmt.Println("Enter interest amount: ")

    fmt.Scanf("%g", &interest)

    fmt.Println("Enter present value: ")

    fmt.Scanf("%g", &presentValue)

    fmt.Println("Enter future value: ")

    fmt.Scanf("%g", &futureValue)

    var logfvpvFactor float64 = futureValue / presentValue

    var logi float64 = math.Log(ratex)

    var logfvpv float64 = math.Log(logfvpvFactor)

    period = logfvpv / logi

    fmt.Printf("Number of period/s is = %g\n", period)

}

运行这个,我得到...


Number of period/s is = +Inf

...我要找的答案是整数或浮点数。我怎么得到的?


慕哥9229398
浏览 1032回答 3
3回答

繁星点点滴滴

为了扩展迭戈的答案,您已经知道了var ratex float64 = 1 + interest在定义利息之前,所以它是0,ratex变成1。var logi float64 = math.Log(ratex)并且由于ratex为1,且log 1为0,logi变为0。然后通过除以logi定义周期,即0,因此将得到+ inf。您应该做的是在获得感兴趣的输入后,将值分配给ratex。

人到中年有点甜

当您分配ratex的值时,兴趣为0。因此,增加您的值所需的时间将是无穷大。您想要的是:func numPeriod() {  fmt.Println("Enter interest amount: ")    fmt.Scanf("%g", &interest)    var ratex float64 = 1 + interest / 100 //used for (1 + i)    fmt.Println("Enter present value: ")    fmt.Scanf("%g", &presentValue)    fmt.Println("Enter future value: ")    fmt.Scanf("%g", &futureValue)    var logfvpvFactor float64 = futureValue / presentValue    var logi float64 = math.Log(ratex)    var logfvpv float64 = math.Log(logfvpvFactor)    period = logfvpv / logi    fmt.Printf("Number of period/s is = %g\n", period)}

手掌心

(09:54) jnml@fsc-r550:~/src/tmp/SO/13739751$ cat main.go package mainimport (        "fmt"        "math")func main() {        var i, pv, fv float64        fmt.Println("Enter interest amount: ")        fmt.Scanf("%g", &i)        fmt.Println("Enter present value: ")        fmt.Scanf("%g", &pv)        fmt.Println("Enter future value: ")        fmt.Scanf("%g", &fv)        fmt.Printf("Number of period/s is = %g\n", math.Log(fv/pv)/math.Log(1+i))}(09:54) jnml@fsc-r550:~/src/tmp/SO/13739751$ go run main.go Enter interest amount: .1Enter present value: 100Enter future value: 200Number of period/s is = 7.272540897341713(09:54) jnml@fsc-r550:~/src/tmp/SO/13739751$ 
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go