The cast technique is much better than it may seem to be at first sight.
Conversion from int to double is exact.
Math.sqrt is specified, for normal positive numbers, to return "the double value closest to the true mathematical square root of the argument value". If the input was a perfect square int, the result will be an integer-valued double that is in the int range.
Similarly, conversion of that integer-valued double back to an int is also exact.
The net effect is that the cast technique does not introduce any rounding error in your situation.
If your program would otherwise benefit from use of the Guava intMath API, you can use that to do the square root, but I would not add dependency on an API just to avoid the cast.
Answer from Patricia Shanahan on Stack OverflowJava Square Root Integer Operations Without Casting? - Stack Overflow
Java square root calculator? - Stack Overflow
java - Utility Method to find the Square root of a number - Code Review Stack Exchange
algorithm - How do I compute the square root of a number without using builtins? - Stack Overflow
Videos
The cast technique is much better than it may seem to be at first sight.
Conversion from int to double is exact.
Math.sqrt is specified, for normal positive numbers, to return "the double value closest to the true mathematical square root of the argument value". If the input was a perfect square int, the result will be an integer-valued double that is in the int range.
Similarly, conversion of that integer-valued double back to an int is also exact.
The net effect is that the cast technique does not introduce any rounding error in your situation.
If your program would otherwise benefit from use of the Guava intMath API, you can use that to do the square root, but I would not add dependency on an API just to avoid the cast.
You can always use the Google Guava IntMath API.
final int sqrtX = IntMath.sqrt(x, RoundingMode.HALF_EVEN);
You have a couple of problems here:
1) Logical bug: 0 == 0 * 0
<= This means while (dummy != dummy * dummy) {..} will never be untrue, and you'll never even enter the loop
2) Floating point numbers are inexact, so your algorithm (which relies on "==") might not work anyway
Look here for more details on floating point imprecision:
http://www.lahey.com/float.htm
This is true for ANY language - your algorithm for square root must take this into account.
Try to use this algorithm which use Newton's iteration:
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
double number, t, squareRoot;
Scanner input = new Scanner(System.in);
number = input.nextDouble();
squareRoot = number / 2;
do
{
t = squareRoot;
squareRoot = (t + (number / t)) / 2;
}
while ((t - squareRoot) != 0);
System.out.println(squareRoot);
}
}
Newton's iteration is an algorithm for computing the square root of a number via the recurrence equation: X(n+1) = (X(n) + number/X(n))/2
Java program to find out square root of a given number without using any Built-In Functions
public class Sqrt
{
public static void main(String[] args)
{
//Number for which square root is to be found
double number = Double.parseDouble(args[0]);
//This method finds out the square root
findSquareRoot(number);
}
/*This method finds out the square root without using
any built-in functions and displays it */
public static void findSquareRoot(double number)
{
boolean isPositiveNumber = true;
double g1;
//if the number given is a 0
if(number==0)
{
System.out.println("Square root of "+number+" = "+0);
}
//If the number given is a -ve number
else if(number<0)
{
number=-number;
isPositiveNumber = false;
}
//Proceeding to find out square root of the number
double squareRoot = number/2;
do
{
g1=squareRoot;
squareRoot = (g1 + (number/g1))/2;
}
while((g1-squareRoot)!=0);
//Displays square root in the case of a positive number
if(isPositiveNumber)
{
System.out.println("Square roots of "+number+" are ");
System.out.println("+"+squareRoot);
System.out.println("-"+squareRoot);
}
//Displays square root in the case of a -ve number
else
{
System.out.println("Square roots of -"+number+" are ");
System.out.println("+"+squareRoot+" i");
System.out.println("-"+squareRoot+" i");
}
}
}
You will probably have to make use of some approximation method.
Have a look at
Methods of computing square roots
I'm a beginner and came to calculating square root of a number part. Lesson says I need to use double with Math.sqrtint number = 42;double squareRoot = Math.sqrt(number);
and I was curious why we can't use int or float. I searched on google but couldn't find an answer.
Simply
System.out.println("Square Root: \u221A");
Source: first match on Google.
Here's the unicode number for it: http://www.fileformat.info/info/unicode/char/221a/index.htm
And this prints it out to a file for me:
package com.sandbox;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
public class Sandbox {
public static void main(String[] args) throws ParseException, IOException {
FileUtils.write(new File("out.txt"), "\u221A", "UTF8");
}
}
When I open that file, it has the square root symbol in it.