duanmei1946 2012-12-06 08:37
浏览 1217
已采纳

golang:为什么我得到+ 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
", 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!

  • 写回答

3条回答 默认 最新

  • duanbing8817 2012-12-06 14:56
    关注

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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵