|
|
Re: What is the meaning of life?
|
Posted: Jan 15, 2012 1:18 PM
|
|
Further experimenting;
public class testj { public static void main(String argv[]) { System.out.format("%20s %10s %20s %5s %5s\n", "i", "subtract", "nines", "sum0", "sum1"); long i = 1; for(i = 1 ; i < 1000000000000000000L ; i *= 10) { double subtractfrom1 = (double)1 / (double) i; double nines = 1 - subtractfrom1; int sum0 = 1; int sum1 = 1; sum0 += nines; sum1 = sum1 + (int)nines; System.out.format("%20s %10s %20s %5s %5s\n", i, subtractfrom1, nines, sum0, sum1); }
} }
$ javac testj.java $ java testj i subtract nines sum0 sum1 1 1.0 0.0 1 1 10 0.1 0.9 1 1 100 0.01 0.99 1 1 1000 0.0010 0.999 1 1 10000 1.0E-4 0.9999 1 1 100000 1.0E-5 0.99999 1 1 1000000 1.0E-6 0.999999 1 1 10000000 1.0E-7 0.9999999 1 1 100000000 1.0E-8 0.99999999 1 1 1000000000 1.0E-9 0.999999999 1 1 10000000000 1.0E-10 0.9999999999 1 1 100000000000 1.0E-11 0.99999999999 1 1 1000000000000 1.0E-12 0.999999999999 1 1 10000000000000 1.0E-13 0.9999999999999 1 1 100000000000000 1.0E-14 0.99999999999999 1 1 1000000000000000 1.0E-15 0.999999999999999 1 1 10000000000000000 1.0E-16 0.9999999999999999 2 1 100000000000000000 1.0E-17 1.0 2 2 $
** sorry, neither pre nor code nor java in square bracks will preserve spacing **
It appears there is a difference between += and +(int).
As another poster suggested it appears that when the floating point rep is maxed out it is "identical" to 1. Is this what's actually going on?
|
|