|
working with decimal point numbers
|
Posted: Dec 9, 2013 10:05 PM
|
|
Advertisement
|
Hello Artima; Good Morning
Here I come up with a code that donot produce an accurate result // my program goes like this
public class FloatingNumber {
public static void main(String...args) {
double i; double num=0.0; for(i=0.0;i<10;i++) num=num+0.1;
System.out.println(num);
}
} // output: 0.9999999999999999
the same code writen in C produced an Accurate result // the C program goes like this
#include<stdio.h> #include<math.h>
main()
{ double i, num=0.0;
for(i=0;i<10;i++) num=num+0.1; printf("%f",num);
} // output: 1.0000
/** please explain me the difference and suggest me a bettrcode to obtain an accurate result through Java Thanks in advance.
|
|