log 10 Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/log-10/ Sat, 28 Mar 2020 14:05:32 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 log 10 Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/log-10/ 32 32 159787465 Log of number in Go (Golang) https://vikasboss.github.io/log-of-number-go-golang/ https://vikasboss.github.io/log-of-number-go-golang/#respond Sat, 28 Mar 2020 09:58:54 +0000 https://vikasboss.github.io/?p=1850 Overview In this tutorial, we will see three types of logarithm possible Natural logarithm Binary Exponent Log (log e) Binary Log (log 2) Decimal Log (log 10) Natural logarithm math package of...

The post Log of number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

In this tutorial, we will see three types of logarithm possible

  • Natural logarithm
  • Binary Exponent Log (log e)
  • Binary Log (log 2)
  • Decimal Log (log 10)

Natural logarithm

math package of GO provides a Log method that can be used to get the natural logarithm of a number

Below is the signature of the function. It takes input as a float64 number and returns a float64.

func Log(x float64) float64

Also some special cases of Logb function are

  • Log(±Inf) = +Inf
  • Log(0) = -Inf
  • Log(NaN) = NaN
  • Log(x < 0) = NaN

Code

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Log(4)
    fmt.Println(res)

    res = math.Log(10.2)
    fmt.Println(res)

    res = math.Log(-10)
    fmt.Println(res)
}

Output:

1.3862943611198906
2.322387720290225
NaN

Binary Exponent Log (log e)

math package of golang provides a Logb method that can be used to get the binary exponent of a number

Below is the signature of the function. It takes input as a float64 number and returns a float64.

func Logb(x float64) float64

Also some special cases of Logb function are

  • Logb(±Inf) = +Inf
  • Logb(0) = -Inf
  • Logb(NaN) = NaN

Code

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Logb(4)
    fmt.Println(res)

    res = math.Logb(10.2)
    fmt.Println(res)

    res = math.Logb(-10)
    fmt.Println(res)
}

Output:

2
3
3

Binary Log (log 2)

math package of golang provides a Log2 method that can be used to get the binary logarithm or log to base 2 of a number

Below is the signature of the function. It takes input as a float64 number and returns a float64.

Also some special cases of Log2 function are

  • Log2(±Inf) = +Inf
  • Log2(0) = -Inf
  • Log2(NaN) = NaN
  • Log2(x < 0) = NaN

Code

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Log2(4)
    fmt.Println(res)

    res = math.Log2(10.2)
    fmt.Println(res)

    res = math.Log2(-10)
    fmt.Println(res)
}

Output:

2
3.321928094887362
NaN

Decimal Log (log 10)

math package of go provides a Log10 method that can be used to get the decimal logarithm or log to base 10 of a number

Below is the signature of the function. It takes input as a float64 number and returns a float64.

func Log10(x float64) float64

Also some special cases of Log10 function are

  • Log10(±Inf) = +Inf
  • Log10(0) = -Inf
  • Log10(NaN) = NaN
  • Log10(x < 0) = NaN

Code

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Log10(100)
    fmt.Println(res)

    res = math.Log10(10)
    fmt.Println(res)

    res = math.Log10(-10)
    fmt.Println(res)
}

Output:

2
1
NaN

The post Log of number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/log-of-number-go-golang/feed/ 0 1850