Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2
|
BOOK: Beginning Java 2
This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 20th, 2003, 03:49 AM
Authorized User
 
Join Date: Dec 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to kaizer Send a message via Yahoo to kaizer
Default 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
 
Old December 22nd, 2003, 07:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yeah, this is really confusing isn't it. The problem here, which exists in languages besides java, is that the results produced when using double and float are not always precise. If you have a look at IEEE specification 754, you will see that it states:

//quote

Why is 0.1 not 0.1?

Binary floating-point numbers consist of signed integers multiplied by powers of two. When fractional, you can also consider them as integers divided by powers of two.The decimal number 0.1, or 1/10, is not an integer over a power of two.

//end of quote

Often this isn't a problem, however if you are dealing with more precise calculations then this behaviour is not acceptable. Let me explain using this example that I've copied from the java.sun.com web site:

double f = 2.9 - 1.1;
System.out.println(f);

It outputs 1.799999999999998 rather than 1.8

To get around this problem you could always use java.math.BigDecimal.


import java.math.*;

class BigDecimalTest
{
    public static void main(String[] args)
    {
        BigDecimal a = new BigDecimal("2.9");
        BigDecimal b = new BigDecimal("1.1");
        BigDecimal c =a.subtract(b);
        System.out.println(c);
    }
}


This outputs 1.8


I hope this helps.

Cheers

Martyn
 
Old December 22nd, 2003, 11:36 PM
Authorized User
 
Join Date: Dec 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to kaizer Send a message via Yahoo to kaizer
Default

Hi Martyn,

Thanks for the insight!

Kaizer.

Kaizer Billimoria
Bangalore, India





Similar Threads
Thread Thread Starter Forum Replies Last Post
Rounding problem rajanikrishna Classic ASP Basics 1 August 17th, 2006 12:14 AM
Rounding JasperGIS Beginning VB 6 1 December 14th, 2005 03:09 PM
Rounding kilika SQL Server 2000 4 June 1st, 2005 03:36 PM
Rounding in C# cjo ASP.NET 1.0 and 1.1 Basics 3 November 3rd, 2003 04:12 PM
Rounding Droopy Classic ASP Basics 3 August 14th, 2003 08:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.