What's with the rounding??
Hi Folks,
Well here's a program i wrote to check out how the Math.floor() and Math.ceil() works. I kinda grasped the idea of these two methods. However, hear me out...
/*************
This program is written with the spcific intention of redirection to a file instead of output to console
***************/
public class Whatzit
{
public static void main(String[] args)
{
double i = 0.0;
for(; i < 11.0; i+=0.1)
{
double ceil = Math.ceil(i);
System.out.println("\ni = " + i + "\t\t ceil = " + ceil);
double floor = Math.floor(i);
System.out.println("i = " + i + "\t\t floor = " + floor);
for(int count = 0; count < 51; count++)
{
System.out.print("-"); // make some sense by drawing a line
} //end of for count loop
} // end for i
} // end of main()
} // end of prog.
after compiling i execute as follows:
c:\$JAVAHOME$\SOURCE>java -ea Whatzit >> Whatzit
the output now gets redirected to a file called Whatzit which i open using MS Word 2000 in UTF-8 format.
here's a portion of the output
i = 0.0 ceil = 0.0
i = 0.0 floor = 0.0
---------------------------------------------------
i = 0.1 ceil = 1.0
i = 0.1 floor = 0.0
---------------------------------------------------
i = 0.2 ceil = 1.0
i = 0.2 floor = 0.0
---------------------------------------------------
i = 0.30000000000000004 ceil = 1.0
i = 0.30000000000000004 floor = 0.0
---------------------------------------------------
i = 0.4 ceil = 1.0
i = 0.4 floor = 0.0
---------------------------------------------------
i = 0.5 ceil = 1.0
i = 0.5 floor = 0.0
---------------------------------------------------
i = 0.6 ceil = 1.0
i = 0.6 floor = 0.0
---------------------------------------------------
i = 0.7 ceil = 1.0
i = 0.7 floor = 0.0
---------------------------------------------------
i = 0.7999999999999999 ceil = 1.0
i = 0.7999999999999999 floor = 0.0
---------------------------------------------------
i = 0.8999999999999999 ceil = 1.0
i = 0.8999999999999999 floor = 0.0
---SNIP--- (file deliberately truncated by kaizer)
My qustion is this...
even though i've instructed to specifically increment i in steps of 0.1 WHY DOES THE VARIABLE I GET ROUNDED UP TO 0.4 FROM 0.30000000000000004? (See above). Infact, it happens again from 0.7999999999999999 and jumps to 0.8999999999999999!!
Is this an inherant anomaly with the SDK or is there something i'm missing here?
Thanks to y'all
And thanks for reading patiently!!
Have a nice day,
Kaiz.
Kaizer Billimoria
Bangalore, India
__________________
Kaizer Billimoria
Bangalore, India
|