What is the Jn function in Golang?

The programming language Go uses the Jn function to find the Bessel function of a passed argument. The order of the function is determined by another argument you give it.

To use this function, you must import the math package in your file and access the Jn function within it using the . notation (math.Jn). Here, Jn is the actual function, while math is the Go package that stores the definition of this function.

Function definition

The definition of the Jn function inside the math package is:

Parameters

Jn function takes two arguments:

  • x: This argument, of type float64, represents the number you want to find the order-n Bessel function of.

  • n: This argument tells the Jn function what order of the Bessel function it needs to generate.

Return value

The Jn function returns a single value of type float64 representing the order-n Bessel function of the argument.

Some special cases are when you pass something that is infinity, 0, or NAN as the second argument :

  • If the argument has an infinite value, the return value will be 0, regardless if it is positive or negative.

  • If a NAN argument is passed, the return value is also NAN.

Examples

The following is a simple example where we find the order-3 Bessel function of 5.35:

package main
import(
"fmt"
"math"
)
func main() {
var x float64 = 5.35
y := math.Jn(2, x)
fmt.Print("The order-two Bessel function of ", x," is ", y, "\n")
}

The following example shows how the Jn function deals with an argument whose value is infinite (regardless of the order you give it):

To generate the infinite value, we are using the Inf function in the math package. It generates an infinite value with the same sign as the sign on the argument passed to it.

package main
import(
"fmt"
"math"
)
func main() {
var x float64 = math.Inf(1)
y := math.Jn(2, x)
fmt.Print("The order-two Bessel function of ", x," is ", y, "\n")
y = math.Jn(7, x)
fmt.Print("The order-seven Bessel function of ", x," is ", y, "\n")
y = math.Jn(20, x)
fmt.Print("The order-twenty Bessel function of ", x," is ", y, "\n")
}

The following piece program shows how the Jn function handles NAN values:

Here, we use the NaN function present in the math go package to generate a NAN value.

package main
import(
"fmt"
"math"
)
func main() {
my_nan := math.NaN()
y := math.Jn(2, my_nan)
fmt.Print("The order-two Bessel function of ", my_nan," is ", y, "\n")
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved