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
🌐
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
🌐
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. "Infinite precision" describes two characteristics: practically unlimited precision for decimal number representation and no support for calculating with any specific fixed precision.
🌐
Go
go.dev › doc › install
Download and install - The Go Programming Language
Download and install Go quickly with the steps described here.
🌐
Educative
educative.io › answers › what-is-the-inf-function-in-golang
What is the Inf function in golang?
To use this function, you must import the math package in your file and access the Inf function within it using the . notation (math.Inf).
🌐
GeeksforGeeks
geeksforgeeks.org › math-inf-function-in-golang-with-examples
math.Inf() Function in Golang With Examples - GeeksforGeeks
April 28, 2025 - GoLang Concurrency · Sign In ▲ · Open In App · Next Article: math.Inf() Function in Golang With Examples · Last Updated : 28 Apr, 2025 · Comments · Improve · Suggest changes · Like Article · Like · Report · Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package.
🌐
IncludeHelp
includehelp.com › golang › math-inf-function-with-examples.aspx
Golang math.Inf() Function with Examples
September 1, 2021 - The return type of Inf() function is a float64, it returns the positive infinity if sign >= 0, negative infinity if sign < 0. // Golang program to demonstrate the // example of math.Inf() Function package main import ( "fmt" "math" ) func main() { fmt.Println(math.Inf(0)) fmt.Println(math.Inf(1)) ...
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
}
Find elsewhere
🌐
GitHub
github.com › golang › go
GitHub - golang/go: The Go programming language · GitHub
March 11, 2026 - Unless otherwise noted, the Go source files are distributed under the BSD-style license found in the LICENSE file. Official binary distributions are available at https://go.dev/dl/. After downloading a binary release, visit https://go.dev/d...
Starred by 134K users
Forked by 19K users
Languages   Go 90.0% | Assembly 5.2% | HTML 4.4% | C 0.2% | Shell 0.1% | Perl 0.1%
🌐
Go
go.dev › dl
All releases - The Go Programming Language
See the release history for more information about Go releases. As of Go 1.13, the go command by default downloads and authenticates modules using the Go module mirror and Go checksum database run by Google. See https://proxy.golang.org/privacy for privacy information about these services and ...
🌐
GitHub
github.com › go-inf
go-inf · GitHub
January 25, 2024 - go-inf has one repository available. Follow their code on GitHub.
🌐
Google
golang.google.cn
The Go Programming Language
Download packages for Windows 64-bit, macOS, Linux, and more
🌐
Docker Hub
hub.docker.com › _ › golang
golang - Official Image | Docker Hub
2 weeks ago - See also docker-library/golang#250 (comment)⁠ for a longer explanation. This image is based on Windows Server Core (mcr.microsoft.com/windows/servercore)⁠. As such, it only works in places which that image does, such as Windows 10 Professional/Enterprise (Anniversary Edition) or Windows Server 2016. For information ...
🌐
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
🌐
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
🌐
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"
🌐
Go
go.dev › learn
Get Started - The Go Programming Language
Install the latest version of Go. For instructions to download and install the Go compilers, tools, and libraries, view the install documentation.
🌐
IncludeHelp
includehelp.com › golang › math-isinf-function-with-examples.aspx
Golang math.IsInf() Function with Examples
September 1, 2021 - Golang | math.IsInf() Function: Here, we are going to learn about the IsInf() function of the math package with its usages, syntax, and examples.