Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

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!

like image 523
Jim Syyap Avatar asked Dec 06 '12 08:12

Jim Syyap


3 Answers

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.

like image 144
Derek Avatar answered Sep 20 '22 23:09

Derek


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)
}
like image 28
Diego Basch Avatar answered Sep 18 '22 23:09

Diego Basch


(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$ 
like image 20
zzzz Avatar answered Sep 19 '22 23:09

zzzz