🌐
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.
🌐
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 - So, you need to add a math package in your program with the help of the import keyword to access the Inf() function. Syntax: ... // Golang program to illustrate the // math.Inf() Function package main import ( "fmt" "math" ) // Main function ...
🌐
GitHub
github.com › go-inf
go-inf · GitHub
January 25, 2024 - go-inf has one repository available. Follow their code on GitHub.
🌐
Go
go.dev › doc › install
Download and install - The Go Programming Language
Download and install Go quickly with the steps described here.
🌐
GitHub
github.com › golang › go
GitHub - golang/go: The Go programming language · GitHub
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 135K users
Forked by 19K users
Languages   Go 90.2% | Assembly 5.1% | HTML 4.3% | C 0.2% | Shell 0.1% | Perl 0.1%
🌐
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)) ...
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.
🌐
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 the go command documentation for configuration details including how to disable the use of these servers or use different ones.
🌐
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. - inf/dec.go at master · go-inf/inf
Author   go-inf
🌐
Go
go.dev
The Go Programming Language
Download packages for Windows 64-bit, macOS, Linux, and more
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
}
🌐
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.
🌐
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.
🌐
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 ·
Author   golang
🌐
Hostman
hostman.com › tutorials › how to install go on windows
How to Install Go on Windows? | Guide by Hostman
December 30, 2025 - You will also need an administrator account to configure environment variables. ... Download the installer for the latest version of Microsoft Windows from the official Go website.
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland