-1

I have no idea what I'm doing wrong here. My math is off but by the smallest ammount.

I tried changing the DecimalFormat but I know its correct at where it's set now. I looked at using long but I don't see why that would make a difference I'm not going past the size of double.

I made sure to check that I was working with all doubles. I'm not even sure exactly how to troubleshoot this.

Input data:

12
657 7
9079 13
9551 8
4633 7
8622 12
98 2
254 2
12040 11
14711 10
46254 8
8978 
62424 2

Expected result:

25.6343560524 95.2837866586 98.7716099678 71.3069977502 92.8547252433 25.7398989899 64.7460784314 109.726933795 121.288922541 258.904619449 566.427585376 15607.249984

My result:

25.6343560524 95.2837866586 98.7716099678 71.3069977502 92.8547252433 25.7398989899 64.7460784314 109.7269337948 121.2889225409 258.9046194492 566.4275853759 15607.2499839808

My code:

package com.secryption.CA18SquareRoot;

import java.text.DecimalFormat;
import java.util.Scanner;


public class SquareRoot {
    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        System.out.println("Enter Data: ");
        int numberOfTestCases = myScanner.nextInt();
        double r;
        DecimalFormat df = new DecimalFormat("##.##########");


        for (int i = 0; i < numberOfTestCases; i++) {
            r = 1.0;
            int calculateSquareRootFor = myScanner.nextInt();
            int numberOfSteps = myScanner.nextInt();
            double d;

            if (numberOfSteps > 0) {
                for (int w = 0; w < numberOfSteps; w++) {
                    d = (double) calculateSquareRootFor / r;
                    r = (r + d) / 2.0;
                }
            }
            System.out.print((df.format(r) + " "));
        }
    }
}

EDIT: After talking to the site owner, double is correct. I ended up trying a few different data sets and it finally saw it as correct.

TheEditor
  • 486
  • 1
  • 4
  • 18

1 Answers1

1

Not all values can be precisely represented by a double. Values that can't will be approximated, which is what you're experiencing here.

For example, the value 1/3 cannot be written as a finite decimal number: it would be infinitely long, 0.333333..., but it could be written as a finite number in a ternary system, for example, where it would be 0.1, or approximated in the decimal system as, e.g. 0.333334 (which is just a little off).

In analogy, there are many values that cannot be represented exactly as a binary number, which is what double uses internally. For example, it is impossible to store 0.1 as a binary number, the exact representation would be infinitely long and therefore a double will use an approximation.

Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
  • I understand the issues with binary/decimal math. Yet this problem has been solved previously by others. ( I'm working through the codeabbey exercises.) So then I can assume my code is correct, that's more what I'm worried about. – TheEditor Nov 06 '15 at 00:05