diff --git a/content/java/concepts/math-methods/terms/pow/pow.md b/content/java/concepts/math-methods/terms/pow/pow.md index d71cc939390..ac9db5947eb 100644 --- a/content/java/concepts/math-methods/terms/pow/pow.md +++ b/content/java/concepts/math-methods/terms/pow/pow.md @@ -1,18 +1,22 @@ --- Title: '.pow()' -Description: 'Returns the first argument raised to the power of the second argument.' +Description: 'Calculates the value of a base number raised to the power of an exponent.' Subjects: - 'Computer Science' + - 'Web Development' Tags: + - 'Arithmetic' - 'Functions' + - 'Math' - 'Methods' - - 'Arithmetic' CatalogContent: - 'learn-java' - 'paths/computer-science' --- -The **`Math.pow()`** method returns a double value of the first argument raised to the power of the second argument. +The **`Math.pow()`** method in Java is a static method that calculates the value of a base number raised to the power of an exponent. It performs exponentiation operations by taking two double parameters and returning the result as a double value. This method is part of the `java.lang.Math` class and provides a convenient way to compute mathematical powers without manually implementing multiplication loops. + +The `Math.pow()` method is widely used in mathematical computations, scientific calculations, financial modeling, geometric calculations, and algorithmic problems. It's particularly useful in scenarios involving compound interest calculations, area and volume computations, statistical analysis, and any application requiring exponential growth or decay calculations. ## Syntax @@ -20,43 +24,167 @@ The **`Math.pow()`** method returns a double value of the first argument raised Math.pow(base, exponent) ``` -The `base` and `exponent` arguments are both `double` values. +**Parameters:** -It's important to note that edge cases with special arguments such as `0.0`, `infinity`, or `NaN` will produce special results as shown in the example below. +- `base`: The base number to be raised to a power (can be any double value) +- `exponent`: The power to which the base number is raised (can be any double value, including negative and fractional values) -## Example +**Return value:** -The following example uses `Math.pow()` to return `3.0` raised to the power of `2.0`: +The method returns a double value representing the result of base raised to the power of exponent. Special cases include returning `NaN` for invalid operations, `Infinity` for overflow conditions, and specific handling for zero and one values. + +## Example 1: Basic Power Calculation + +This example demonstrates the fundamental usage of the `Math.pow()` method with basic integer exponents: ```java -public class Main { +public class BasicPowerExample { public static void main(String[] args) { + // Calculate 2 raised to the power of 3 + double result1 = Math.pow(2, 3); + System.out.println("2^3 = " + result1); + + // Calculate 5 raised to the power of 4 + double result2 = Math.pow(5, 4); + System.out.println("5^4 = " + result2); - double base = 3.0; - double exponent = 2.0; + // Calculate 10 raised to the power of 2 + double result3 = Math.pow(10, 2); + System.out.println("10^2 = " + result3); - System.out.println(Math.pow(base, exponent)); + // Calculate negative base with even exponent + double result4 = Math.pow(-3, 2); + System.out.println("(-3)^2 = " + result4); + } +} +``` - System.out.println(Math.pow(base, 0.0)); +This example results in the following output: + +```shell +2^3 = 8.0 +5^4 = 625.0 +10^2 = 100.0 +(-3)^2 = 9.0 +``` - System.out.println(Math.pow(0.0, exponent)); +The code demonstrates how `Math.pow()` handles various combinations of base and exponent values. Note that all results are returned as double values, even when the mathematical result would be a whole number. - double notANumber = Double.NaN; - double negInfinity = Double.NEGATIVE_INFINITY; +## Example 2: Compound Interest Calculator - System.out.println(Math.pow(notANumber, exponent)); +This example shows how to use `Math.pow()` in a real-world financial scenario to calculate compound interest. - System.out.println(Math.pow(0.0, negInfinity)); +```java +import java.util.Scanner; + +public class CompoundInterestCalculator { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // Get input from user + System.out.print("Enter principal amount: $"); + double principal = scanner.nextDouble(); + + System.out.print("Enter annual interest rate (%): "); + double rate = scanner.nextDouble() / 100; // Convert percentage to decimal + + System.out.print("Enter number of years: "); + int years = scanner.nextInt(); + + System.out.print("Enter compound frequency per year: "); + int frequency = scanner.nextInt(); + + // Calculate compound interest using Math.pow() + // Formula: A = P(1 + r/n)^(nt) + double amount = principal * Math.pow(1 + (rate / frequency), frequency * years); + double interest = amount - principal; + + System.out.println("\nCompound Interest Calculation:"); + System.out.printf("Principal Amount: $%.2f%n", principal); + System.out.printf("Final Amount: $%.2f%n", amount); + System.out.printf("Total Interest Earned: $%.2f%n", interest); + + scanner.close(); } } ``` -This will produce the following output: +This example results in the following output (with sample input): ```shell -9.0 -1.0 -0.0 -NaN -Infinity +Enter principal amount: $1000 +Enter annual interest rate (%): 5 +Enter number of years: 3 +Enter compound frequency per year: 4 + +Compound Interest Calculation: +Principal Amount: $1000.00 +Final Amount: $1161.62 +Total Interest Earned: $161.62 ``` + +This practical example demonstrates how `Math.pow()` is essential for financial calculations. The compound interest formula requires raising (1 + r/n) to the power of (n\*t), which is efficiently handled by the `Math.pow()` method. + +## Example 3: Distance and Physics Calculations + +This example illustrates using `Math.pow()` for physics calculations, specifically computing distances and areas in geometric problems. + +```java +public class PhysicsCalculations { + public static void main(String[] args) { + // Calculate distance between two points using distance formula + // Distance = sqrt((x2-x1)^2 + (y2-y1)^2) + double x1 = 3, y1 = 4; + double x2 = 7, y2 = 1; + + double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); + System.out.printf("Distance between points: %.2f units%n", distance); + + // Calculate area of a circle using A = π * r^2 + double radius = 5.5; + double circleArea = Math.PI * Math.pow(radius, 2); + System.out.printf("Area of circle with radius %.1f: %.2f square units%n", + radius, circleArea); + + // Calculate volume of a sphere using V = (4/3) * π * r^3 + double sphereVolume = (4.0/3.0) * Math.PI * Math.pow(radius, 3); + System.out.printf("Volume of sphere with radius %.1f: %.2f cubic units%n", + radius, sphereVolume); + + // Calculate kinetic energy using KE = 0.5 * m * v^2 + double mass = 10; // kg + double velocity = 25; // m/s + double kineticEnergy = 0.5 * mass * Math.pow(velocity, 2); + System.out.printf("Kinetic energy: %.2f Joules%n", kineticEnergy); + } +} +``` + +This example results in the following output: + +```shell +Distance between points: 5.00 units +Area of circle with radius 5.5: 95.03 square units +Volume of sphere with radius 5.5: 696.91 cubic units +Kinetic energy: 3125.00 Joules +``` + +This example showcases the versatility of `Math.pow()` in scientific calculations. From basic geometric formulas to physics equations, the method provides accurate exponentiation for various mathematical models commonly used in engineering and scientific applications. + +## Frequently Asked Questions + +### 1. Can `Math.pow()` handle negative exponents? + +Yes, `Math.pow()` can handle negative exponents. A negative exponent represents the reciprocal of the positive exponent. For example, `Math.pow(2, -3)` returns 0.125, which is equivalent to 1/(2^3). + +### 2. What happens when I use fractional exponents with `Math.pow()`? + +Fractional exponents work perfectly with `Math.pow()`. For example, `Math.pow(9, 0.5)` returns 3.0, which is the square root of 9. This makes the method useful for calculating roots and other fractional powers. + +### 3. How does `Math.pow()` handle special cases like `NaN` or `Infinity`? + +The method follows IEEE floating-point standards. It returns `NaN` when the result is undefined (like `Math.pow(-1, 0.5)`), and `Infinity` for overflow conditions (like `Math.pow(10, 1000)`). + +### 4. What's the difference between `Math.pow()` and using the `^` operator? + +Java doesn't have a built-in exponentiation operator (`^` is the XOR operator). `Math.pow()` is the standard way to perform exponentiation in Java, providing accurate results for both integer and floating-point operations.