# Find Base-10 Exponential of Given Number in Golang

Exponential functions are widely used in mathematics and computer science to represent the growth or decay of various phenomena. In Golang, there are several ways to find the base-10 exponential of a given number. In this article, we will explore some of these methods.

## Understanding the Base-10 Exponential

The base-10 exponential of a given number can be defined as the power to which the number 10 must be raised to obtain the given number. For example, the base-10 exponential of 1000 is 3, because 10 to the power of 3 is 1000. Similarly, the base-10 exponential of 0.01 is -2, because 10 to the power of -2 is 0.01.

## Using the Math Package

Golang provides a built-in math package that contains several mathematical functions, including exponential functions. To find the base-10 exponential of a given number using this package, we can use the Log10 function, which returns the logarithm base-10 of the given number. We can then round this value to the nearest integer using the Round function to obtain the base-10 exponential.

### Example

package main

import (
"fmt"
"math"
)

func main() {
num := 1000.0
exp := math.Round(math.Log10(num))
fmt.Println(exp) // Output: 3
}


### Output

3


In this example, we first define a variable num with the value of 1000. We then use the Log10 function from the math package to find the logarithm base-10 of num. We round this value using the Round function and store it in the variable exp. Finally, we print the value of exp, which is the base-10 exponential of num.

## Using a Custom Function

We can also write a custom function to find the base-10 exponential of a given number. To do this, we can use a loop to repeatedly divide the number by 10 until the quotient is less than 10. The number of times we perform this operation is the base-10 exponential.

### Example

package main

import "fmt"

func findExp(num float64) int {
exp := 0
for num >= 10 {
num /= 10
exp++
}
return exp
}

func main() {
num := 1000.0
exp := findExp(num)
fmt.Println(exp) // Output: 3
}


### Output

3


In this example, we define a custom function findExp that takes a float64 variable num as input and returns an integer variable exp. We initialize exp to zero and use a loop to repeatedly divide num by 10 until the quotient is less than 10. We increment exp with each iteration of the loop. Finally, we return the value of exp.

In the main function, we define a variable num with the value of 1000. We then call the findExp function with num as input and store the result in the variable exp. Finally, we print the value of exp, which is the base-10 exponential of num.

## Conclusion

In this article, we explored different ways to find the base-10 exponential of a given number in Golang. We learned how to use the built-in math package and write a custom function to perform this task. These methods can be useful in various mathematical and scientific applications where exponential functions are involved.

Updated on: 12-Apr-2023

118 Views

Get certified by completing the course