Dart Math Library
Mathematical functions and constants for calculations
🔢 What is Dart Math Library?
Dart Math Library provides mathematical functions, constants, and utilities for numerical computations. It includes trigonometric functions, logarithms, random number generation, and essential mathematical operations for calculations.
import 'dart:math';
void main() {
double radius = 5.0;
double area = pi * pow(radius, 2);
print('Circle area: ${area.toStringAsFixed(2)}');
int randomNum = Random().nextInt(100);
print('Random number: $randomNum');
}
Output:
Circle area: 78.54
Random number: 42
Math Functions
Basic Math
Power, square root, and basic operations
pow(2, 3); // 8
sqrt(16); // 4.0
abs(-5); // 5
Trigonometry
Sine, cosine, tangent functions
sin(pi / 2); // 1.0
cos(0); // 1.0
tan(pi / 4); // 1.0
Random Numbers
Generate random values
Random rnd = Random();
rnd.nextInt(10); // 0-9
rnd.nextDouble(); // 0.0-1.0
Constants
Mathematical constants
pi; // 3.14159...
e; // 2.71828...
ln2; // 0.69314...
🔹 Basic Mathematical Operations
Common mathematical functions and calculations:
import 'dart:math';
void main() {
// Power and roots
print('2^3 = ${pow(2, 3)}');
print('Square root of 16 = ${sqrt(16)}');
print('Cube root of 27 = ${pow(27, 1/3).toStringAsFixed(2)}');
// Absolute value and sign
print('Absolute value of -10 = ${abs(-10)}');
print('Sign of -5 = ${-5.sign}');
print('Sign of 5 = ${5.sign}');
// Min and max
print('Min of 5 and 10 = ${min(5, 10)}');
print('Max of 5 and 10 = ${max(5, 10)}');
// Rounding functions
double value = 3.7;
print('Original: $value');
print('Ceiling: ${value.ceil()}');
print('Floor: ${value.floor()}');
print('Round: ${value.round()}');
print('Truncate: ${value.truncate()}');
}
Output:
2^3 = 8.0
Square root of 16 = 4.0
Cube root of 27 = 3.00
Absolute value of -10 = 10
Sign of -5 = -1.0
Sign of 5 = 1.0
Min of 5 and 10 = 5
Max of 5 and 10 = 10
Original: 3.7
Ceiling: 4
Floor: 3
Round: 4
Truncate: 3
🔹 Trigonometric Functions
Working with angles and trigonometry:
import 'dart:math';
void main() {
// Convert degrees to radians
double degrees = 90;
double radians = degrees * (pi / 180);
print('$degrees degrees = $radians radians');
// Basic trigonometric functions
print('sin(90°) = ${sin(radians).toStringAsFixed(2)}');
print('cos(0°) = ${cos(0).toStringAsFixed(2)}');
print('tan(45°) = ${tan(pi / 4).toStringAsFixed(2)}');
// Inverse trigonometric functions
print('asin(1) = ${asin(1) * (180 / pi)} degrees');
print('acos(0) = ${acos(0) * (180 / pi)} degrees');
print('atan(1) = ${atan(1) * (180 / pi)} degrees');
// Hyperbolic functions
print('sinh(1) = ${sinh(1).toStringAsFixed(3)}');
print('cosh(0) = ${cosh(0).toStringAsFixed(3)}');
print('tanh(1) = ${tanh(1).toStringAsFixed(3)}');
// Calculate distance between two points
double x1 = 0, y1 = 0;
double x2 = 3, y2 = 4;
double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
print('Distance between (0,0) and (3,4) = $distance');
}
Output:
90 degrees = 1.5707963267948966 radians
sin(90°) = 1.00
cos(0°) = 1.00
tan(45°) = 1.00
asin(1) = 90.0 degrees
acos(0) = 90.0 degrees
atan(1) = 45.0 degrees
sinh(1) = 1.175
cosh(0) = 1.000
tanh(1) = 0.762
Distance between (0,0) and (3,4) = 5.0
🔹 Random Number Generation
Generating random numbers and values:
import 'dart:math';
void main() {
Random random = Random();
// Basic random numbers
print('Random integer 0-9: ${random.nextInt(10)}');
print('Random double 0.0-1.0: ${random.nextDouble().toStringAsFixed(3)}');
print('Random boolean: ${random.nextBool()}');
// Random in range
int min = 10, max = 50;
int randomInRange = min + random.nextInt(max - min + 1);
print('Random between $min-$max: $randomInRange');
// Random double in range
double minDouble = 1.0, maxDouble = 10.0;
double randomDouble = minDouble + random.nextDouble() * (maxDouble - minDouble);
print('Random double 1.0-10.0: ${randomDouble.toStringAsFixed(2)}');
// Random from list
List colors = ['red', 'green', 'blue', 'yellow', 'purple'];
String randomColor = colors[random.nextInt(colors.length)];
print('Random color: $randomColor');
// Seeded random (reproducible)
Random seededRandom = Random(42);
print('Seeded random 1: ${seededRandom.nextInt(100)}');
print('Seeded random 2: ${seededRandom.nextInt(100)}');
// Generate multiple random numbers
List randomNumbers = List.generate(5, (index) => random.nextInt(100));
print('5 random numbers: $randomNumbers');
}
Output:
Random integer 0-9: 7
Random double 0.0-1.0: 0.342
Random boolean: true
Random between 10-50: 23
Random double 1.0-10.0: 6.78
Random color: blue
Seeded random 1: 15
Seeded random 2: 87
5 random numbers: [34, 67, 12, 89, 45]
🔹 Logarithmic Functions
Working with logarithms and exponentials:
import 'dart:math';
void main() {
// Natural logarithm (base e)
print('ln(e) = ${log(e).toStringAsFixed(3)}');
print('ln(10) = ${log(10).toStringAsFixed(3)}');
// Exponential function
print('e^1 = ${exp(1).toStringAsFixed(3)}');
print('e^2 = ${exp(2).toStringAsFixed(3)}');
// Logarithm base 10 (using change of base formula)
double log10(double x) => log(x) / ln10;
print('log10(100) = ${log10(100).toStringAsFixed(1)}');
print('log10(1000) = ${log10(1000).toStringAsFixed(1)}');
// Logarithm base 2
double log2(double x) => log(x) / ln2;
print('log2(8) = ${log2(8).toStringAsFixed(1)}');
print('log2(16) = ${log2(16).toStringAsFixed(1)}');
// Mathematical constants
print('\nMathematical Constants:');
print('π (pi) = ${pi.toStringAsFixed(6)}');
print('e = ${e.toStringAsFixed(6)}');
print('ln(2) = ${ln2.toStringAsFixed(6)}');
print('ln(10) = ${ln10.toStringAsFixed(6)}');
print('log2(e) = ${log2e.toStringAsFixed(6)}');
print('log10(e) = ${log10e.toStringAsFixed(6)}');
print('sqrt(2) = ${sqrt2.toStringAsFixed(6)}');
print('sqrt(1/2) = ${sqrt1_2.toStringAsFixed(6)}');
}
Output:
ln(e) = 1.000
ln(10) = 2.303
e^1 = 2.718
e^2 = 7.389
log10(100) = 2.0
log10(1000) = 3.0
log2(8) = 3.0
log2(16) = 4.0
Mathematical Constants:
π (pi) = 3.141593
e = 2.718282
ln(2) = 0.693147
ln(10) = 2.302585
log2(e) = 1.442695
log10(e) = 0.434294
sqrt(2) = 1.414214
sqrt(1/2) = 0.707107
🔹 Practical Math Examples
Real-world mathematical calculations:
import 'dart:math';
void main() {
// Calculate compound interest
double principal = 1000;
double rate = 0.05; // 5%
int years = 10;
double amount = principal * pow(1 + rate, years);
print('Compound Interest:');
print('Principal: \$${principal.toStringAsFixed(2)}');
print('Amount after $years years: \$${amount.toStringAsFixed(2)}');
print('Interest earned: \$${(amount - principal).toStringAsFixed(2)}');
// Calculate area and circumference of circle
double radius = 7.5;
double area = pi * pow(radius, 2);
double circumference = 2 * pi * radius;
print('\nCircle Calculations:');
print('Radius: $radius');
print('Area: ${area.toStringAsFixed(2)}');
print('Circumference: ${circumference.toStringAsFixed(2)}');
// Generate statistics
List numbers = [12, 15, 18, 20, 22, 25, 28, 30];
double sum = numbers.reduce((a, b) => a + b).toDouble();
double mean = sum / numbers.length;
// Calculate standard deviation
double variance = numbers
.map((x) => pow(x - mean, 2))
.reduce((a, b) => a + b) / numbers.length;
double stdDev = sqrt(variance);
print('\nStatistics:');
print('Numbers: $numbers');
print('Mean: ${mean.toStringAsFixed(2)}');
print('Standard Deviation: ${stdDev.toStringAsFixed(2)}');
}
Output:
Compound Interest:
Principal: $1000.00
Amount after 10 years: $1628.89
Interest earned: $628.89
Circle Calculations:
Radius: 7.5
Area: 176.71
Circumference: 47.12
Statistics:
Numbers: [12, 15, 18, 20, 22, 25, 28, 30]
Mean: 21.25
Standard Deviation: 6.12