Skip to content

refactor: RootPrecision #5383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 13 additions & 30 deletions src/main/java/com/thealgorithms/others/RootPrecision.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,21 @@
package com.thealgorithms.others;

import java.util.Scanner;

public final class RootPrecision {
private RootPrecision() {
}

public static void main(String[] args) {
// take input
Scanner scn = new Scanner(System.in);

// n is the input number
int n = scn.nextInt();

// p is precision value for eg - p is 3 in 2.564 and 5 in 3.80870.
int p = scn.nextInt();
System.out.println(squareRoot(n, p));

scn.close();
}

public static double squareRoot(int n, int p) {
// rv means return value
double rv;

double root = Math.pow(n, 0.5);

// calculate precision to power of 10 and then multiply it with root value.
int precision = (int) Math.pow(10, p);
root = root * precision;
/*typecast it into integer then divide by precision and again typecast into double
so as to have decimal points upto p precision */

rv = (int) root;
return rv / precision;
/**
* Calculates the square root of a number with the specified precision.
*
* @param number The number to calculate the square root of.
* @param precision The number of decimal places of precision.
* @return The square root of the number with the specified precision.
*/
public static double calculateSquareRoot(int number, int precision) {
double rawRoot = Math.sqrt(number); // Calculate the square root using Math.sqrt
double scalingFactor = Math.pow(10, precision); // Calculate the scaling factor for precision

// Scale the square root, truncate the extra decimals, and rescale back
return Math.round(rawRoot * scalingFactor) / scalingFactor;
}
}
33 changes: 33 additions & 0 deletions src/test/java/com/thealgorithms/others/RootPrecisionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.thealgorithms.others;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class RootPrecisionTest {

@Test
public void testSquareRootWithZeroPrecision() {
assertEquals(2.0, RootPrecision.calculateSquareRoot(4, 0));
assertEquals(3.0, RootPrecision.calculateSquareRoot(9, 0));
assertEquals(5.0, RootPrecision.calculateSquareRoot(25, 0));
}

@Test
public void testSquareRootWithPrecision() {
assertEquals(1.414, RootPrecision.calculateSquareRoot(2, 3));
assertEquals(3.162, RootPrecision.calculateSquareRoot(10, 3));
assertEquals(5.000, RootPrecision.calculateSquareRoot(25, 3));
}

@Test
public void testSquareRootWithHighPrecision() {
assertEquals(1.41421, RootPrecision.calculateSquareRoot(2, 5));
assertEquals(3.16228, RootPrecision.calculateSquareRoot(10, 5));
}

@Test
public void testSquareRootOfZero() {
assertEquals(0.0, RootPrecision.calculateSquareRoot(0, 3));
}
}