-1
import java.util.*;
public class test {
    public static void main(String [] args) {
        Scanner scn = new Scanner(System.in);
        double s = 253.3;
        while(s > 9) {
            s = s/10;
        }
        System.out.println(s);
    }
}

Why the output is 2.5330000000000004 and not 2.533?

  • That kind of question is related to Numerical analysis which is studied by those who learn Informational Technology, Mathematics etc... Explaining that here I think is overkill. I suggest searching for that kind of books and reading them if you are interested! Even the answer from the provided link I think is not enough. I've spent one year at university learning such things :) – XMight Oct 01 '14 at 14:59

1 Answers1

2

Because floating point values are an approximation.

http://en.wikipedia.org/wiki/Floating_point

khelwood
  • 55,782
  • 14
  • 81
  • 108