Dart Math
Dart Math
You can do simple math with the arithmetic operators, like +, -, * and /.
For more math, Dart has the dart:math library.
To use it, import it at the top of your file:
import 'dart:math';
max() and min()
The max() function returns the larger of two numbers.
The min() function returns the smaller of two numbers:
Example
import 'dart:math';
void main() {
print(max(5, 10));
print(min(5, 10));
print(max(2.5, 1.5));
}
Try it Yourself »
Result:
10
5
2.5
To find the largest number in a list, you can use reduce() with max:
Example
import 'dart:math';
void main() {
List<int> scores = [72, 95, 64, 88];
print(scores.reduce(max));
print(scores.reduce(min));
}
Try it Yourself »
Result:
95
64
sqrt() and pow()
The sqrt() function returns the square root of a number.
The pow(x, y) function returns x to the power of y:
Example
import 'dart:math';
void main() {
print(sqrt(64));
print(pow(2, 3));
print(pow(2.0, 3));
}
Try it Yourself »
Result:
8.0
8
8.0
sqrt() always returns a double.
pow() returns an int when both numbers are integers and y is not negative.
In all other cases, it returns a double.
The library also has other functions, like sin(), cos(), tan() and log().
Math Constants
The dart:math library has some well-known numbers as constants:
Result:
3.141592653589793
2.718281828459045
1.4142135623730951
This example uses pi to find the area of a circle:
Example
import 'dart:math';
void main() {
double radius = 3;
double area = pi * pow(radius, 2);
print('Area: ${area.toStringAsFixed(2)}');
}
Try it Yourself »
Result:
Area: 28.27
Rounding and Absolute Value
Some math methods are built into the numbers themselves, so they do not need an import:
| Method | Description |
|---|---|
abs() | Returns the absolute (positive) value |
round() | Rounds to the nearest integer |
ceil() | Rounds up |
floor() | Rounds down |
Example
void main() {
print((-7).abs());
print(4.6.round());
print(4.2.ceil());
print(4.8.floor());
}
Try it Yourself »
Result:
7
5
5
4
Random Numbers
The Random class makes random numbers.
nextInt(max)returns a random integer from 0 up to, but not including,max.nextDouble()returns a random double from 0.0 up to, but not including, 1.0.nextBool()returnstrueorfalse.
If you give Random() a number, called a seed, you get the same random numbers every time you run the program.
This is useful for testing, and it is why the output below never changes:
Example
import 'dart:math';
void main() {
final random = Random(42);
print(random.nextInt(10));
print(random.nextInt(10));
print(random.nextBool());
}
Try it Yourself »
Result:
7
8
true
Without a seed, Random() gives different numbers each time.
Note: The same seed can give other numbers in another version of Dart, or when Dart runs in a web browser.
To get a number in another range, add to the result.
This example rolls a dice five times, so each number is from 1 to 6:
Example
import 'dart:math';
void main() {
final random = Random(7);
for (int i = 0; i < 5; i++) {
int dice = random.nextInt(6) + 1;
print('You rolled $dice');
}
}
Try it Yourself »
Result:
You rolled 5
You rolled 6
You rolled 3
You rolled 4
You rolled 1
Note: For passwords and other secret values, use Random.secure() instead, because its numbers are much harder to guess.