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.
Math class
A utility class providing static methods for mathematical operations like powers, roots, and trigonometry.
Basic Operations
The Math class provides fundamental mathematical operations.
Basic.java
Replay: real traced execution (multi-file project)
// 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
}
y2 ← 4.0, distance ← 5.0, radius ← 5.0, area ← 78.53981633974483
3public class Basic {4 public static void main(String[] args) {5 // Absolute value6 System.out.println("Absolute value:");7 System.out.println("abs(-5): " + Math.abs(-5));8 System.out.println("abs(-3.14): " + Math.abs(-3.14));9 System.out.println("abs(7): " + Math.abs(7));1011 // Power12 System.out.println("\nPower:");13 System.out.println("pow(2, 3): " + Math.pow(2, 3));14 System.out.println("pow(5, 2): " + Math.pow(5, 2));15 System.out.println("pow(2, 10): " + Math.pow(2, 10));16 System.out.println("pow(3, 0): " + Math.pow(3, 0));1718 // Square root19 System.out.println("\nSquare root:");20 System.out.println("sqrt(16): " + Math.sqrt(16));21 System.out.println("sqrt(2): " + Math.sqrt(2));22 System.out.println("sqrt(100): " + Math.sqrt(100));23 System.out.println("sqrt(0.25): " + Math.sqrt(0.25));2425 // Cube root26 System.out.println("\nCube root:");27 System.out.println("cbrt(8): " + Math.cbrt(8));28 System.out.println("cbrt(27): " + Math.cbrt(27));29 System.out.println("cbrt(64): " + Math.cbrt(64));3031 // Max and min32 System.out.println("\nMax and min:");33 System.out.println("max(5, 10): " + Math.max(5, 10));34 System.out.println("min(5, 10): " + Math.min(5, 10));35 System.out.println("max(-3, -7): " + Math.max(-3, -7));36 System.out.println("min(3.14, 2.71): " + Math.min(3.14, 2.71));3738 // Sign39 System.out.println("\nSign (signum):");40 System.out.println("signum(5): " + Math.signum(5)); // 1.041 System.out.println("signum(-5): " + Math.signum(-5)); // -1.042 System.out.println("signum(0): " + Math.signum(0)); // 0.04344 // Exponential and logarithm45 System.out.println("\nExponential and logarithm:");46 System.out.println("exp(1): " + Math.exp(1)); // e^147 System.out.println("exp(2): " + Math.exp(2)); // e^248 System.out.println("log(Math.E): " + Math.log(Math.E)); // ln(e) = 149 System.out.println("log10(100): " + Math.log10(100)); // log base 105051 // Constants52 System.out.println("\nMath constants:");53 System.out.println("PI: " + Math.PI);54 System.out.println("E: " + Math.E);5556 // Practical examples57 System.out.println("\nPractical examples:");58 59 // Distance60 double x1 = 0, y1 = 0, x2 = 3, y2→ 4.0 = 4;61 double distance→ 5.0 = Math.sqrt(Math.pow(x23.0 - x10.0, 2) + Math.pow(y24.0 - y10.0, 2));62 System.out.println("Distance from (0,0) to (3,4): " + distance5.0);6364 // Circle area65 double radius→ 5.0 = 5;66 double area→ 78.53981633974483 = Math.PI * Math.pow(radius5.0, 2);67 System.out.println("Circle area (r=5): " + area78.53981633974483);6869 // Compound interest70 double principal→ 1000.0 = 1000;71 double rate→ 0.05 = 0.05;72 int years→ 10 = 10;73 double amount→ 1628.894626777442 = principal1000.0 * Math.pow(1 + rate0.05, years10);74 System.out.println("Compound interest: $" + amount1628.894626777442);75 }outputAbsolute value: abs(-5): 5 abs(-3.14): 3.14 abs(7): 7 Power: pow(2, 3): 8.0 pow(5, 2): 25.0 pow(2, 10): 1024.0 pow(3, 0): 1.0 Square root: sqrt(16): 4.0 sqrt(2): 1.4142135623730951 sqrt(100): 10.0 sqrt(0.25): 0.5 Cube root: cbrt(8): 2.0 cbrt(27): 3.0 cbrt(64): 4.0 Max and min: max(5, 10): 10 min(5, 10): 5 max(-3, -7): -3 min(3.14, 2.71): 2.71 Sign (signum): signum(5): 1.0 signum(-5): -1.0 signum(0): 0.0 Exponential and logarithm: exp(1): 2.718281828459045 exp(2): 7.38905609893065 log(Math.E): 1.0 log10(100): 2.0 Math constants: PI: 3.141592653589793 E: 2.718281828459045 Practical examples: Distance from (0,0) to (3,4): 5.0 Circle area (r=5): 78.53981633974483 Compound interest: $1628.894626777442
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
Replay: real traced execution (multi-file project)
// 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
}
value ← 3.7
3public class Rounding {4 public static void main(String[] args) {5 double value→ 3.7 = 3.7;6 7 System.out.println("Value: " + value3.7);8 System.out.println();910 // Round11 System.out.println("round():");12 System.out.println("round(3.7): " + Math.round(3.7)); // 413 System.out.println("round(3.4): " + Math.round(3.4)); // 314 System.out.println("round(3.5): " + Math.round(3.5)); // 415 System.out.println("round(-3.5): " + Math.round(-3.5)); // -316 System.out.println("round(-3.6): " + Math.round(-3.6)); // -41718 // Ceil (round up)19 System.out.println("\nceil() - round up:");20 System.out.println("ceil(3.1): " + Math.ceil(3.1)); // 4.021 System.out.println("ceil(3.9): " + Math.ceil(3.9)); // 4.022 System.out.println("ceil(-3.1): " + Math.ceil(-3.1)); // -3.023 System.out.println("ceil(5.0): " + Math.ceil(5.0)); // 5.02425 // Floor (round down)26 System.out.println("\nfloor() - round down:");27 System.out.println("floor(3.1): " + Math.floor(3.1)); // 3.028 System.out.println("floor(3.9): " + Math.floor(3.9)); // 3.029 System.out.println("floor(-3.1): " + Math.floor(-3.1)); // -4.030 System.out.println("floor(5.0): " + Math.floor(5.0)); // 5.03132 // Compare all three33 System.out.println("\nCompare rounding methods:");34 double[] testValues = {2.3, 2.5, 2.7, -2.3, -2.5, -2.7};35 36 System.out.println("Value\tFloor\tRound\tCeil");37 for (double v : testValues) {outputValue: 3.7 round(): round(3.7): 4 round(3.4): 3 round(3.5): 4 round(-3.5): -3 round(-3.6): -4 ceil() - round up: ceil(3.1): 4.0 ceil(3.9): 4.0 ceil(-3.1): -3.0 ceil(5.0): 5.0 floor() - round down: floor(3.1): 3.0 floor(3.9): 3.0 floor(-3.1): -4.0 floor(5.0): 5.0 Compare rounding methods: Value Floor Round Ceilfor (double v : testValues)
pass 1 of 636System.out.println("Value\tFloor\tRound\tCeil");37for (double v2.3 : testValues) {38 System.out.printf("%.1f\t%.1f\t%d\t%.1f%n",39 v2.3, Math.floor(v), Math.round(v), Math.ceil(v));40}All 6 passes — pass 1 is the card above pass v1 2.3 2 2.5 3 2.7 4 -2.3 5 -2.5 6 -2.7 pi ← 3.141592653589793
42// Rounding to decimal places43System.out.println("\nRound to decimal places:");44double pi→ 3.141592653589793 = Math.PI;45System.out.println("Original: " + pi3.141592653589793);46System.out.println("2 decimals: " + roundToDecimals(pi3.141592653589793, 2));47System.out.println("4 decimals: " + roundToDecimals(pi, 4));output Round to decimal places: Original: 3.141592653589793scale ← 100.0
pass 1 of 379}80public static double roundToDecimals(double value3.141592653589793, int decimals2) {81 double scale→ 100.0 = Math.pow(10, decimals2);82 return Math.round(value3.141592653589793 * scale100.0) / scale;83}All 3 passes — pass 1 is the card above pass valuedecimalsscale1 3.141592653589793 2 100.0 2 3.141592653589793 4 10000.0 3 19.996 2 100.0 System.out.println("2 decimals: " + roundToDecimals(pi, 2));
45System.out.println("Original: " + pi);46System.out.println("2 decimals: " + roundToDecimals(pi3.141592653589793, 2));47System.out.println("4 decimals: " + roundToDecimals(pi3.141592653589793, 4));output2 decimals: 3.14items ← 47, itemsPerPage ← 10, pages ← 5, price ← 19.996
46System.out.println("2 decimals: " + roundToDecimals(pi, 2));47System.out.println("4 decimals: " + roundToDecimals(pi3.141592653589793, 4));4849// Truncate (toward zero)50System.out.println("\nTruncate (cast to int):");51System.out.println("(int) 3.9: " + (int) 3.9); // 352System.out.println("(int) -3.9: " + (int) -3.9); // -35354// Practical examples55System.out.println("\nPractical examples:");5657// Calculate pages needed58int items→ 47 = 47;59int itemsPerPage→ 10 = 10;60int pages→ 5 = (int) Math.ceil((double) items / itemsPerPage10);61System.out.println(items47 + " items, " + itemsPerPage10 + " per page = " + pages5 + " pages");6263// Round money64double price→ 19.996 = 19.996;65double rounded = roundToDecimals(price19.996, 2);66System.out.println("Price $" + price + " rounded: $" + rounded);output4 decimals: 3.1416 Truncate (cast to int): (int) 3.9: 3 (int) -3.9: -3 Practical examples: 47 items, 10 per page = 5 pagesrounded ← 20.0, number ← 47, multiple ← 10, nearest ← 50
64 double price = 19.996;65 double rounded→ 20.0 = roundToDecimals(price19.996, 2);66 System.out.println("Price $" + price19.996 + " rounded: $" + rounded20.0);6768 // Nearest multiple69 int number→ 47 = 47;70 int multiple→ 10 = 10;71 int nearest→ 50 = (int) (Math.round((double) number / multiple10) * multiple);72 System.out.println(number47 + " to nearest " + multiple10 + ": " + nearest50);7374 // Division with rounding75 System.out.println("\nDivision with rounding:");76 System.out.println("7 / 2 (floor): " + 7 / 2);77 System.out.println("7 / 2.0 (round): " + Math.round(7 / 2.0));78 System.out.println("7 / 2.0 (ceil): " + (int) Math.ceil(7 / 2.0));79}outputPrice $19.996 rounded: $20.0 47 to nearest 10: 50 Division with rounding: 7 / 2 (floor): 3 7 / 2.0 (round): 4 7 / 2.0 (ceil): 4
Comparison Functions
Find minimum and maximum values efficiently.
Comparison.java
Replay: real traced execution (multi-file project)
// 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 = 150;
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 = -10;
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 = 50;
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
}
public static void main(String[] args)
3public class Comparison {4 public static void main(String[] args) {5 // Max and min6 System.out.println("Max and min:");7 System.out.println("max(5, 10): " + Math.max(5, 10));8 System.out.println("min(5, 10): " + Math.min(5, 10));9 System.out.println("max(-5, -10): " + Math.max(-5, -10));10 System.out.println("min(3.14, 2.71): " + Math.min(3.14, 2.71));1112 // Max/min of multiple values13 System.out.println("\nMax/min of multiple:");14 int[] numbers = {5, 2, 9, 1, 7, 3};15 int maxValue = findMax(numbers);16 int minValue = findMin(numbers);outputMax and min: max(5, 10): 10 min(5, 10): 5 max(-5, -10): -5 min(3.14, 2.71): 2.71 Max/min of multiple:max ← 5
pass 1 of 285}86public static int findMax(int[] arr) {87 int max→ 5 = arr[0]5;88 for (int val : arr) {max ← 5
pass 1 of 1187int max = arr[0];88for (int val5 : arr) {89 max→ 5 = Math.max(max, val5);90}All 11 passes — pass 1 is the card above pass valmax1 5 5 2 2 5 3 9 5 → 9 4 1 9 5 7 9 6 3 9 7 23 23 8 45 23 → 45 9 12 45 10 67 45 → 67 11 34 67 return max;
90 }91 return max9;92}maxValue ← 9
14int[] numbers = {5, 2, 9, 1, 7, 3};15int maxValue→ 9 = findMax(numbers);16int minValue = findMin(numbers);17System.out.println("Array: " + java.util.Arrays.toString(numbers));min ← 5
pass 1 of 292}93public static int findMin(int[] arr) {94 int min→ 5 = arr[0]5;95 for (int val : arr) {min ← 5
pass 1 of 1194int min = arr[0];95for (int val5 : arr) {96 min→ 5 = Math.min(min, val5);97}All 11 passes — pass 1 is the card above pass valmin1 5 5 2 2 5 → 2 3 9 2 4 1 2 → 1 5 7 1 6 3 1 7 23 23 8 45 23 9 12 23 → 12 10 67 12 11 34 12 return min;
97 }98 return min1;99}minValue ← 1
15int maxValue = findMax(numbers);16int minValue→ 1 = findMin(numbers);17System.out.println("Array: " + java.util.Arrays.toString(numbers));18System.out.println("Max: " + maxValue9);19System.out.println("Min: " + minValue1);2021// Clamp (restrict to range)22System.out.println("\nClamp to range [0, 100]:");23System.out.println("clamp(-10): " + clamp(-10, 0, 100));24System.out.println("clamp(50): " + clamp(50, 0, 100));outputArray: [5, 2, 9, 1, 7, 3] Max: 9 Min: 1 Clamp to range [0, 100]:public static int clamp(int value, int min, int max)
pass 1 of 599}100public static int clamp(int value-10, int min0, int max100) {101 return Math.max(min0, Math.min(max100, value-10));102}All 5 passes — pass 1 is the card above pass valuemax1 -10 100 2 50 100 3 150 100 4 150 100 5 -5 30 System.out.println("clamp(-10): " + clamp(-10, 0, 100));
22System.out.println("\nClamp to range [0, 100]:");23System.out.println("clamp(-10): " + clamp(-10, 0, 100));24System.out.println("clamp(50): " + clamp(50, 0, 100));25System.out.println("clamp(150): " + clamp(150, 0, 100));outputclamp(-10): 0System.out.println("clamp(50): " + clamp(50, 0, 100));
23System.out.println("clamp(-10): " + clamp(-10, 0, 100));24System.out.println("clamp(50): " + clamp(50, 0, 100));25System.out.println("clamp(150): " + clamp(150, 0, 100));outputclamp(50): 50System.out.println("clamp(150): " + clamp(150, 0, 100));
24System.out.println("clamp(50): " + clamp(50, 0, 100));25System.out.println("clamp(150): " + clamp(150, 0, 100));2627// Sign comparison28System.out.println("\nSign comparison:");29System.out.println("signum(5): " + Math.signum(5));30System.out.println("signum(-5): " + Math.signum(-5));31System.out.println("signum(0): " + Math.signum(0));3233// Same sign check34System.out.println("\nSame sign:");35System.out.println("5 and 10: " + sameSign(5, 10));36System.out.println("5 and -10: " + sameSign(5, -10));outputclamp(150): 100 Sign comparison: signum(5): 1.0 signum(-5): -1.0 signum(0): 0.0 Same sign:public static boolean sameSign(double a, double b)
pass 1 of 3106}107public static boolean sameSign(double a5.0, double b10.0) {108 return Math.signum(a5.0) == Math.signum(b10.0);109}All 3 passes — pass 1 is the card above pass ab1 5.0 10.0 2 5.0 -10.0 3 -5.0 -10.0 System.out.println("5 and 10: " + sameSign(5, 10));
34System.out.println("\nSame sign:");35System.out.println("5 and 10: " + sameSign(5, 10));36System.out.println("5 and -10: " + sameSign(5, -10));37System.out.println("-5 and -10: " + sameSign(-5, -10));output5 and 10: trueSystem.out.println("5 and -10: " + sameSign(5, -10));
35System.out.println("5 and 10: " + sameSign(5, 10));36System.out.println("5 and -10: " + sameSign(5, -10));37System.out.println("-5 and -10: " + sameSign(-5, -10));output5 and -10: falsea ← 3.14159, b ← 3.14, tolerance ← 0.01
36System.out.println("5 and -10: " + sameSign(5, -10));37System.out.println("-5 and -10: " + sameSign(-5, -10));3839// Absolute difference40System.out.println("\nAbsolute difference:");41System.out.println("|5 - 10|: " + Math.abs(5 - 10));42System.out.println("|10 - 5|: " + Math.abs(10 - 5));43System.out.println("|-5 - (-10)|: " + Math.abs(-5 - (-10)));4445// Within tolerance46System.out.println("\nWithin tolerance:");47double a→ 3.14159 = 3.14159;48double b→ 3.14 = 3.14;49double tolerance→ 0.01 = 0.01;50System.out.println(a3.14159 + " ≈ " + b3.14 + " (±" + tolerance0.01 + "): " + 51 withinTolerance(a3.14159, b3.14, tolerance0.01));output-5 and -10: true Absolute difference: |5 - 10|: 5 |10 - 5|: 5 |-5 - (-10)|: 5 Within tolerance:public static boolean withinTolerance(double a, double b, double toler…
109}110public static boolean withinTolerance(double a3.14159, double b3.14, double tolerance0.01) {111 return Math.abs(a3.14159 - b3.14) <= tolerance0.01;112}score ← 150
49double tolerance = 0.01;50System.out.println(a3.14159 + " ≈ " + b3.14 + " (±" + tolerance0.01 + "): " + 51 withinTolerance(a3.14159, b3.14, tolerance0.01));5253// Practical examples54System.out.println("\nPractical examples:");5556// Keep score in bounds57int score→ 150 = 150; //@score=-10, 5058int bounded = clamp(score150, 0, 100);59System.out.println("Score " + score + " bounded to [0,100]: " + bounded);output3.14159 ≈ 3.14 (±0.01): true Practical examples:bounded ← 100, volume ← 1.5
57int score = 150; //@score=-10, 5058int bounded→ 100 = clamp(score150, 0, 100);59System.out.println("Score " + score150 + " bounded to [0,100]: " + bounded100);6061// Volume control62double volume→ 1.5 = 1.5;63double validVolume = clamp(volume1.5, 0.0, 1.0);64System.out.println("Volume " + volume + " clamped to [0,1]: " + validVolume);outputScore 150 bounded to [0,100]: 100public static double clamp(double value, double min, double max)
104public static double clamp(double value1.5, double min0.0, double max1.0) {105 return Math.max(min0.0, Math.min(max1.0, value1.5));106}validVolume ← 1.0, temp ← -5, minTemp ← 0, maxTemp ← 30
62double volume = 1.5;63double validVolume→ 1.0 = clamp(volume1.5, 0.0, 1.0);64System.out.println("Volume " + volume1.5 + " clamped to [0,1]: " + validVolume1.0);6566// Temperature range67int temp→ -5 = -5;68int minTemp→ 0 = 0;69int maxTemp→ 30 = 30;70int validTemp = clamp(temp-5, minTemp0, maxTemp30);71System.out.println("Temp " + temp + "° bounded to [" + minTemp + "," + maxTemp + "]: " + validTemp);outputVolume 1.5 clamped to [0,1]: 1.0validTemp ← 0
69int maxTemp = 30;70int validTemp→ 0 = clamp(temp-5, minTemp0, maxTemp30);71System.out.println("Temp " + temp-5 + "° bounded to [" + minTemp0 + "," + maxTemp30 + "]: " + validTemp0);7273// Find range74int[] values = {23, 45, 12, 67, 34};75int min = findMin(values);76int max = findMax(values);outputTemp -5° bounded to [0,30]: 0min ← 23
pass 2 of 292}93public static int findMin(int[] arr) {94 int min→ 23 = arr[0]23;95 for (int val : arr) {return min;
97 }98 return min12;99}min ← 12
74int[] values = {23, 45, 12, 67, 34};75int min→ 12 = findMin(values);76int max = findMax(values);77int range = max - min;max ← 23
pass 2 of 285}86public static int findMax(int[] arr) {87 int max→ 23 = arr[0]23;88 for (int val : arr) {return max;
90 }91 return max67;92}max ← 67, range ← 55
75int min = findMin(values);76int max→ 67 = findMax(values);77int range→ 55 = max67 - min12;78System.out.println("\nValues: " + java.util.Arrays.toString(values));79System.out.println("Range: " + min12 + " to " + max67 + " (span: " + range55 + ")");8081// Midpoint82System.out.println("\nMidpoint:");83System.out.println("Between 10 and 20: " + midpoint(10, 20));84System.out.println("Between -5 and 15: " + midpoint(-5, 15));output Values: [23, 45, 12, 67, 34] Range: 12 to 67 (span: 55) Midpoint:public static double midpoint(double a, double b)
pass 1 of 2112}113public static double midpoint(double a10.0, double b20.0) {114 return (a10.0 + b20.0) / 2;115}System.out.println("Between 10 and 20: " + midpoint(10, 20));
82 System.out.println("\nMidpoint:");83 System.out.println("Between 10 and 20: " + midpoint(10, 20));84 System.out.println("Between -5 and 15: " + midpoint(-5, 15));85}outputBetween 10 and 20: 15.0public static double midpoint(double a, double b)
pass 2 of 2112}113public static double midpoint(double a-5.0, double b15.0) {114 return (a-5.0 + b15.0) / 2;115}System.out.println("Between -5 and 15: " + midpoint(-5, 15));
83 System.out.println("Between 10 and 20: " + midpoint(10, 20));84 System.out.println("Between -5 and 15: " + midpoint(-5, 15));85}outputBetween -5 and 15: 5.0
public static void main(String[] args)
3public class Comparison {4 public static void main(String[] args) {5 // Max and min6 System.out.println("Max and min:");7 System.out.println("max(5, 10): " + Math.max(5, 10));8 System.out.println("min(5, 10): " + Math.min(5, 10));9 System.out.println("max(-5, -10): " + Math.max(-5, -10));10 System.out.println("min(3.14, 2.71): " + Math.min(3.14, 2.71));1112 // Max/min of multiple values13 System.out.println("\nMax/min of multiple:");14 int[] numbers = {5, 2, 9, 1, 7, 3};15 int maxValue = findMax(numbers);16 int minValue = findMin(numbers);outputMax and min: max(5, 10): 10 min(5, 10): 5 max(-5, -10): -5 min(3.14, 2.71): 2.71 Max/min of multiple:max ← 5
pass 1 of 285}86public static int findMax(int[] arr) {87 int max→ 5 = arr[0]5;88 for (int val : arr) {max ← 5
pass 1 of 1187int max = arr[0];88for (int val5 : arr) {89 max→ 5 = Math.max(max, val5);90}All 11 passes — pass 1 is the card above pass valmax1 5 5 2 2 5 3 9 5 → 9 4 1 9 5 7 9 6 3 9 7 23 23 8 45 23 → 45 9 12 45 10 67 45 → 67 11 34 67 return max;
90 }91 return max9;92}maxValue ← 9
14int[] numbers = {5, 2, 9, 1, 7, 3};15int maxValue→ 9 = findMax(numbers);16int minValue = findMin(numbers);17System.out.println("Array: " + java.util.Arrays.toString(numbers));min ← 5
pass 1 of 292}93public static int findMin(int[] arr) {94 int min→ 5 = arr[0]5;95 for (int val : arr) {min ← 5
pass 1 of 1194int min = arr[0];95for (int val5 : arr) {96 min→ 5 = Math.min(min, val5);97}All 11 passes — pass 1 is the card above pass valmin1 5 5 2 2 5 → 2 3 9 2 4 1 2 → 1 5 7 1 6 3 1 7 23 23 8 45 23 9 12 23 → 12 10 67 12 11 34 12 return min;
97 }98 return min1;99}minValue ← 1
15int maxValue = findMax(numbers);16int minValue→ 1 = findMin(numbers);17System.out.println("Array: " + java.util.Arrays.toString(numbers));18System.out.println("Max: " + maxValue9);19System.out.println("Min: " + minValue1);2021// Clamp (restrict to range)22System.out.println("\nClamp to range [0, 100]:");23System.out.println("clamp(-10): " + clamp(-10, 0, 100));24System.out.println("clamp(50): " + clamp(50, 0, 100));outputArray: [5, 2, 9, 1, 7, 3] Max: 9 Min: 1 Clamp to range [0, 100]:public static int clamp(int value, int min, int max)
pass 1 of 599}100public static int clamp(int value-10, int min0, int max100) {101 return Math.max(min0, Math.min(max100, value-10));102}All 5 passes — pass 1 is the card above pass valuemax1 -10 100 2 50 100 3 150 100 4 -10 100 5 -5 30 System.out.println("clamp(-10): " + clamp(-10, 0, 100));
22System.out.println("\nClamp to range [0, 100]:");23System.out.println("clamp(-10): " + clamp(-10, 0, 100));24System.out.println("clamp(50): " + clamp(50, 0, 100));25System.out.println("clamp(150): " + clamp(150, 0, 100));outputclamp(-10): 0System.out.println("clamp(50): " + clamp(50, 0, 100));
23System.out.println("clamp(-10): " + clamp(-10, 0, 100));24System.out.println("clamp(50): " + clamp(50, 0, 100));25System.out.println("clamp(150): " + clamp(150, 0, 100));outputclamp(50): 50System.out.println("clamp(150): " + clamp(150, 0, 100));
24System.out.println("clamp(50): " + clamp(50, 0, 100));25System.out.println("clamp(150): " + clamp(150, 0, 100));2627// Sign comparison28System.out.println("\nSign comparison:");29System.out.println("signum(5): " + Math.signum(5));30System.out.println("signum(-5): " + Math.signum(-5));31System.out.println("signum(0): " + Math.signum(0));3233// Same sign check34System.out.println("\nSame sign:");35System.out.println("5 and 10: " + sameSign(5, 10));36System.out.println("5 and -10: " + sameSign(5, -10));outputclamp(150): 100 Sign comparison: signum(5): 1.0 signum(-5): -1.0 signum(0): 0.0 Same sign:public static boolean sameSign(double a, double b)
pass 1 of 3106}107public static boolean sameSign(double a5.0, double b10.0) {108 return Math.signum(a5.0) == Math.signum(b10.0);109}All 3 passes — pass 1 is the card above pass ab1 5.0 10.0 2 5.0 -10.0 3 -5.0 -10.0 System.out.println("5 and 10: " + sameSign(5, 10));
34System.out.println("\nSame sign:");35System.out.println("5 and 10: " + sameSign(5, 10));36System.out.println("5 and -10: " + sameSign(5, -10));37System.out.println("-5 and -10: " + sameSign(-5, -10));output5 and 10: trueSystem.out.println("5 and -10: " + sameSign(5, -10));
35System.out.println("5 and 10: " + sameSign(5, 10));36System.out.println("5 and -10: " + sameSign(5, -10));37System.out.println("-5 and -10: " + sameSign(-5, -10));output5 and -10: falsea ← 3.14159, b ← 3.14, tolerance ← 0.01
36System.out.println("5 and -10: " + sameSign(5, -10));37System.out.println("-5 and -10: " + sameSign(-5, -10));3839// Absolute difference40System.out.println("\nAbsolute difference:");41System.out.println("|5 - 10|: " + Math.abs(5 - 10));42System.out.println("|10 - 5|: " + Math.abs(10 - 5));43System.out.println("|-5 - (-10)|: " + Math.abs(-5 - (-10)));4445// Within tolerance46System.out.println("\nWithin tolerance:");47double a→ 3.14159 = 3.14159;48double b→ 3.14 = 3.14;49double tolerance→ 0.01 = 0.01;50System.out.println(a3.14159 + " ≈ " + b3.14 + " (±" + tolerance0.01 + "): " + 51 withinTolerance(a3.14159, b3.14, tolerance0.01));output-5 and -10: true Absolute difference: |5 - 10|: 5 |10 - 5|: 5 |-5 - (-10)|: 5 Within tolerance:public static boolean withinTolerance(double a, double b, double toler…
109}110public static boolean withinTolerance(double a3.14159, double b3.14, double tolerance0.01) {111 return Math.abs(a3.14159 - b3.14) <= tolerance0.01;112}score ← -10
49double tolerance = 0.01;50System.out.println(a3.14159 + " ≈ " + b3.14 + " (±" + tolerance0.01 + "): " + 51 withinTolerance(a3.14159, b3.14, tolerance0.01));5253// Practical examples54System.out.println("\nPractical examples:");5556// Keep score in bounds57int score→ -10 = -10;58int bounded = clamp(score-10, 0, 100);59System.out.println("Score " + score + " bounded to [0,100]: " + bounded);output3.14159 ≈ 3.14 (±0.01): true Practical examples:bounded ← 0, volume ← 1.5
57int score = -10;58int bounded→ 0 = clamp(score-10, 0, 100);59System.out.println("Score " + score-10 + " bounded to [0,100]: " + bounded0);6061// Volume control62double volume→ 1.5 = 1.5;63double validVolume = clamp(volume1.5, 0.0, 1.0);64System.out.println("Volume " + volume + " clamped to [0,1]: " + validVolume);outputScore -10 bounded to [0,100]: 0public static double clamp(double value, double min, double max)
104public static double clamp(double value1.5, double min0.0, double max1.0) {105 return Math.max(min0.0, Math.min(max1.0, value1.5));106}validVolume ← 1.0, temp ← -5, minTemp ← 0, maxTemp ← 30
62double volume = 1.5;63double validVolume→ 1.0 = clamp(volume1.5, 0.0, 1.0);64System.out.println("Volume " + volume1.5 + " clamped to [0,1]: " + validVolume1.0);6566// Temperature range67int temp→ -5 = -5;68int minTemp→ 0 = 0;69int maxTemp→ 30 = 30;70int validTemp = clamp(temp-5, minTemp0, maxTemp30);71System.out.println("Temp " + temp + "° bounded to [" + minTemp + "," + maxTemp + "]: " + validTemp);outputVolume 1.5 clamped to [0,1]: 1.0validTemp ← 0
69int maxTemp = 30;70int validTemp→ 0 = clamp(temp-5, minTemp0, maxTemp30);71System.out.println("Temp " + temp-5 + "° bounded to [" + minTemp0 + "," + maxTemp30 + "]: " + validTemp0);7273// Find range74int[] values = {23, 45, 12, 67, 34};75int min = findMin(values);76int max = findMax(values);outputTemp -5° bounded to [0,30]: 0min ← 23
pass 2 of 292}93public static int findMin(int[] arr) {94 int min→ 23 = arr[0]23;95 for (int val : arr) {return min;
97 }98 return min12;99}min ← 12
74int[] values = {23, 45, 12, 67, 34};75int min→ 12 = findMin(values);76int max = findMax(values);77int range = max - min;max ← 23
pass 2 of 285}86public static int findMax(int[] arr) {87 int max→ 23 = arr[0]23;88 for (int val : arr) {return max;
90 }91 return max67;92}max ← 67, range ← 55
75int min = findMin(values);76int max→ 67 = findMax(values);77int range→ 55 = max67 - min12;78System.out.println("\nValues: " + java.util.Arrays.toString(values));79System.out.println("Range: " + min12 + " to " + max67 + " (span: " + range55 + ")");8081// Midpoint82System.out.println("\nMidpoint:");83System.out.println("Between 10 and 20: " + midpoint(10, 20));84System.out.println("Between -5 and 15: " + midpoint(-5, 15));output Values: [23, 45, 12, 67, 34] Range: 12 to 67 (span: 55) Midpoint:public static double midpoint(double a, double b)
pass 1 of 2112}113public static double midpoint(double a10.0, double b20.0) {114 return (a10.0 + b20.0) / 2;115}System.out.println("Between 10 and 20: " + midpoint(10, 20));
82 System.out.println("\nMidpoint:");83 System.out.println("Between 10 and 20: " + midpoint(10, 20));84 System.out.println("Between -5 and 15: " + midpoint(-5, 15));85}outputBetween 10 and 20: 15.0public static double midpoint(double a, double b)
pass 2 of 2112}113public static double midpoint(double a-5.0, double b15.0) {114 return (a-5.0 + b15.0) / 2;115}System.out.println("Between -5 and 15: " + midpoint(-5, 15));
83 System.out.println("Between 10 and 20: " + midpoint(10, 20));84 System.out.println("Between -5 and 15: " + midpoint(-5, 15));85}outputBetween -5 and 15: 5.0
public static void main(String[] args)
3public class Comparison {4 public static void main(String[] args) {5 // Max and min6 System.out.println("Max and min:");7 System.out.println("max(5, 10): " + Math.max(5, 10));8 System.out.println("min(5, 10): " + Math.min(5, 10));9 System.out.println("max(-5, -10): " + Math.max(-5, -10));10 System.out.println("min(3.14, 2.71): " + Math.min(3.14, 2.71));1112 // Max/min of multiple values13 System.out.println("\nMax/min of multiple:");14 int[] numbers = {5, 2, 9, 1, 7, 3};15 int maxValue = findMax(numbers);16 int minValue = findMin(numbers);outputMax and min: max(5, 10): 10 min(5, 10): 5 max(-5, -10): -5 min(3.14, 2.71): 2.71 Max/min of multiple:max ← 5
pass 1 of 285}86public static int findMax(int[] arr) {87 int max→ 5 = arr[0]5;88 for (int val : arr) {max ← 5
pass 1 of 1187int max = arr[0];88for (int val5 : arr) {89 max→ 5 = Math.max(max, val5);90}All 11 passes — pass 1 is the card above pass valmax1 5 5 2 2 5 3 9 5 → 9 4 1 9 5 7 9 6 3 9 7 23 23 8 45 23 → 45 9 12 45 10 67 45 → 67 11 34 67 return max;
90 }91 return max9;92}maxValue ← 9
14int[] numbers = {5, 2, 9, 1, 7, 3};15int maxValue→ 9 = findMax(numbers);16int minValue = findMin(numbers);17System.out.println("Array: " + java.util.Arrays.toString(numbers));min ← 5
pass 1 of 292}93public static int findMin(int[] arr) {94 int min→ 5 = arr[0]5;95 for (int val : arr) {min ← 5
pass 1 of 1194int min = arr[0];95for (int val5 : arr) {96 min→ 5 = Math.min(min, val5);97}All 11 passes — pass 1 is the card above pass valmin1 5 5 2 2 5 → 2 3 9 2 4 1 2 → 1 5 7 1 6 3 1 7 23 23 8 45 23 9 12 23 → 12 10 67 12 11 34 12 return min;
97 }98 return min1;99}minValue ← 1
15int maxValue = findMax(numbers);16int minValue→ 1 = findMin(numbers);17System.out.println("Array: " + java.util.Arrays.toString(numbers));18System.out.println("Max: " + maxValue9);19System.out.println("Min: " + minValue1);2021// Clamp (restrict to range)22System.out.println("\nClamp to range [0, 100]:");23System.out.println("clamp(-10): " + clamp(-10, 0, 100));24System.out.println("clamp(50): " + clamp(50, 0, 100));outputArray: [5, 2, 9, 1, 7, 3] Max: 9 Min: 1 Clamp to range [0, 100]:public static int clamp(int value, int min, int max)
pass 1 of 599}100public static int clamp(int value-10, int min0, int max100) {101 return Math.max(min0, Math.min(max100, value-10));102}All 5 passes — pass 1 is the card above pass valuemax1 -10 100 2 50 100 3 150 100 4 50 100 5 -5 30 System.out.println("clamp(-10): " + clamp(-10, 0, 100));
22System.out.println("\nClamp to range [0, 100]:");23System.out.println("clamp(-10): " + clamp(-10, 0, 100));24System.out.println("clamp(50): " + clamp(50, 0, 100));25System.out.println("clamp(150): " + clamp(150, 0, 100));outputclamp(-10): 0System.out.println("clamp(50): " + clamp(50, 0, 100));
23System.out.println("clamp(-10): " + clamp(-10, 0, 100));24System.out.println("clamp(50): " + clamp(50, 0, 100));25System.out.println("clamp(150): " + clamp(150, 0, 100));outputclamp(50): 50System.out.println("clamp(150): " + clamp(150, 0, 100));
24System.out.println("clamp(50): " + clamp(50, 0, 100));25System.out.println("clamp(150): " + clamp(150, 0, 100));2627// Sign comparison28System.out.println("\nSign comparison:");29System.out.println("signum(5): " + Math.signum(5));30System.out.println("signum(-5): " + Math.signum(-5));31System.out.println("signum(0): " + Math.signum(0));3233// Same sign check34System.out.println("\nSame sign:");35System.out.println("5 and 10: " + sameSign(5, 10));36System.out.println("5 and -10: " + sameSign(5, -10));outputclamp(150): 100 Sign comparison: signum(5): 1.0 signum(-5): -1.0 signum(0): 0.0 Same sign:public static boolean sameSign(double a, double b)
pass 1 of 3106}107public static boolean sameSign(double a5.0, double b10.0) {108 return Math.signum(a5.0) == Math.signum(b10.0);109}All 3 passes — pass 1 is the card above pass ab1 5.0 10.0 2 5.0 -10.0 3 -5.0 -10.0 System.out.println("5 and 10: " + sameSign(5, 10));
34System.out.println("\nSame sign:");35System.out.println("5 and 10: " + sameSign(5, 10));36System.out.println("5 and -10: " + sameSign(5, -10));37System.out.println("-5 and -10: " + sameSign(-5, -10));output5 and 10: trueSystem.out.println("5 and -10: " + sameSign(5, -10));
35System.out.println("5 and 10: " + sameSign(5, 10));36System.out.println("5 and -10: " + sameSign(5, -10));37System.out.println("-5 and -10: " + sameSign(-5, -10));output5 and -10: falsea ← 3.14159, b ← 3.14, tolerance ← 0.01
36System.out.println("5 and -10: " + sameSign(5, -10));37System.out.println("-5 and -10: " + sameSign(-5, -10));3839// Absolute difference40System.out.println("\nAbsolute difference:");41System.out.println("|5 - 10|: " + Math.abs(5 - 10));42System.out.println("|10 - 5|: " + Math.abs(10 - 5));43System.out.println("|-5 - (-10)|: " + Math.abs(-5 - (-10)));4445// Within tolerance46System.out.println("\nWithin tolerance:");47double a→ 3.14159 = 3.14159;48double b→ 3.14 = 3.14;49double tolerance→ 0.01 = 0.01;50System.out.println(a3.14159 + " ≈ " + b3.14 + " (±" + tolerance0.01 + "): " + 51 withinTolerance(a3.14159, b3.14, tolerance0.01));output-5 and -10: true Absolute difference: |5 - 10|: 5 |10 - 5|: 5 |-5 - (-10)|: 5 Within tolerance:public static boolean withinTolerance(double a, double b, double toler…
109}110public static boolean withinTolerance(double a3.14159, double b3.14, double tolerance0.01) {111 return Math.abs(a3.14159 - b3.14) <= tolerance0.01;112}score ← 50
49double tolerance = 0.01;50System.out.println(a3.14159 + " ≈ " + b3.14 + " (±" + tolerance0.01 + "): " + 51 withinTolerance(a3.14159, b3.14, tolerance0.01));5253// Practical examples54System.out.println("\nPractical examples:");5556// Keep score in bounds57int score→ 50 = 50;58int bounded = clamp(score50, 0, 100);59System.out.println("Score " + score + " bounded to [0,100]: " + bounded);output3.14159 ≈ 3.14 (±0.01): true Practical examples:bounded ← 50, volume ← 1.5
57int score = 50;58int bounded→ 50 = clamp(score50, 0, 100);59System.out.println("Score " + score50 + " bounded to [0,100]: " + bounded50);6061// Volume control62double volume→ 1.5 = 1.5;63double validVolume = clamp(volume1.5, 0.0, 1.0);64System.out.println("Volume " + volume + " clamped to [0,1]: " + validVolume);outputScore 50 bounded to [0,100]: 50public static double clamp(double value, double min, double max)
104public static double clamp(double value1.5, double min0.0, double max1.0) {105 return Math.max(min0.0, Math.min(max1.0, value1.5));106}validVolume ← 1.0, temp ← -5, minTemp ← 0, maxTemp ← 30
62double volume = 1.5;63double validVolume→ 1.0 = clamp(volume1.5, 0.0, 1.0);64System.out.println("Volume " + volume1.5 + " clamped to [0,1]: " + validVolume1.0);6566// Temperature range67int temp→ -5 = -5;68int minTemp→ 0 = 0;69int maxTemp→ 30 = 30;70int validTemp = clamp(temp-5, minTemp0, maxTemp30);71System.out.println("Temp " + temp + "° bounded to [" + minTemp + "," + maxTemp + "]: " + validTemp);outputVolume 1.5 clamped to [0,1]: 1.0validTemp ← 0
69int maxTemp = 30;70int validTemp→ 0 = clamp(temp-5, minTemp0, maxTemp30);71System.out.println("Temp " + temp-5 + "° bounded to [" + minTemp0 + "," + maxTemp30 + "]: " + validTemp0);7273// Find range74int[] values = {23, 45, 12, 67, 34};75int min = findMin(values);76int max = findMax(values);outputTemp -5° bounded to [0,30]: 0min ← 23
pass 2 of 292}93public static int findMin(int[] arr) {94 int min→ 23 = arr[0]23;95 for (int val : arr) {return min;
97 }98 return min12;99}min ← 12
74int[] values = {23, 45, 12, 67, 34};75int min→ 12 = findMin(values);76int max = findMax(values);77int range = max - min;max ← 23
pass 2 of 285}86public static int findMax(int[] arr) {87 int max→ 23 = arr[0]23;88 for (int val : arr) {return max;
90 }91 return max67;92}max ← 67, range ← 55
75int min = findMin(values);76int max→ 67 = findMax(values);77int range→ 55 = max67 - min12;78System.out.println("\nValues: " + java.util.Arrays.toString(values));79System.out.println("Range: " + min12 + " to " + max67 + " (span: " + range55 + ")");8081// Midpoint82System.out.println("\nMidpoint:");83System.out.println("Between 10 and 20: " + midpoint(10, 20));84System.out.println("Between -5 and 15: " + midpoint(-5, 15));output Values: [23, 45, 12, 67, 34] Range: 12 to 67 (span: 55) Midpoint:public static double midpoint(double a, double b)
pass 1 of 2112}113public static double midpoint(double a10.0, double b20.0) {114 return (a10.0 + b20.0) / 2;115}System.out.println("Between 10 and 20: " + midpoint(10, 20));
82 System.out.println("\nMidpoint:");83 System.out.println("Between 10 and 20: " + midpoint(10, 20));84 System.out.println("Between -5 and 15: " + midpoint(-5, 15));85}outputBetween 10 and 20: 15.0public static double midpoint(double a, double b)
pass 2 of 2112}113public static double midpoint(double a-5.0, double b15.0) {114 return (a-5.0 + b15.0) / 2;115}System.out.println("Between -5 and 15: " + midpoint(-5, 15));
83 System.out.println("Between 10 and 20: " + midpoint(10, 20));84 System.out.println("Between -5 and 15: " + midpoint(-5, 15));85}outputBetween -5 and 15: 5.0
Trigonometric Functions
Calculate sine, cosine, and other trigonometric values.
Trigonometry.java
Replay: real traced execution (multi-file project)
// 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 = 45;
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 = 30;
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 = 60;
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
}
angle ← 0.7853981633974483, degrees ← 45.0, radians ← 0.7853981633974483
3public class Trigonometry {4 public static void main(String[] args) {5 // Angles in radians6 double angle→ 0.7853981633974483 = Math.PI / 4; // 45 degrees7 8 System.out.println("Angle: " + angle0.7853981633974483 + " radians (45 degrees)");9 System.out.println();1011 // Basic trig functions12 System.out.println("Basic trigonometry:");13 System.out.println("sin(π/4): " + Math.sin(angle0.7853981633974483));14 System.out.println("cos(π/4): " + Math.cos(angle0.7853981633974483));15 System.out.println("tan(π/4): " + Math.tan(angle0.7853981633974483));1617 // Common angles18 System.out.println("\nCommon angles:");19 System.out.println("sin(0): " + Math.sin(0));20 System.out.println("sin(π/2): " + Math.sin(Math.PI / 2)); // 90 degrees21 System.out.println("sin(π): " + Math.sin(Math.PI)); // 180 degrees22 System.out.println("cos(0): " + Math.cos(0));23 System.out.println("cos(π): " + Math.cos(Math.PI));2425 // Inverse trig functions26 System.out.println("\nInverse trigonometry:");27 System.out.println("asin(1): " + Math.asin(1)); // π/228 System.out.println("acos(0): " + Math.acos(0)); // π/229 System.out.println("atan(1): " + Math.atan(1)); // π/430 System.out.println("atan2(1, 1): " + Math.atan2(1, 1)); // π/43132 // Hyperbolic functions33 System.out.println("\nHyperbolic functions:");34 System.out.println("sinh(1): " + Math.sinh(1));35 System.out.println("cosh(1): " + Math.cosh(1));36 System.out.println("tanh(1): " + Math.tanh(1));3738 // Degree/radian conversion39 System.out.println("\nDegree/radian conversion:");40 double degrees→ 45.0 = 45; //@degrees=30, 6041 double radians→ 0.7853981633974483 = Math.toRadians(degrees45.0);42 System.out.println(degrees45.0 + " degrees = " + radians0.7853981633974483 + " radians");43 System.out.println(radians0.7853981633974483 + " radians = " + Math.toDegrees(radians) + " degrees");4445 // Trig with degrees46 System.out.println("\nTrig with degrees (convert first):");47 double angle45→ 0.7853981633974483 = Math.toRadians(45);48 double angle90→ 1.5707963267948966 = Math.toRadians(90);49 System.out.println("sin(45°): " + Math.sin(angle450.7853981633974483));50 System.out.println("cos(90°): " + Math.cos(angle901.5707963267948966));5152 // Practical examples53 System.out.println("\nPractical examples:");54 55 // Right triangle56 double adjacent→ 3.0 = 3;57 double opposite→ 4.0 = 4;58 double hypotenuse→ 5.0 = Math.sqrt(Math.pow(adjacent3.0, 2) + Math.pow(opposite4.0, 2));59 double angleRad→ 0.9272952180016122 = Math.atan2(opposite4.0, adjacent3.0);60 double angleDeg→ 53.13010235415598 = Math.toDegrees(angleRad0.9272952180016122);61 62 System.out.println("Right triangle (3, 4, ?):");63 System.out.println("Hypotenuse: " + hypotenuse5.0);64 System.out.println("Angle: " + angleDeg53.13010235415598 + " degrees");6566 // Circle point67 double radius→ 10.0 = 10;68 double angleCircle→ 1.0471975511965976 = Math.toRadians(60);69 double x→ 5.000000000000001 = radius10.0 * Math.cos(angleCircle1.0471975511965976);70 double y→ 8.660254037844386 = radius10.0 * Math.sin(angleCircle1.0471975511965976);71 System.out.println("\nPoint on circle (r=10, 60°):");72 System.out.println("x: " + x5.000000000000001);73 System.out.println("y: " + y8.660254037844386);7475 // Distance and angle between points76 double x1 = 0, y1→ 0.0 = 0;77 double x2 = 3, y2→ 3.0 = 3;78 double distance→ 4.242640687119285 = Math.sqrt(Math.pow(x23.0 - x10.0, 2) + Math.pow(y23.0 - y10.0, 2));79 double angleToPoint→ 45.0 = Math.toDegrees(Math.atan2(y23.0 - y10.0, x23.0 - x10.0));80 81 System.out.println("\nFrom (0,0) to (3,3):");82 System.out.println("Distance: " + distance4.242640687119285);83 System.out.println("Angle: " + angleToPoint45.0 + " degrees");8485 // Table of sine values86 System.out.println("\nSine values (0° to 90°, step 15°):");87 for (int deg = 0; deg <= 90; deg += 15) {outputAngle: 0.7853981633974483 radians (45 degrees) Basic trigonometry: sin(π/4): 0.7071067811865475 cos(π/4): 0.7071067811865476 tan(π/4): 0.9999999999999999 Common angles: sin(0): 0.0 sin(π/2): 1.0 sin(π): 1.2246467991473532E-16 cos(0): 1.0 cos(π): -1.0 Inverse trigonometry: asin(1): 1.5707963267948966 acos(0): 1.5707963267948966 atan(1): 0.7853981633974483 atan2(1, 1): 0.7853981633974483 Hyperbolic functions: sinh(1): 1.1752011936438014 cosh(1): 1.543080634815244 tanh(1): 0.7615941559557649 Degree/radian conversion: 45.0 degrees = 0.7853981633974483 radians 0.7853981633974483 radians = 45.0 degrees Trig with degrees (convert first): sin(45°): 0.7071067811865475 cos(90°): 6.123233995736766E-17 Practical examples: Right triangle (3, 4, ?): Hypotenuse: 5.0 Angle: 53.13010235415598 degrees Point on circle (r=10, 60°): x: 5.000000000000001 y: 8.660254037844386 From (0,0) to (3,3): Distance: 4.242640687119285 Angle: 45.0 degrees Sine values (0° to 90°, step 15°):rad ← 0.0
pass 1 of 786System.out.println("\nSine values (0° to 90°, step 15°):");87for (int deg0 = 0; deg <= 90; deg += 15) {88 double rad→ 0.0 = Math.toRadians(deg0);89 System.out.printf("%3d° : %.4f%n", deg0, Math.sin(rad0.0));90}All 7 passes — pass 1 is the card above pass degrad1 0 0.0 2 15 0.2617993877991494 3 30 0.5235987755982988 4 45 0.7853981633974483 5 60 1.0471975511965976 6 75 1.3089969389957472 7 90 1.5707963267948966
angle ← 0.7853981633974483, degrees ← 30.0, radians ← 0.5235987755982988
3public class Trigonometry {4 public static void main(String[] args) {5 // Angles in radians6 double angle→ 0.7853981633974483 = Math.PI / 4; // 45 degrees7 8 System.out.println("Angle: " + angle0.7853981633974483 + " radians (45 degrees)");9 System.out.println();1011 // Basic trig functions12 System.out.println("Basic trigonometry:");13 System.out.println("sin(π/4): " + Math.sin(angle0.7853981633974483));14 System.out.println("cos(π/4): " + Math.cos(angle0.7853981633974483));15 System.out.println("tan(π/4): " + Math.tan(angle0.7853981633974483));1617 // Common angles18 System.out.println("\nCommon angles:");19 System.out.println("sin(0): " + Math.sin(0));20 System.out.println("sin(π/2): " + Math.sin(Math.PI / 2)); // 90 degrees21 System.out.println("sin(π): " + Math.sin(Math.PI)); // 180 degrees22 System.out.println("cos(0): " + Math.cos(0));23 System.out.println("cos(π): " + Math.cos(Math.PI));2425 // Inverse trig functions26 System.out.println("\nInverse trigonometry:");27 System.out.println("asin(1): " + Math.asin(1)); // π/228 System.out.println("acos(0): " + Math.acos(0)); // π/229 System.out.println("atan(1): " + Math.atan(1)); // π/430 System.out.println("atan2(1, 1): " + Math.atan2(1, 1)); // π/43132 // Hyperbolic functions33 System.out.println("\nHyperbolic functions:");34 System.out.println("sinh(1): " + Math.sinh(1));35 System.out.println("cosh(1): " + Math.cosh(1));36 System.out.println("tanh(1): " + Math.tanh(1));3738 // Degree/radian conversion39 System.out.println("\nDegree/radian conversion:");40 double degrees→ 30.0 = 30;41 double radians→ 0.5235987755982988 = Math.toRadians(degrees30.0);42 System.out.println(degrees30.0 + " degrees = " + radians0.5235987755982988 + " radians");43 System.out.println(radians0.5235987755982988 + " radians = " + Math.toDegrees(radians) + " degrees");4445 // Trig with degrees46 System.out.println("\nTrig with degrees (convert first):");47 double angle45→ 0.7853981633974483 = Math.toRadians(45);48 double angle90→ 1.5707963267948966 = Math.toRadians(90);49 System.out.println("sin(45°): " + Math.sin(angle450.7853981633974483));50 System.out.println("cos(90°): " + Math.cos(angle901.5707963267948966));5152 // Practical examples53 System.out.println("\nPractical examples:");54 55 // Right triangle56 double adjacent→ 3.0 = 3;57 double opposite→ 4.0 = 4;58 double hypotenuse→ 5.0 = Math.sqrt(Math.pow(adjacent3.0, 2) + Math.pow(opposite4.0, 2));59 double angleRad→ 0.9272952180016122 = Math.atan2(opposite4.0, adjacent3.0);60 double angleDeg→ 53.13010235415598 = Math.toDegrees(angleRad0.9272952180016122);61 62 System.out.println("Right triangle (3, 4, ?):");63 System.out.println("Hypotenuse: " + hypotenuse5.0);64 System.out.println("Angle: " + angleDeg53.13010235415598 + " degrees");6566 // Circle point67 double radius→ 10.0 = 10;68 double angleCircle→ 1.0471975511965976 = Math.toRadians(60);69 double x→ 5.000000000000001 = radius10.0 * Math.cos(angleCircle1.0471975511965976);70 double y→ 8.660254037844386 = radius10.0 * Math.sin(angleCircle1.0471975511965976);71 System.out.println("\nPoint on circle (r=10, 60°):");72 System.out.println("x: " + x5.000000000000001);73 System.out.println("y: " + y8.660254037844386);7475 // Distance and angle between points76 double x1 = 0, y1→ 0.0 = 0;77 double x2 = 3, y2→ 3.0 = 3;78 double distance→ 4.242640687119285 = Math.sqrt(Math.pow(x23.0 - x10.0, 2) + Math.pow(y23.0 - y10.0, 2));79 double angleToPoint→ 45.0 = Math.toDegrees(Math.atan2(y23.0 - y10.0, x23.0 - x10.0));80 81 System.out.println("\nFrom (0,0) to (3,3):");82 System.out.println("Distance: " + distance4.242640687119285);83 System.out.println("Angle: " + angleToPoint45.0 + " degrees");8485 // Table of sine values86 System.out.println("\nSine values (0° to 90°, step 15°):");87 for (int deg = 0; deg <= 90; deg += 15) {outputAngle: 0.7853981633974483 radians (45 degrees) Basic trigonometry: sin(π/4): 0.7071067811865475 cos(π/4): 0.7071067811865476 tan(π/4): 0.9999999999999999 Common angles: sin(0): 0.0 sin(π/2): 1.0 sin(π): 1.2246467991473532E-16 cos(0): 1.0 cos(π): -1.0 Inverse trigonometry: asin(1): 1.5707963267948966 acos(0): 1.5707963267948966 atan(1): 0.7853981633974483 atan2(1, 1): 0.7853981633974483 Hyperbolic functions: sinh(1): 1.1752011936438014 cosh(1): 1.543080634815244 tanh(1): 0.7615941559557649 Degree/radian conversion: 30.0 degrees = 0.5235987755982988 radians 0.5235987755982988 radians = 29.999999999999996 degrees Trig with degrees (convert first): sin(45°): 0.7071067811865475 cos(90°): 6.123233995736766E-17 Practical examples: Right triangle (3, 4, ?): Hypotenuse: 5.0 Angle: 53.13010235415598 degrees Point on circle (r=10, 60°): x: 5.000000000000001 y: 8.660254037844386 From (0,0) to (3,3): Distance: 4.242640687119285 Angle: 45.0 degrees Sine values (0° to 90°, step 15°):rad ← 0.0
pass 1 of 786System.out.println("\nSine values (0° to 90°, step 15°):");87for (int deg0 = 0; deg <= 90; deg += 15) {88 double rad→ 0.0 = Math.toRadians(deg0);89 System.out.printf("%3d° : %.4f%n", deg0, Math.sin(rad0.0));90}All 7 passes — pass 1 is the card above pass degrad1 0 0.0 2 15 0.2617993877991494 3 30 0.5235987755982988 4 45 0.7853981633974483 5 60 1.0471975511965976 6 75 1.3089969389957472 7 90 1.5707963267948966
angle ← 0.7853981633974483, degrees ← 60.0, radians ← 1.0471975511965976
3public class Trigonometry {4 public static void main(String[] args) {5 // Angles in radians6 double angle→ 0.7853981633974483 = Math.PI / 4; // 45 degrees7 8 System.out.println("Angle: " + angle0.7853981633974483 + " radians (45 degrees)");9 System.out.println();1011 // Basic trig functions12 System.out.println("Basic trigonometry:");13 System.out.println("sin(π/4): " + Math.sin(angle0.7853981633974483));14 System.out.println("cos(π/4): " + Math.cos(angle0.7853981633974483));15 System.out.println("tan(π/4): " + Math.tan(angle0.7853981633974483));1617 // Common angles18 System.out.println("\nCommon angles:");19 System.out.println("sin(0): " + Math.sin(0));20 System.out.println("sin(π/2): " + Math.sin(Math.PI / 2)); // 90 degrees21 System.out.println("sin(π): " + Math.sin(Math.PI)); // 180 degrees22 System.out.println("cos(0): " + Math.cos(0));23 System.out.println("cos(π): " + Math.cos(Math.PI));2425 // Inverse trig functions26 System.out.println("\nInverse trigonometry:");27 System.out.println("asin(1): " + Math.asin(1)); // π/228 System.out.println("acos(0): " + Math.acos(0)); // π/229 System.out.println("atan(1): " + Math.atan(1)); // π/430 System.out.println("atan2(1, 1): " + Math.atan2(1, 1)); // π/43132 // Hyperbolic functions33 System.out.println("\nHyperbolic functions:");34 System.out.println("sinh(1): " + Math.sinh(1));35 System.out.println("cosh(1): " + Math.cosh(1));36 System.out.println("tanh(1): " + Math.tanh(1));3738 // Degree/radian conversion39 System.out.println("\nDegree/radian conversion:");40 double degrees→ 60.0 = 60;41 double radians→ 1.0471975511965976 = Math.toRadians(degrees60.0);42 System.out.println(degrees60.0 + " degrees = " + radians1.0471975511965976 + " radians");43 System.out.println(radians1.0471975511965976 + " radians = " + Math.toDegrees(radians) + " degrees");4445 // Trig with degrees46 System.out.println("\nTrig with degrees (convert first):");47 double angle45→ 0.7853981633974483 = Math.toRadians(45);48 double angle90→ 1.5707963267948966 = Math.toRadians(90);49 System.out.println("sin(45°): " + Math.sin(angle450.7853981633974483));50 System.out.println("cos(90°): " + Math.cos(angle901.5707963267948966));5152 // Practical examples53 System.out.println("\nPractical examples:");54 55 // Right triangle56 double adjacent→ 3.0 = 3;57 double opposite→ 4.0 = 4;58 double hypotenuse→ 5.0 = Math.sqrt(Math.pow(adjacent3.0, 2) + Math.pow(opposite4.0, 2));59 double angleRad→ 0.9272952180016122 = Math.atan2(opposite4.0, adjacent3.0);60 double angleDeg→ 53.13010235415598 = Math.toDegrees(angleRad0.9272952180016122);61 62 System.out.println("Right triangle (3, 4, ?):");63 System.out.println("Hypotenuse: " + hypotenuse5.0);64 System.out.println("Angle: " + angleDeg53.13010235415598 + " degrees");6566 // Circle point67 double radius→ 10.0 = 10;68 double angleCircle→ 1.0471975511965976 = Math.toRadians(60);69 double x→ 5.000000000000001 = radius10.0 * Math.cos(angleCircle1.0471975511965976);70 double y→ 8.660254037844386 = radius10.0 * Math.sin(angleCircle1.0471975511965976);71 System.out.println("\nPoint on circle (r=10, 60°):");72 System.out.println("x: " + x5.000000000000001);73 System.out.println("y: " + y8.660254037844386);7475 // Distance and angle between points76 double x1 = 0, y1→ 0.0 = 0;77 double x2 = 3, y2→ 3.0 = 3;78 double distance→ 4.242640687119285 = Math.sqrt(Math.pow(x23.0 - x10.0, 2) + Math.pow(y23.0 - y10.0, 2));79 double angleToPoint→ 45.0 = Math.toDegrees(Math.atan2(y23.0 - y10.0, x23.0 - x10.0));80 81 System.out.println("\nFrom (0,0) to (3,3):");82 System.out.println("Distance: " + distance4.242640687119285);83 System.out.println("Angle: " + angleToPoint45.0 + " degrees");8485 // Table of sine values86 System.out.println("\nSine values (0° to 90°, step 15°):");87 for (int deg = 0; deg <= 90; deg += 15) {outputAngle: 0.7853981633974483 radians (45 degrees) Basic trigonometry: sin(π/4): 0.7071067811865475 cos(π/4): 0.7071067811865476 tan(π/4): 0.9999999999999999 Common angles: sin(0): 0.0 sin(π/2): 1.0 sin(π): 1.2246467991473532E-16 cos(0): 1.0 cos(π): -1.0 Inverse trigonometry: asin(1): 1.5707963267948966 acos(0): 1.5707963267948966 atan(1): 0.7853981633974483 atan2(1, 1): 0.7853981633974483 Hyperbolic functions: sinh(1): 1.1752011936438014 cosh(1): 1.543080634815244 tanh(1): 0.7615941559557649 Degree/radian conversion: 60.0 degrees = 1.0471975511965976 radians 1.0471975511965976 radians = 59.99999999999999 degrees Trig with degrees (convert first): sin(45°): 0.7071067811865475 cos(90°): 6.123233995736766E-17 Practical examples: Right triangle (3, 4, ?): Hypotenuse: 5.0 Angle: 53.13010235415598 degrees Point on circle (r=10, 60°): x: 5.000000000000001 y: 8.660254037844386 From (0,0) to (3,3): Distance: 4.242640687119285 Angle: 45.0 degrees Sine values (0° to 90°, step 15°):rad ← 0.0
pass 1 of 786System.out.println("\nSine values (0° to 90°, step 15°):");87for (int deg0 = 0; deg <= 90; deg += 15) {88 double rad→ 0.0 = Math.toRadians(deg0);89 System.out.printf("%3d° : %.4f%n", deg0, Math.sin(rad0.0));90}All 7 passes — pass 1 is the card above pass degrad1 0 0.0 2 15 0.2617993877991494 3 30 0.5235987755982988 4 45 0.7853981633974483 5 60 1.0471975511965976 6 75 1.3089969389957472 7 90 1.5707963267948966
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
Replay: real traced execution (multi-file project)
// 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
}
x ← 1.0, val ← 1.0, a ← 1.0E200, b ← 1.0E200, hypot ← 1.414213562373095E200
3public class Special {4 public static void main(String[] args) {5 // Hypotenuse6 System.out.println("Hypotenuse:");7 System.out.println("hypot(3, 4): " + Math.hypot(3, 4));8 System.out.println("hypot(5, 12): " + Math.hypot(5, 12));910 // Expm1 (exp(x) - 1) - accurate for small x11 System.out.println("\nExpm1 (e^x - 1):");12 System.out.println("expm1(0): " + Math.expm1(0));13 System.out.println("expm1(1): " + Math.expm1(1));14 System.out.println("expm1(0.001): " + Math.expm1(0.001));1516 // Log1p (log(1 + x)) - accurate for small x17 System.out.println("\nLog1p (ln(1 + x)):");18 System.out.println("log1p(0): " + Math.log1p(0));19 System.out.println("log1p(1): " + Math.log1p(1));20 System.out.println("log1p(0.001): " + Math.log1p(0.001));2122 // CopySign23 System.out.println("\nCopySign (magnitude of first, sign of second):");24 System.out.println("copySign(5, -1): " + Math.copySign(5, -1)); // -5.025 System.out.println("copySign(-5, 1): " + Math.copySign(-5, 1)); // 5.026 System.out.println("copySign(3.14, -2): " + Math.copySign(3.14, -2));2728 // NextAfter29 System.out.println("\nNextAfter (next floating-point value):");30 double x→ 1.0 = 1.0;31 System.out.println("nextAfter(1.0, 2.0): " + Math.nextAfter(x1.0, 2.0));32 System.out.println("nextAfter(1.0, 0.0): " + Math.nextAfter(x1.0, 0.0));3334 // Ulp (unit in last place)35 System.out.println("\nUlp (spacing to next value):");36 System.out.println("ulp(1.0): " + Math.ulp(1.0));37 System.out.println("ulp(10.0): " + Math.ulp(10.0));38 System.out.println("ulp(100.0): " + Math.ulp(100.0));3940 // Get exponent41 System.out.println("\nGet exponent:");42 System.out.println("getExponent(8.0): " + Math.getExponent(8.0)); // 3 (2^3 = 8)43 System.out.println("getExponent(16.0): " + Math.getExponent(16.0)); // 444 System.out.println("getExponent(0.5): " + Math.getExponent(0.5)); // -14546 // Scalb (multiply by 2^n)47 System.out.println("\nScalb (multiply by 2^n):");48 System.out.println("scalb(3.0, 2): " + Math.scalb(3.0, 2)); // 3 * 2^2 = 1249 System.out.println("scalb(5.0, 3): " + Math.scalb(5.0, 3)); // 5 * 2^3 = 4050 System.out.println("scalb(1.0, -2): " + Math.scalb(1.0, -2)); // 1 * 2^-2 = 0.255152 // FMA (fused multiply-add) - a*b + c53 System.out.println("\nFMA (fused multiply-add):");54 System.out.println("fma(2, 3, 5): " + Math.fma(2, 3, 5)); // 2*3 + 5 = 1155 System.out.println("fma(1.5, 2, 0.5): " + Math.fma(1.5, 2, 0.5)); // 3.55657 // NextUp and NextDown58 System.out.println("\nNextUp and NextDown:");59 double val→ 1.0 = 1.0;60 System.out.println("nextUp(1.0): " + Math.nextUp(val1.0));61 System.out.println("nextDown(1.0): " + Math.nextDown(val1.0));6263 // Special values64 System.out.println("\nSpecial values:");65 System.out.println("Infinity: " + Double.POSITIVE_INFINITY);66 System.out.println("NaN: " + Double.NaN);67 System.out.println("Max double: " + Double.MAX_VALUE);68 System.out.println("Min double: " + Double.MIN_VALUE);6970 // Check special values71 System.out.println("\nCheck special values:");72 System.out.println("isNaN(0/0): " + Double.isNaN(0.0 / 0.0));73 System.out.println("isInfinite(1/0): " + Double.isInfinite(1.0 / 0.0));74 System.out.println("isFinite(100): " + Double.isFinite(100.0));7576 // Practical examples77 System.out.println("\nPractical examples:");78 79 // Calculate hypotenuse without overflow80 double a→ 1.0E200 = 1e200;81 double b→ 1.0E200 = 1e200;82 double hypot→ 1.414213562373095E200 = Math.hypot(a1.0E200, b1.0E200);83 System.out.println("Hypotenuse of very large sides: " + hypot1.414213562373095E200);8485 // Accurate small calculations86 double small→ 1.0E-4 = 0.0001;87 double expResult→ 1.0000500016667084E-4 = Math.expm1(small1.0E-4);88 double logResult→ 9.999500033330834E-5 = Math.log1p(small1.0E-4);89 System.out.println("expm1(" + small1.0E-4 + "): " + expResult1.0000500016667084E-4);90 System.out.println("log1p(" + small1.0E-4 + "): " + logResult9.999500033330834E-5);9192 // Apply sign93 double magnitude→ 42.0 = 42;94 double sign→ -1.0 = -1;95 double result→ -42.0 = Math.copySign(magnitude42.0, sign-1.0);96 System.out.println("Apply sign of " + sign-1.0 + " to " + magnitude42.0 + ": " + result-42.0);97 }outputHypotenuse: hypot(3, 4): 5.0 hypot(5, 12): 13.0 Expm1 (e^x - 1): expm1(0): 0.0 expm1(1): 1.718281828459045 expm1(0.001): 0.0010005001667083417 Log1p (ln(1 + x)): log1p(0): 0.0 log1p(1): 0.6931471805599453 log1p(0.001): 9.995003330835331E-4 CopySign (magnitude of first, sign of second): copySign(5, -1): -5.0 copySign(-5, 1): 5.0 copySign(3.14, -2): -3.14 NextAfter (next floating-point value): nextAfter(1.0, 2.0): 1.0000000000000002 nextAfter(1.0, 0.0): 0.9999999999999999 Ulp (spacing to next value): ulp(1.0): 2.220446049250313E-16 ulp(10.0): 1.7763568394002505E-15 ulp(100.0): 1.4210854715202004E-14 Get exponent: getExponent(8.0): 3 getExponent(16.0): 4 getExponent(0.5): -1 Scalb (multiply by 2^n): scalb(3.0, 2): 12.0 scalb(5.0, 3): 40.0 scalb(1.0, -2): 0.25 FMA (fused multiply-add): fma(2, 3, 5): 11.0 fma(1.5, 2, 0.5): 3.5 NextUp and NextDown: nextUp(1.0): 1.0000000000000002 nextDown(1.0): 0.9999999999999999 Special values: Infinity: Infinity NaN: NaN Max double: 1.7976931348623157E308 Min double: 4.9E-324 Check special values: isNaN(0/0): true isInfinite(1/0): true isFinite(100): true Practical examples: Hypotenuse of very large sides: 1.414213562373095E200 expm1(1.0E-4): 1.0000500016667084E-4 log1p(1.0E-4): 9.999500033330834E-5 Apply sign of -1.0 to 42.0: -42.0
@seealso biginteger_intro, random_intro
Exercise: Practical.java
Calculate the distance between two points and the area of a circle