What is log() in Dart?

The log() function in Dart calculates the natural logarithm of a number. The image below shows the mathematical representation of the log() function.

The dart:math module is required for this function.

Syntax

double log(double num)

Parameters

This function requires:

  • A numbermust be greater than 0 for which the logarithm is to be calculated.

Return value

log() returns the natural logarithm of the number sent as a parameter.

  • If the parameter value is less than zero, then log() returns NaN.
  • If the parameter value is zero, then log() returns negativeinfinity.

Code

The following code shows how to use log() in Dart.

import 'dart:convert';
import 'dart:math';
void main() {
//positive number
print("The value of log(10) = ${log(10)}");
// e
print("The value of log(e) = ${log(e)}");
//negative number
print("The value of log(-2) = ${log(-2)}");
//zero number
print("The value of log(0) = ${log(0)}");
}

Free Resources