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 must be greater than 0number for which the logarithm is to be calculated. The number should have the following properties:

  • 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 integer
print ("log10(4.0) = ", log10(4.0));
// Using a positive float
print ("log10(4.4) = ",log10(4.4));
// Using a value of 10
print ("log10(10.0) = ",log10(10.0));
// Using values which raise exceptions
print ("log10(-4.5) = ",log10(-4.5));
print ("log10(0.0) = ",log10(0.0));

Code explanation

  • Line 2: We add the Foundation header required to use the log10() 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.

    Free Resources