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

huangapple go评论75阅读模式
英文:

golang: Why do I get +Inf instead of an integer or a float?

问题

我正在学习golang,正在处理货币时间价值计算。

我正在尝试计算翻倍的时间周期。我使用的公式是period = log(fv/pv) / log(1 + i)。我目前的代码如下:

package main
import (
    "fmt"
    "math"
)
var (
    interest,
    futureValue,
    period,
    presentValue float64
)
var rate float64 = interest / 100 //将利率转换为小数... interest / 100
var ratex float64 = 1 + interest //用于(1 + i)的计算

func main() {
    numPeriod()
}
func numPeriod() {
    fmt.Println("输入利率金额:")
    fmt.Scanf("%g", &interest)
    fmt.Println("输入现值:")
    fmt.Scanf("%g", &presentValue)
    fmt.Println("输入未来值:")
    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("时间周期数为 = %g\n", period)
}

运行这段代码,我得到的结果是:

时间周期数为 = +Inf

...我想要的答案应该是一个整数或浮点数。我该如何得到这样的结果?

谢谢你的帮助!

英文:

I am learning golang, working on time value of money computations

I am trying to compute for number of period to say, double your money. The formula that I am using is period = log(fv/pv) / log(1 + i). What I have so far is...

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)
}

Running this, I get...

Number of period/s is = +Inf

...the answer I was looking for is either an integer or a float. How do I get that?

Thanks for your help!

答案1

得分: 6

为了扩展Diego的答案,你有这一行代码:

var ratex float64 = 1 + interest

在定义interest之前,所以它的值是0,ratex变成了1。然后你有这一行代码:

var logi float64 = math.Log(ratex)

由于ratex是1,而1的对数是0,logi变成了0。然后你通过除以logi来定义period,而logi是0,所以你会得到+inf。

你应该在获取了interest的输入后将值赋给ratex。

英文:

To expand on Diego's answer, you have the line

var ratex float64 = 1 + interest

before interest is defined, so it is 0 and ratex becomes 1. Then you have the line

var logi float64 = math.Log(ratex)

and since ratex is 1, and the log of 1 is 0, logi becomes 0. You then define the period by dividing by logi, which is 0, so you will get +inf.

What you should do is assign the value to ratex after you have gotten the input for what the interest is.

答案2

得分: 2

当你给ratex赋值时,利息为0。因此,增加你的价值所需的时间将是无限的。你想要的是:

func numPeriod() {
  fmt.Println("输入利息金额:")
    fmt.Scanf("%g", &interest)
    var ratex float64 = 1 + interest / 100 //用于(1 + i)
    fmt.Println("输入现值:")
    fmt.Scanf("%g", &presentValue)
    fmt.Println("输入未来值:")
    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("期数为 = %g\n", period)
}
英文:

When you assign the value of ratex, interest is 0. Therefore, the time required to increase your value will be infinity. What you want is:

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)
}

答案3

得分: 1

package main

import (
"fmt"
"math"
)

func main() {
var i, pv, fv float64
fmt.Println("输入利息金额:")
fmt.Scanf("%g", &i)
fmt.Println("输入现值:")
fmt.Scanf("%g", &pv)
fmt.Println("输入未来值:")
fmt.Scanf("%g", &fv)
fmt.Printf("期数 = %g\n", math.Log(fv/pv)/math.Log(1+i))
}

英文:
(09:54) jnml@fsc-r550:~/src/tmp/SO/13739751$ cat main.go 
package main
    
import (
        "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: 
.1
Enter present value: 
100
Enter future value: 
200
Number of period/s is = 7.272540897341713
(09:54) jnml@fsc-r550:~/src/tmp/SO/13739751$ 

huangapple
  • 本文由 发表于 2012年12月6日 16:37:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/13739751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定