Math & Numbers
Math Functions
When building applications that need calculations like distance between points, interest rates, or game physics, you need reliable mathematical operations. Java's Math class provides static methods for common mathematical computations including powers, roots, trigonometry, and rounding.
Basic Operations
The Math class provides fundamental mathematical operations.
Basic.java
// Basic math operations
public class Basic {
public static void main(String[] args) {
// Absolute value
System.out.println("Absolute value:");
System.out.println("abs(-5): " + Math.abs(-5));
System.out.println("abs(-3.14): " + Math.abs(-3.14));
System.out.println("abs(7): " + Math.abs(7));
// Power
System.out.println("\nPower:");
System.out.println("pow(2, 3): " + Math.pow(2, 3));
System.out.println("pow(5, 2): " + Math.pow(5, 2));
System.out.println("pow(2, 10): " + Math.pow(2, 10));
System.out.println("pow(3, 0): " + Math.pow(3, 0));
// Square root
System.out.println("\nSquare root:");
System.out.println("sqrt(16): " + Math.sqrt(16));
System.out.println("sqrt(2): " + Math.sqrt(2));
System.out.println("sqrt(100): " + Math.sqrt(100));
System.out.println("sqrt(0.25): " + Math.sqrt(0.25));
// Cube root
System.out.println("\nCube root:");
System.out.println("cbrt(8): " + Math.cbrt(8));
System.out.println("cbrt(27): " + Math.cbrt(27));
System.out.println("cbrt(64): " + Math.cbrt(64));
// Max and min
System.out.println("\nMax and min:");
System.out.println("max(5, 10): " + Math.max(5, 10));
System.out.println("min(5, 10): " + Math.min(5, 10));
System.out.println("max(-3, -7): " + Math.max(-3, -7));
System.out.println("min(3.14, 2.71): " + Math.min(3.14, 2.71));
// Sign
System.out.println("\nSign (signum):");
System.out.println("signum(5): " + Math.signum(5)); // 1.0
System.out.println("signum(-5): " + Math.signum(-5)); // -1.0
System.out.println("signum(0): " + Math.signum(0)); // 0.0
// Exponential and logarithm
System.out.println("\nExponential and logarithm:");
System.out.println("exp(1): " + Math.exp(1)); // e^1
System.out.println("exp(2): " + Math.exp(2)); // e^2
System.out.println("log(Math.E): " + Math.log(Math.E)); // ln(e) = 1
System.out.println("log10(100): " + Math.log10(100)); // log base 10
// Constants
System.out.println("\nMath constants:");
System.out.println("PI: " + Math.PI);
System.out.println("E: " + Math.E);
// Practical examples
System.out.println("\nPractical examples:");
// Distance
double x1 = 0, y1 = 0, x2 = 3, y2 = 4;
double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
System.out.println("Distance from (0,0) to (3,4): " + distance);
// Circle area
double radius = 5;
double area = Math.PI * Math.pow(radius, 2);
System.out.println("Circle area (r=5): " + area);
// Compound interest
double principal = 1000;
double rate = 0.05;
int years = 10;
double amount = principal * Math.pow(1 + rate, years);
System.out.println("Compound interest: $" + amount);
}
//help h1
// Math.abs(x) - absolute value
// Math.pow(base, exp) - power
// Math.sqrt(x) - square root
// Math.cbrt(x) - cube root
// Math.max(a, b), Math.min(a, b)
// Math.exp(x) - e^x
// Math.log(x) - natural log
// Math.PI, Math.E - constants
//end
}
Math class
A utility class providing static methods for mathematical operations like powers, roots, and trigonometry.
Static methods
Methods called on the class itself (Math.pow()) rather than on an instance, since Math has no state to maintain.
Rounding Functions
Control how decimal numbers convert to integers.
Rounding.java
// Rounding operations
public class Rounding {
public static void main(String[] args) {
double value = 3.7;
System.out.println("Value: " + value);
System.out.println();
// Round
System.out.println("round():");
System.out.println("round(3.7): " + Math.round(3.7)); // 4
System.out.println("round(3.4): " + Math.round(3.4)); // 3
System.out.println("round(3.5): " + Math.round(3.5)); // 4
System.out.println("round(-3.5): " + Math.round(-3.5)); // -3
System.out.println("round(-3.6): " + Math.round(-3.6)); // -4
// Ceil (round up)
System.out.println("\nceil() - round up:");
System.out.println("ceil(3.1): " + Math.ceil(3.1)); // 4.0
System.out.println("ceil(3.9): " + Math.ceil(3.9)); // 4.0
System.out.println("ceil(-3.1): " + Math.ceil(-3.1)); // -3.0
System.out.println("ceil(5.0): " + Math.ceil(5.0)); // 5.0
// Floor (round down)
System.out.println("\nfloor() - round down:");
System.out.println("floor(3.1): " + Math.floor(3.1)); // 3.0
System.out.println("floor(3.9): " + Math.floor(3.9)); // 3.0
System.out.println("floor(-3.1): " + Math.floor(-3.1)); // -4.0
System.out.println("floor(5.0): " + Math.floor(5.0)); // 5.0
// Compare all three
System.out.println("\nCompare rounding methods:");
double[] testValues = {2.3, 2.5, 2.7, -2.3, -2.5, -2.7};
System.out.println("Value\tFloor\tRound\tCeil");
for (double v : testValues) {
System.out.printf("%.1f\t%.1f\t%d\t%.1f%n",
v, Math.floor(v), Math.round(v), Math.ceil(v));
}
// Rounding to decimal places
System.out.println("\nRound to decimal places:");
double pi = Math.PI;
System.out.println("Original: " + pi);
System.out.println("2 decimals: " + roundToDecimals(pi, 2));
System.out.println("4 decimals: " + roundToDecimals(pi, 4));
// Truncate (toward zero)
System.out.println("\nTruncate (cast to int):");
System.out.println("(int) 3.9: " + (int) 3.9); // 3
System.out.println("(int) -3.9: " + (int) -3.9); // -3
// Practical examples
System.out.println("\nPractical examples:");
// Calculate pages needed
int items = 47;
int itemsPerPage = 10;
int pages = (int) Math.ceil((double) items / itemsPerPage);
System.out.println(items + " items, " + itemsPerPage + " per page = " + pages + " pages");
// Round money
double price = 19.996;
double rounded = roundToDecimals(price, 2);
System.out.println("Price $" + price + " rounded: $" + rounded);
// Nearest multiple
int number = 47;
int multiple = 10;
int nearest = (int) (Math.round((double) number / multiple) * multiple);
System.out.println(number + " to nearest " + multiple + ": " + nearest);
// Division with rounding
System.out.println("\nDivision with rounding:");
System.out.println("7 / 2 (floor): " + 7 / 2);
System.out.println("7 / 2.0 (round): " + Math.round(7 / 2.0));
System.out.println("7 / 2.0 (ceil): " + (int) Math.ceil(7 / 2.0));
}
public static double roundToDecimals(double value, int decimals) {
double scale = Math.pow(10, decimals);
return Math.round(value * scale) / scale;
}
//help h1
// Math.round(x) - round to nearest integer
// Math.ceil(x) - round up to integer
// Math.floor(x) - round down to integer
// (int) x - truncate (toward zero)
// Round to N decimals: round(x * 10^N) / 10^N
// ceil() useful for pagination
//end
}
Comparison Functions
Find minimum and maximum values efficiently.
Comparison.java
// Comparison and clamping
public class Comparison {
public static void main(String[] args) {
// Max and min
System.out.println("Max and min:");
System.out.println("max(5, 10): " + Math.max(5, 10));
System.out.println("min(5, 10): " + Math.min(5, 10));
System.out.println("max(-5, -10): " + Math.max(-5, -10));
System.out.println("min(3.14, 2.71): " + Math.min(3.14, 2.71));
// Max/min of multiple values
System.out.println("\nMax/min of multiple:");
int[] numbers = {5, 2, 9, 1, 7, 3};
int maxValue = findMax(numbers);
int minValue = findMin(numbers);
System.out.println("Array: " + java.util.Arrays.toString(numbers));
System.out.println("Max: " + maxValue);
System.out.println("Min: " + minValue);
// Clamp (restrict to range)
System.out.println("\nClamp to range [0, 100]:");
System.out.println("clamp(-10): " + clamp(-10, 0, 100));
System.out.println("clamp(50): " + clamp(50, 0, 100));
System.out.println("clamp(150): " + clamp(150, 0, 100));
// Sign comparison
System.out.println("\nSign comparison:");
System.out.println("signum(5): " + Math.signum(5));
System.out.println("signum(-5): " + Math.signum(-5));
System.out.println("signum(0): " + Math.signum(0));
// Same sign check
System.out.println("\nSame sign:");
System.out.println("5 and 10: " + sameSign(5, 10));
System.out.println("5 and -10: " + sameSign(5, -10));
System.out.println("-5 and -10: " + sameSign(-5, -10));
// Absolute difference
System.out.println("\nAbsolute difference:");
System.out.println("|5 - 10|: " + Math.abs(5 - 10));
System.out.println("|10 - 5|: " + Math.abs(10 - 5));
System.out.println("|-5 - (-10)|: " + Math.abs(-5 - (-10)));
// Within tolerance
System.out.println("\nWithin tolerance:");
double a = 3.14159;
double b = 3.14;
double tolerance = 0.01;
System.out.println(a + " ≈ " + b + " (±" + tolerance + "): " +
withinTolerance(a, b, tolerance));
// Practical examples
System.out.println("\nPractical examples:");
// Keep score in bounds
int score = ;
int bounded = clamp(score, 0, 100);
System.out.println("Score " + score + " bounded to [0,100]: " + bounded);
// Volume control
double volume = 1.5;
double validVolume = clamp(volume, 0.0, 1.0);
System.out.println("Volume " + volume + " clamped to [0,1]: " + validVolume);
// Temperature range
int temp = -5;
int minTemp = 0;
int maxTemp = 30;
int validTemp = clamp(temp, minTemp, maxTemp);
System.out.println("Temp " + temp + "° bounded to [" + minTemp + "," + maxTemp + "]: " + validTemp);
// Find range
int[] values = {23, 45, 12, 67, 34};
int min = findMin(values);
int max = findMax(values);
int range = max - min;
System.out.println("\nValues: " + java.util.Arrays.toString(values));
System.out.println("Range: " + min + " to " + max + " (span: " + range + ")");
// Midpoint
System.out.println("\nMidpoint:");
System.out.println("Between 10 and 20: " + midpoint(10, 20));
System.out.println("Between -5 and 15: " + midpoint(-5, 15));
}
public static int findMax(int[] arr) {
int max = arr[0];
for (int val : arr) {
max = Math.max(max, val);
}
return max;
}
public static int findMin(int[] arr) {
int min = arr[0];
for (int val : arr) {
min = Math.min(min, val);
}
return min;
}
public static int clamp(int value, int min, int max) {
return Math.max(min, Math.min(max, value));
}
public static double clamp(double value, double min, double max) {
return Math.max(min, Math.min(max, value));
}
public static boolean sameSign(double a, double b) {
return Math.signum(a) == Math.signum(b);
}
public static boolean withinTolerance(double a, double b, double tolerance) {
return Math.abs(a - b) <= tolerance;
}
public static double midpoint(double a, double b) {
return (a + b) / 2;
}
//help h1
// Math.max(a, b), Math.min(a, b)
// Clamp: max(min, min(max, value))
// Math.signum(x) - sign of number
// Math.abs(a - b) - absolute difference
// Within tolerance: abs(a - b) <= tol
// Iterate for max/min of array
//end
}
// Comparison and clamping
public class Comparison {
public static void main(String[] args) {
// Max and min
System.out.println("Max and min:");
System.out.println("max(5, 10): " + Math.max(5, 10));
System.out.println("min(5, 10): " + Math.min(5, 10));
System.out.println("max(-5, -10): " + Math.max(-5, -10));
System.out.println("min(3.14, 2.71): " + Math.min(3.14, 2.71));
// Max/min of multiple values
System.out.println("\nMax/min of multiple:");
int[] numbers = {5, 2, 9, 1, 7, 3};
int maxValue = findMax(numbers);
int minValue = findMin(numbers);
System.out.println("Array: " + java.util.Arrays.toString(numbers));
System.out.println("Max: " + maxValue);
System.out.println("Min: " + minValue);
// Clamp (restrict to range)
System.out.println("\nClamp to range [0, 100]:");
System.out.println("clamp(-10): " + clamp(-10, 0, 100));
System.out.println("clamp(50): " + clamp(50, 0, 100));
System.out.println("clamp(150): " + clamp(150, 0, 100));
// Sign comparison
System.out.println("\nSign comparison:");
System.out.println("signum(5): " + Math.signum(5));
System.out.println("signum(-5): " + Math.signum(-5));
System.out.println("signum(0): " + Math.signum(0));
// Same sign check
System.out.println("\nSame sign:");
System.out.println("5 and 10: " + sameSign(5, 10));
System.out.println("5 and -10: " + sameSign(5, -10));
System.out.println("-5 and -10: " + sameSign(-5, -10));
// Absolute difference
System.out.println("\nAbsolute difference:");
System.out.println("|5 - 10|: " + Math.abs(5 - 10));
System.out.println("|10 - 5|: " + Math.abs(10 - 5));
System.out.println("|-5 - (-10)|: " + Math.abs(-5 - (-10)));
// Within tolerance
System.out.println("\nWithin tolerance:");
double a = 3.14159;
double b = 3.14;
double tolerance = 0.01;
System.out.println(a + " ≈ " + b + " (±" + tolerance + "): " +
withinTolerance(a, b, tolerance));
// Practical examples
System.out.println("\nPractical examples:");
// Keep score in bounds
int score = ;
int bounded = clamp(score, 0, 100);
System.out.println("Score " + score + " bounded to [0,100]: " + bounded);
// Volume control
double volume = 1.5;
double validVolume = clamp(volume, 0.0, 1.0);
System.out.println("Volume " + volume + " clamped to [0,1]: " + validVolume);
// Temperature range
int temp = -5;
int minTemp = 0;
int maxTemp = 30;
int validTemp = clamp(temp, minTemp, maxTemp);
System.out.println("Temp " + temp + "° bounded to [" + minTemp + "," + maxTemp + "]: " + validTemp);
// Find range
int[] values = {23, 45, 12, 67, 34};
int min = findMin(values);
int max = findMax(values);
int range = max - min;
System.out.println("\nValues: " + java.util.Arrays.toString(values));
System.out.println("Range: " + min + " to " + max + " (span: " + range + ")");
// Midpoint
System.out.println("\nMidpoint:");
System.out.println("Between 10 and 20: " + midpoint(10, 20));
System.out.println("Between -5 and 15: " + midpoint(-5, 15));
}
public static int findMax(int[] arr) {
int max = arr[0];
for (int val : arr) {
max = Math.max(max, val);
}
return max;
}
public static int findMin(int[] arr) {
int min = arr[0];
for (int val : arr) {
min = Math.min(min, val);
}
return min;
}
public static int clamp(int value, int min, int max) {
return Math.max(min, Math.min(max, value));
}
public static double clamp(double value, double min, double max) {
return Math.max(min, Math.min(max, value));
}
public static boolean sameSign(double a, double b) {
return Math.signum(a) == Math.signum(b);
}
public static boolean withinTolerance(double a, double b, double tolerance) {
return Math.abs(a - b) <= tolerance;
}
public static double midpoint(double a, double b) {
return (a + b) / 2;
}
//help h1
// Math.max(a, b), Math.min(a, b)
// Clamp: max(min, min(max, value))
// Math.signum(x) - sign of number
// Math.abs(a - b) - absolute difference
// Within tolerance: abs(a - b) <= tol
// Iterate for max/min of array
//end
}
// Comparison and clamping
public class Comparison {
public static void main(String[] args) {
// Max and min
System.out.println("Max and min:");
System.out.println("max(5, 10): " + Math.max(5, 10));
System.out.println("min(5, 10): " + Math.min(5, 10));
System.out.println("max(-5, -10): " + Math.max(-5, -10));
System.out.println("min(3.14, 2.71): " + Math.min(3.14, 2.71));
// Max/min of multiple values
System.out.println("\nMax/min of multiple:");
int[] numbers = {5, 2, 9, 1, 7, 3};
int maxValue = findMax(numbers);
int minValue = findMin(numbers);
System.out.println("Array: " + java.util.Arrays.toString(numbers));
System.out.println("Max: " + maxValue);
System.out.println("Min: " + minValue);
// Clamp (restrict to range)
System.out.println("\nClamp to range [0, 100]:");
System.out.println("clamp(-10): " + clamp(-10, 0, 100));
System.out.println("clamp(50): " + clamp(50, 0, 100));
System.out.println("clamp(150): " + clamp(150, 0, 100));
// Sign comparison
System.out.println("\nSign comparison:");
System.out.println("signum(5): " + Math.signum(5));
System.out.println("signum(-5): " + Math.signum(-5));
System.out.println("signum(0): " + Math.signum(0));
// Same sign check
System.out.println("\nSame sign:");
System.out.println("5 and 10: " + sameSign(5, 10));
System.out.println("5 and -10: " + sameSign(5, -10));
System.out.println("-5 and -10: " + sameSign(-5, -10));
// Absolute difference
System.out.println("\nAbsolute difference:");
System.out.println("|5 - 10|: " + Math.abs(5 - 10));
System.out.println("|10 - 5|: " + Math.abs(10 - 5));
System.out.println("|-5 - (-10)|: " + Math.abs(-5 - (-10)));
// Within tolerance
System.out.println("\nWithin tolerance:");
double a = 3.14159;
double b = 3.14;
double tolerance = 0.01;
System.out.println(a + " ≈ " + b + " (±" + tolerance + "): " +
withinTolerance(a, b, tolerance));
// Practical examples
System.out.println("\nPractical examples:");
// Keep score in bounds
int score = ;
int bounded = clamp(score, 0, 100);
System.out.println("Score " + score + " bounded to [0,100]: " + bounded);
// Volume control
double volume = 1.5;
double validVolume = clamp(volume, 0.0, 1.0);
System.out.println("Volume " + volume + " clamped to [0,1]: " + validVolume);
// Temperature range
int temp = -5;
int minTemp = 0;
int maxTemp = 30;
int validTemp = clamp(temp, minTemp, maxTemp);
System.out.println("Temp " + temp + "° bounded to [" + minTemp + "," + maxTemp + "]: " + validTemp);
// Find range
int[] values = {23, 45, 12, 67, 34};
int min = findMin(values);
int max = findMax(values);
int range = max - min;
System.out.println("\nValues: " + java.util.Arrays.toString(values));
System.out.println("Range: " + min + " to " + max + " (span: " + range + ")");
// Midpoint
System.out.println("\nMidpoint:");
System.out.println("Between 10 and 20: " + midpoint(10, 20));
System.out.println("Between -5 and 15: " + midpoint(-5, 15));
}
public static int findMax(int[] arr) {
int max = arr[0];
for (int val : arr) {
max = Math.max(max, val);
}
return max;
}
public static int findMin(int[] arr) {
int min = arr[0];
for (int val : arr) {
min = Math.min(min, val);
}
return min;
}
public static int clamp(int value, int min, int max) {
return Math.max(min, Math.min(max, value));
}
public static double clamp(double value, double min, double max) {
return Math.max(min, Math.min(max, value));
}
public static boolean sameSign(double a, double b) {
return Math.signum(a) == Math.signum(b);
}
public static boolean withinTolerance(double a, double b, double tolerance) {
return Math.abs(a - b) <= tolerance;
}
public static double midpoint(double a, double b) {
return (a + b) / 2;
}
//help h1
// Math.max(a, b), Math.min(a, b)
// Clamp: max(min, min(max, value))
// Math.signum(x) - sign of number
// Math.abs(a - b) - absolute difference
// Within tolerance: abs(a - b) <= tol
// Iterate for max/min of array
//end
}
Trigonometric Functions
Calculate sine, cosine, and other trigonometric values.
Trigonometry.java
// Trigonometric functions
public class Trigonometry {
public static void main(String[] args) {
// Angles in radians
double angle = Math.PI / 4; // 45 degrees
System.out.println("Angle: " + angle + " radians (45 degrees)");
System.out.println();
// Basic trig functions
System.out.println("Basic trigonometry:");
System.out.println("sin(π/4): " + Math.sin(angle));
System.out.println("cos(π/4): " + Math.cos(angle));
System.out.println("tan(π/4): " + Math.tan(angle));
// Common angles
System.out.println("\nCommon angles:");
System.out.println("sin(0): " + Math.sin(0));
System.out.println("sin(π/2): " + Math.sin(Math.PI / 2)); // 90 degrees
System.out.println("sin(π): " + Math.sin(Math.PI)); // 180 degrees
System.out.println("cos(0): " + Math.cos(0));
System.out.println("cos(π): " + Math.cos(Math.PI));
// Inverse trig functions
System.out.println("\nInverse trigonometry:");
System.out.println("asin(1): " + Math.asin(1)); // π/2
System.out.println("acos(0): " + Math.acos(0)); // π/2
System.out.println("atan(1): " + Math.atan(1)); // π/4
System.out.println("atan2(1, 1): " + Math.atan2(1, 1)); // π/4
// Hyperbolic functions
System.out.println("\nHyperbolic functions:");
System.out.println("sinh(1): " + Math.sinh(1));
System.out.println("cosh(1): " + Math.cosh(1));
System.out.println("tanh(1): " + Math.tanh(1));
// Degree/radian conversion
System.out.println("\nDegree/radian conversion:");
double degrees = ;
double radians = Math.toRadians(degrees);
System.out.println(degrees + " degrees = " + radians + " radians");
System.out.println(radians + " radians = " + Math.toDegrees(radians) + " degrees");
// Trig with degrees
System.out.println("\nTrig with degrees (convert first):");
double angle45 = Math.toRadians(45);
double angle90 = Math.toRadians(90);
System.out.println("sin(45°): " + Math.sin(angle45));
System.out.println("cos(90°): " + Math.cos(angle90));
// Practical examples
System.out.println("\nPractical examples:");
// Right triangle
double adjacent = 3;
double opposite = 4;
double hypotenuse = Math.sqrt(Math.pow(adjacent, 2) + Math.pow(opposite, 2));
double angleRad = Math.atan2(opposite, adjacent);
double angleDeg = Math.toDegrees(angleRad);
System.out.println("Right triangle (3, 4, ?):");
System.out.println("Hypotenuse: " + hypotenuse);
System.out.println("Angle: " + angleDeg + " degrees");
// Circle point
double radius = 10;
double angleCircle = Math.toRadians(60);
double x = radius * Math.cos(angleCircle);
double y = radius * Math.sin(angleCircle);
System.out.println("\nPoint on circle (r=10, 60°):");
System.out.println("x: " + x);
System.out.println("y: " + y);
// Distance and angle between points
double x1 = 0, y1 = 0;
double x2 = 3, y2 = 3;
double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
double angleToPoint = Math.toDegrees(Math.atan2(y2 - y1, x2 - x1));
System.out.println("\nFrom (0,0) to (3,3):");
System.out.println("Distance: " + distance);
System.out.println("Angle: " + angleToPoint + " degrees");
// Table of sine values
System.out.println("\nSine values (0° to 90°, step 15°):");
for (int deg = 0; deg <= 90; deg += 15) {
double rad = Math.toRadians(deg);
System.out.printf("%3d° : %.4f%n", deg, Math.sin(rad));
}
}
//help h1
// Math.sin(x), Math.cos(x), Math.tan(x) - trig functions
// Math.asin(x), Math.acos(x), Math.atan(x) - inverse
// Math.atan2(y, x) - angle from origin to (x,y)
// Math.toRadians(degrees) - convert to radians
// Math.toDegrees(radians) - convert to degrees
// Angles in radians by default
// Use atan2 for proper quadrant
//end
}
// Trigonometric functions
public class Trigonometry {
public static void main(String[] args) {
// Angles in radians
double angle = Math.PI / 4; // 45 degrees
System.out.println("Angle: " + angle + " radians (45 degrees)");
System.out.println();
// Basic trig functions
System.out.println("Basic trigonometry:");
System.out.println("sin(π/4): " + Math.sin(angle));
System.out.println("cos(π/4): " + Math.cos(angle));
System.out.println("tan(π/4): " + Math.tan(angle));
// Common angles
System.out.println("\nCommon angles:");
System.out.println("sin(0): " + Math.sin(0));
System.out.println("sin(π/2): " + Math.sin(Math.PI / 2)); // 90 degrees
System.out.println("sin(π): " + Math.sin(Math.PI)); // 180 degrees
System.out.println("cos(0): " + Math.cos(0));
System.out.println("cos(π): " + Math.cos(Math.PI));
// Inverse trig functions
System.out.println("\nInverse trigonometry:");
System.out.println("asin(1): " + Math.asin(1)); // π/2
System.out.println("acos(0): " + Math.acos(0)); // π/2
System.out.println("atan(1): " + Math.atan(1)); // π/4
System.out.println("atan2(1, 1): " + Math.atan2(1, 1)); // π/4
// Hyperbolic functions
System.out.println("\nHyperbolic functions:");
System.out.println("sinh(1): " + Math.sinh(1));
System.out.println("cosh(1): " + Math.cosh(1));
System.out.println("tanh(1): " + Math.tanh(1));
// Degree/radian conversion
System.out.println("\nDegree/radian conversion:");
double degrees = ;
double radians = Math.toRadians(degrees);
System.out.println(degrees + " degrees = " + radians + " radians");
System.out.println(radians + " radians = " + Math.toDegrees(radians) + " degrees");
// Trig with degrees
System.out.println("\nTrig with degrees (convert first):");
double angle45 = Math.toRadians(45);
double angle90 = Math.toRadians(90);
System.out.println("sin(45°): " + Math.sin(angle45));
System.out.println("cos(90°): " + Math.cos(angle90));
// Practical examples
System.out.println("\nPractical examples:");
// Right triangle
double adjacent = 3;
double opposite = 4;
double hypotenuse = Math.sqrt(Math.pow(adjacent, 2) + Math.pow(opposite, 2));
double angleRad = Math.atan2(opposite, adjacent);
double angleDeg = Math.toDegrees(angleRad);
System.out.println("Right triangle (3, 4, ?):");
System.out.println("Hypotenuse: " + hypotenuse);
System.out.println("Angle: " + angleDeg + " degrees");
// Circle point
double radius = 10;
double angleCircle = Math.toRadians(60);
double x = radius * Math.cos(angleCircle);
double y = radius * Math.sin(angleCircle);
System.out.println("\nPoint on circle (r=10, 60°):");
System.out.println("x: " + x);
System.out.println("y: " + y);
// Distance and angle between points
double x1 = 0, y1 = 0;
double x2 = 3, y2 = 3;
double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
double angleToPoint = Math.toDegrees(Math.atan2(y2 - y1, x2 - x1));
System.out.println("\nFrom (0,0) to (3,3):");
System.out.println("Distance: " + distance);
System.out.println("Angle: " + angleToPoint + " degrees");
// Table of sine values
System.out.println("\nSine values (0° to 90°, step 15°):");
for (int deg = 0; deg <= 90; deg += 15) {
double rad = Math.toRadians(deg);
System.out.printf("%3d° : %.4f%n", deg, Math.sin(rad));
}
}
//help h1
// Math.sin(x), Math.cos(x), Math.tan(x) - trig functions
// Math.asin(x), Math.acos(x), Math.atan(x) - inverse
// Math.atan2(y, x) - angle from origin to (x,y)
// Math.toRadians(degrees) - convert to radians
// Math.toDegrees(radians) - convert to degrees
// Angles in radians by default
// Use atan2 for proper quadrant
//end
}
// Trigonometric functions
public class Trigonometry {
public static void main(String[] args) {
// Angles in radians
double angle = Math.PI / 4; // 45 degrees
System.out.println("Angle: " + angle + " radians (45 degrees)");
System.out.println();
// Basic trig functions
System.out.println("Basic trigonometry:");
System.out.println("sin(π/4): " + Math.sin(angle));
System.out.println("cos(π/4): " + Math.cos(angle));
System.out.println("tan(π/4): " + Math.tan(angle));
// Common angles
System.out.println("\nCommon angles:");
System.out.println("sin(0): " + Math.sin(0));
System.out.println("sin(π/2): " + Math.sin(Math.PI / 2)); // 90 degrees
System.out.println("sin(π): " + Math.sin(Math.PI)); // 180 degrees
System.out.println("cos(0): " + Math.cos(0));
System.out.println("cos(π): " + Math.cos(Math.PI));
// Inverse trig functions
System.out.println("\nInverse trigonometry:");
System.out.println("asin(1): " + Math.asin(1)); // π/2
System.out.println("acos(0): " + Math.acos(0)); // π/2
System.out.println("atan(1): " + Math.atan(1)); // π/4
System.out.println("atan2(1, 1): " + Math.atan2(1, 1)); // π/4
// Hyperbolic functions
System.out.println("\nHyperbolic functions:");
System.out.println("sinh(1): " + Math.sinh(1));
System.out.println("cosh(1): " + Math.cosh(1));
System.out.println("tanh(1): " + Math.tanh(1));
// Degree/radian conversion
System.out.println("\nDegree/radian conversion:");
double degrees = ;
double radians = Math.toRadians(degrees);
System.out.println(degrees + " degrees = " + radians + " radians");
System.out.println(radians + " radians = " + Math.toDegrees(radians) + " degrees");
// Trig with degrees
System.out.println("\nTrig with degrees (convert first):");
double angle45 = Math.toRadians(45);
double angle90 = Math.toRadians(90);
System.out.println("sin(45°): " + Math.sin(angle45));
System.out.println("cos(90°): " + Math.cos(angle90));
// Practical examples
System.out.println("\nPractical examples:");
// Right triangle
double adjacent = 3;
double opposite = 4;
double hypotenuse = Math.sqrt(Math.pow(adjacent, 2) + Math.pow(opposite, 2));
double angleRad = Math.atan2(opposite, adjacent);
double angleDeg = Math.toDegrees(angleRad);
System.out.println("Right triangle (3, 4, ?):");
System.out.println("Hypotenuse: " + hypotenuse);
System.out.println("Angle: " + angleDeg + " degrees");
// Circle point
double radius = 10;
double angleCircle = Math.toRadians(60);
double x = radius * Math.cos(angleCircle);
double y = radius * Math.sin(angleCircle);
System.out.println("\nPoint on circle (r=10, 60°):");
System.out.println("x: " + x);
System.out.println("y: " + y);
// Distance and angle between points
double x1 = 0, y1 = 0;
double x2 = 3, y2 = 3;
double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
double angleToPoint = Math.toDegrees(Math.atan2(y2 - y1, x2 - x1));
System.out.println("\nFrom (0,0) to (3,3):");
System.out.println("Distance: " + distance);
System.out.println("Angle: " + angleToPoint + " degrees");
// Table of sine values
System.out.println("\nSine values (0° to 90°, step 15°):");
for (int deg = 0; deg <= 90; deg += 15) {
double rad = Math.toRadians(deg);
System.out.printf("%3d° : %.4f%n", deg, Math.sin(rad));
}
}
//help h1
// Math.sin(x), Math.cos(x), Math.tan(x) - trig functions
// Math.asin(x), Math.acos(x), Math.atan(x) - inverse
// Math.atan2(y, x) - angle from origin to (x,y)
// Math.toRadians(degrees) - convert to radians
// Math.toDegrees(radians) - convert to degrees
// Angles in radians by default
// Use atan2 for proper quadrant
//end
}
Radians
The unit of angle measurement used by Math methods. Convert degrees to radians with Math.toRadians().
Special Functions
Additional mathematical operations for advanced calculations.
Special.java
// Special functions
public class Special {
public static void main(String[] args) {
// Hypotenuse
System.out.println("Hypotenuse:");
System.out.println("hypot(3, 4): " + Math.hypot(3, 4));
System.out.println("hypot(5, 12): " + Math.hypot(5, 12));
// Expm1 (exp(x) - 1) - accurate for small x
System.out.println("\nExpm1 (e^x - 1):");
System.out.println("expm1(0): " + Math.expm1(0));
System.out.println("expm1(1): " + Math.expm1(1));
System.out.println("expm1(0.001): " + Math.expm1(0.001));
// Log1p (log(1 + x)) - accurate for small x
System.out.println("\nLog1p (ln(1 + x)):");
System.out.println("log1p(0): " + Math.log1p(0));
System.out.println("log1p(1): " + Math.log1p(1));
System.out.println("log1p(0.001): " + Math.log1p(0.001));
// CopySign
System.out.println("\nCopySign (magnitude of first, sign of second):");
System.out.println("copySign(5, -1): " + Math.copySign(5, -1)); // -5.0
System.out.println("copySign(-5, 1): " + Math.copySign(-5, 1)); // 5.0
System.out.println("copySign(3.14, -2): " + Math.copySign(3.14, -2));
// NextAfter
System.out.println("\nNextAfter (next floating-point value):");
double x = 1.0;
System.out.println("nextAfter(1.0, 2.0): " + Math.nextAfter(x, 2.0));
System.out.println("nextAfter(1.0, 0.0): " + Math.nextAfter(x, 0.0));
// Ulp (unit in last place)
System.out.println("\nUlp (spacing to next value):");
System.out.println("ulp(1.0): " + Math.ulp(1.0));
System.out.println("ulp(10.0): " + Math.ulp(10.0));
System.out.println("ulp(100.0): " + Math.ulp(100.0));
// Get exponent
System.out.println("\nGet exponent:");
System.out.println("getExponent(8.0): " + Math.getExponent(8.0)); // 3 (2^3 = 8)
System.out.println("getExponent(16.0): " + Math.getExponent(16.0)); // 4
System.out.println("getExponent(0.5): " + Math.getExponent(0.5)); // -1
// Scalb (multiply by 2^n)
System.out.println("\nScalb (multiply by 2^n):");
System.out.println("scalb(3.0, 2): " + Math.scalb(3.0, 2)); // 3 * 2^2 = 12
System.out.println("scalb(5.0, 3): " + Math.scalb(5.0, 3)); // 5 * 2^3 = 40
System.out.println("scalb(1.0, -2): " + Math.scalb(1.0, -2)); // 1 * 2^-2 = 0.25
// FMA (fused multiply-add) - a*b + c
System.out.println("\nFMA (fused multiply-add):");
System.out.println("fma(2, 3, 5): " + Math.fma(2, 3, 5)); // 2*3 + 5 = 11
System.out.println("fma(1.5, 2, 0.5): " + Math.fma(1.5, 2, 0.5)); // 3.5
// NextUp and NextDown
System.out.println("\nNextUp and NextDown:");
double val = 1.0;
System.out.println("nextUp(1.0): " + Math.nextUp(val));
System.out.println("nextDown(1.0): " + Math.nextDown(val));
// Special values
System.out.println("\nSpecial values:");
System.out.println("Infinity: " + Double.POSITIVE_INFINITY);
System.out.println("NaN: " + Double.NaN);
System.out.println("Max double: " + Double.MAX_VALUE);
System.out.println("Min double: " + Double.MIN_VALUE);
// Check special values
System.out.println("\nCheck special values:");
System.out.println("isNaN(0/0): " + Double.isNaN(0.0 / 0.0));
System.out.println("isInfinite(1/0): " + Double.isInfinite(1.0 / 0.0));
System.out.println("isFinite(100): " + Double.isFinite(100.0));
// Practical examples
System.out.println("\nPractical examples:");
// Calculate hypotenuse without overflow
double a = 1e200;
double b = 1e200;
double hypot = Math.hypot(a, b);
System.out.println("Hypotenuse of very large sides: " + hypot);
// Accurate small calculations
double small = 0.0001;
double expResult = Math.expm1(small);
double logResult = Math.log1p(small);
System.out.println("expm1(" + small + "): " + expResult);
System.out.println("log1p(" + small + "): " + logResult);
// Apply sign
double magnitude = 42;
double sign = -1;
double result = Math.copySign(magnitude, sign);
System.out.println("Apply sign of " + sign + " to " + magnitude + ": " + result);
}
//help h1
// Math.hypot(x, y) - sqrt(x^2 + y^2) without overflow
// Math.expm1(x) - e^x - 1 (accurate for small x)
// Math.log1p(x) - ln(1 + x) (accurate for small x)
// Math.copySign(magnitude, sign)
// Math.fma(a, b, c) - a*b + c (fused)
// Math.scalb(x, n) - x * 2^n
// Double.isNaN(), isInfinite(), isFinite()
//end
}
@seealso biginteger_intro, random_intro
Exercise: Practical.java
Calculate the distance between two points and the area of a circle