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.

Answer from Derek on Stack Overflow
🌐
Educative
educative.io › answers › what-is-the-inf-function-in-golang
What is the Inf function in golang?
The Go programming language uses the Inf function to generate an infinite value.
🌐
Go Packages
pkg.go.dev › gopkg.in › inf.v0
inf package - gopkg.in/inf.v0 - Go Packages
Package inf (type inf.Dec) implements "infinite-precision" decimal arithmetic.
🌐
GeeksforGeeks
geeksforgeeks.org › math-inf-function-in-golang-with-examples
math.Inf() Function in Golang With Examples - GeeksforGeeks
April 28, 2025 - func Inf(sign int) float64 · Example 1: C · // Golang program to illustrate the // math.Inf() Function package main import ( "fmt" "math" ) // Main function func main() { // Finding positive infinity // and negative infinity // Using Inf() function res_1 := math.Inf(-1) res_2 := math.Inf(1) // Displaying the result fmt.Println("Result 1: ", res_1) fmt.Println("Result 2: ", res_2) } Output: Result 1: -Inf Result 2: +Inf ·
🌐
IncludeHelp
includehelp.com › golang › math-inf-function-with-examples.aspx
Golang math.Inf() Function with Examples
September 1, 2021 - The Inf() function is an inbuilt function of the math package which is used to get the positive infinity or negative infinity. It returns the positive infinity (+Inf) if sign >= 0, the negative infinity (-Inf) if sign < 0.
🌐
GitHub
github.com › go-inf › inf
GitHub - go-inf/inf: Package inf (type inf.Dec) implements "infinite-precision" decimal arithmetic. · GitHub
Package inf (type inf.Dec) implements "infinite-precision" decimal arithmetic. - go-inf/inf
Starred by 45 users
Forked by 16 users
Languages   Go
Top answer
1 of 2
1

Infinity is not representable by int.
According to the go spec,

In all non-constant conversions involving floating-point or complex values, if the result type cannot represent the value the conversion succeeds but the result value is implementation-dependent.

Maybe you are looking for the largest representable int? How to get it is explained here.

2 of 2
0

math.Inf() returns an IEEE double-precision float representing positive infinity if the sign of the argument is >= 0, and negative infinity if the sign is < 0, so your code is incorrect.

But, the Go language specifiction (always good to read the specifications) says this:

Conversions between numeric types . . . In all non-constant conversions involving floating-point or complex values, if the result type cannot represent the value the conversion succeeds but the result value is implementation-dependent.

Two's complement integer values don't have the concept of infinity, so the result is implementation dependent.

Myself, I'd have expected to get the largest or smallest integer value for the integer type the cast is targeting, but apparently that's not the case.

This looks to the runtime source file responsible for the conversion, https://go.dev/src/runtime/softfloat64.go

And this is the actual source code.

Note that an IEEE-754 double-precision float is a 64-bit double word, consisting of

  • a sign bit, the high-order (most significant/leftmost bit), 0 indicating positive, 1 indicating negative.
  • an exponent (biased), consisting of the next 11 bits, and
  • a mantissa, consisting of the remaining 52 bits, which can be denormalized.

Positive Infinity is a special value with a sign bit of 0, a exponent of all 1 bits, and a mantissa of all 0 bits:

0 11111111111 0000000000000000000000000000000000000000000000000000

or 0x7FF0000000000000.

Negative infinity is the same, with the exception that the sign bit is 1:

1 11111111111 0000000000000000000000000000000000000000000000000000

or 0xFFF0000000000000.

Looks like `funpack64() returns 5 values:

  • a uint64 representing the sign (0 or the very large non-zero value 0x8000000000000000),
  • a uint64 representing the normalized mantissa,
  • an int representing the exponent,
  • a bool indicating whether or not this is +/- infinity, and
  • a bool indicating whether or not this is NaN.

From that, you should be able to figure out why it returns the value it does.

[Frankly, I'm surprised that f64toint() doesn't short-circuit when funpack64() returns fi = true.]

const mantbits64 uint = 52
const expbits64  uint = 11
const bias64          = -1<<(expbits64-1) + 1

func f64toint(f uint64) (val int64, ok bool) {
    fs, fm, fe, fi, fn := funpack64(f)

    switch {
    case fi, fn: // NaN
        return 0, false

    case fe < -1: // f < 0.5
        return 0, false

    case fe > 63: // f >= 2^63
        if fs != 0 && fm == 0 { // f == -2^63
            return -1 << 63, true
        }
        if fs != 0 {
            return 0, false
        }
        return 0, false
    }

    for fe > int(mantbits64) {
        fe--
        fm <<= 1
    }
    for fe < int(mantbits64) {
        fe++
        fm >>= 1
    }
    val = int64(fm)
    if fs != 0 {
        val = -val
    }
    return val, true
}

func funpack64(f uint64) (sign, mant uint64, exp int, inf, nan bool) {
    sign = f & (1 << (mantbits64 + expbits64))
    mant = f & (1<<mantbits64 - 1)
    exp = int(f>>mantbits64) & (1<<expbits64 - 1)

    switch exp {
    case 1<<expbits64 - 1:
        if mant != 0 {
            nan = true
            return
        }
        inf = true
        return

    case 0:
        // denormalized
        if mant != 0 {
            exp += bias64 + 1
            for mant < 1<<mantbits64 {
                mant <<= 1
                exp--
            }
        }

    default:
        // add implicit top bit
        mant |= 1 << mantbits64
        exp += bias64
    }
    return
}
🌐
Wikipedia
en.wikipedia.org › wiki › Go_(programming_language)
Go (programming language) - Wikipedia
1 week ago - A combined declaration/initialization operator was introduced that allows the programmer to write i := 3 or s := "Hello, world!", without specifying the types of variables used. This contrasts with C's int i = 3; and string s = "Hello, world!"; (though since C23 type inference has been supported using auto, like C++).
Find elsewhere
🌐
Gitbooks
adihumara.gitbooks.io › golang › data-types › infinite-and-nan.html
Infinite and NaN · golang
var z float64 fmt.Println(z, -z, 1/z, -1/z, z/z) // "0 -0 +Inf -Inf NaN" NaN (‘‘not a number’’): the result of mathematically dubious operations as 0/0 or Sqrt(-1) . The function math.IsNaN tests whether its argument is a not-a-number value, and math.NaN returns such a value.
🌐
GitHub
github.com › golang › go › issues › 56023
compiler: conversion from Infinity to integer is always negative · Issue #56023 · golang/go
October 4, 2022 - package main import ( "fmt" "math" ) func main() { fmt.Println(int(math.Inf(+1))) fmt.Println(int(math.Inf(-1))) fmt.Println(int(math.Inf(+1)) == int(math.Inf(-1))) fmt.Println(int8(math.Inf(+1))) fmt.Println(int8(math.Inf(-1))) fmt.Println(int8(math.Inf(+1)) == int8(math.Inf(-1))) fmt.Println(int16(math.Inf(+1))) fmt.Println(int16(math.Inf(-1))) fmt.Println(int16(math.Inf(+1)) == int16(math.Inf(-1))) fmt.Println(int32(math.Inf(+1))) fmt.Println(int32(math.Inf(-1))) fmt.Println(int32(math.Inf(+1)) == int32(math.Inf(-1))) fmt.Println(int64(math.Inf(+1))) fmt.Println(int64(math.Inf(-1))) fmt.Println(int64(math.Inf(+1)) == int64(math.Inf(-1))) } This was reported on the mailing list: https://groups.google.com/g/golang-nuts/c/pRUoXRt7syk/m/_jnc1N48DQAJ?utm_medium=email&utm_source=footer&pli=1 ·
Author   robpike
🌐
Google Groups
groups.google.com › g › golang-nuts › c › BVyo-QQO8xg
Assigning +Inf to a float32 ..
Couldn't find a way to do this - not even in the math package .. (I see that +Inf is in the math package but it is not exported) ie lines 8-10 in math/bits.go ... In https://golang.org/ref/spec#Numeric_types it states "float32 the set of all IEEE-754 32-bit floating-point numbers"
🌐
Socket Loop
socketloop.com › tutorials › golang-test-floating-point-numbers-not-a-number-and-infinite-example
Golang : Test floating point numbers not-a-number and infinite example
January 26, 2016 - : ", math.IsInf(fNaN, -1)) fmt.Println("Is either infinity? : ", math.IsInf(fNaN, 0)) // reference https://golang.org/pkg/math/#IsInf fmt.Println("---------------------------------------------") fmt.Println("Test positive infinite value") fmt.Println("---------------------------------------------") posInf := math.Inf(1) posInf = posInf + 12.2 // infinite value will still propagate after add operation fmt.Println("Symbol : ", posInf) fmt.Println("Is positive infinity?
🌐
GitHub
github.com › golang › go › issues › 20517
typecasting +Inf to int, should result positive value · Issue #20517 · golang/go
May 28, 2017 - go program used to test: https://play.golang.org/p/EdOljzgjr0 · package main import ( "fmt" "math" ) func main() { fmt.Println(int(math.Inf(+1)), int(math.Inf(-1))) } both -Inf and +Inf result same value when converted into int · for example in java, converting +Inf to int returns Integet.MAX_VALUE which is positive ·
Published   May 28, 2017
Author   santhosh-tekuri
🌐
GitHub
github.com › go-inf › inf › blob › master › dec.go
inf/dec.go at master · go-inf/inf
Package inf (type inf.Dec) implements "infinite-precision" decimal arithmetic. - go-inf/inf
Author   go-inf
🌐
Go
go.dev › src › math › cmplx › isinf.go
- The Go Programming Language
10 func IsInf(x complex128) bool { 11 if math.IsInf(real(x), 0) || math.IsInf(imag(x), 0) { 12 return true 13 } 14 return false 15 } 16 17 // Inf returns a complex infinity, complex(+Inf, +Inf).
🌐
GitHub
github.com › golang › go › issues › 44225
math: Type Conversion of Inf(1) to int produces a negative integer instead of a positive one · Issue #44225 · golang/go
February 11, 2021 - package main import ( "fmt" "math" ) func main() { fmt.Println("Hello, playground") m := int(math.Inf(1)) fmt.Println(m) } go playground What did you expect to see? I expected to see positive infinity representation or 922337203685477580...
Author   BozeBro
🌐
GitHub
github.com › golang › go › blob › master › src › math › cmplx › isinf.go
go/src/math/cmplx/isinf.go at master · golang/go
// IsInf reports whether either real(x) or imag(x) is an infinity. func IsInf(x complex128) bool { if math.IsInf(real(x), 0) || math.IsInf(imag(x), 0) { return true · } return false · } · // Inf returns a complex infinity, complex(+Inf, +Inf).
Author   golang
🌐
GitHub
github.com › go-inf
go-inf · GitHub
January 25, 2024 - Package inf (type inf.Dec) implements "infinite-precision" decimal arithmetic.
🌐
GitHub
github.com › golang › go › issues › 3480
encoding/json: handle NaN and Inf · Issue #3480 · golang/go
April 5, 2012 - I would't mind the error since it might be a reasonable idea to notify the user that the encoding didn't preserve, although I would prefer no error at all. The RFC draft describing JSON (http://tools.ietf.org/html/rfc4627) explicitly forbids the use of NaN and (-)Inf as numbers, so does the ...
Author   FlorianUekermann