What is the log10() function in Swift?
Overview
The log10() function in Swift uses the base of 10 to calculate the logarithm of a number. The mathematical representation of the log10() function is given below:
Syntax
log10(number)
Parameter
A
- It should be greater than 0.
- It should be either an integer, float or double.
Return value
The logarithm of the given number is returned.
Code example
Let's look at the code below:
import Foundation// Using a positive integerprint ("log10(4.0) = ", log10(4.0));// Using a positive floatprint ("log10(4.4) = ",log10(4.4));// Using a value of 10print ("log10(10.0) = ",log10(10.0));// Using values which raise exceptionsprint ("log10(-4.5) = ",log10(-4.5));print ("log10(0.0) = ",log10(0.0));
Code explanation
- Line 2: We add the
Foundationheader required to use thelog10()function.
- Lines 4: We use the
log10()function to calculate the logarithm with base 10 of a positive integer.
- Line 7: We use the
log10()function to calculate the logarithm with base 10 of a positive float.
- Line 10: We use the
log10()function to calculate the logarithm with base 10 of the number 10.
- Lines 13 to 15: We use the
log10()function to calculate the logarithm with base 10 of exceptional values.