Numbers and Math
Math Functions and Constants
The dart:math library adds common math helpers on top of the int/double operators: max(a, b) and min(a, b) pick the larger or smaller of two values; sqrt(x) returns the square root as a double; pow(x, y) raises one number to another; the constant pi is the IEEE-754 double closest to the true value. Combine pi, round(), and / to format a fixed-decimal approximation without a printf.
Program
Play the program to compare two numbers, take a square root, raise a power, and round pi to two decimal places.
dart_math.dart
import 'dart:math';
void main() {
var biggest = max(12, 7);
var smallest = min(12, 7);
var root = sqrt(144);
var cube = pow(2, 10);
var rounded = (pi * 100).round() / 100;
print('$biggest $smallest $root $cube $rounded');
}
max and min
`max(a, b)` and `min(a, b)` return whichever operand wins; when both operands are `int`, the result stays `int`.
sqrt and pow
`sqrt(x)` returns a `double` square root; `pow(x, y)` returns `num` and picks `int` automatically when both operands are non-negative `int`.
pi
`pi` is the IEEE-754 `double` closest to the true value of pi. `(pi * 100).round() / 100` builds a 2-decimal approximation without `toStringAsFixed`.