Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Casting Theory

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Casting Theory

    I came across something interesting in my book and I'm unsure as to why this is happening. Here is some sample code:
    public class DividingWithDifferentTypes {
    	public static void main(String args[]) {
    		int iVar = 15;
    		long lVar = 2;
    		float fVar = 7.6f - iVar / lVar;
    		double dVar = 1L / lVar + fVar / lVar;
    		int result = (int) (100 * dVar);
    		System.out.println(iVar / lVar);
    		System.out.println(7.6f);
    		System.out.println((float) (iVar / lVar));
    		System.out.println(7.6f - (float) (iVar / lVar));
    		System.out.println(1L / lVar);
    		System.out.println(fVar / lVar);
    		System.out.println(100 * dVar);
    		System.out.println(result);
    	}
    }

    And here is the output:
    7
    7.6
    7.0
    0.5999999
    0
    0.29999995
    29.999995231628418
    29

    My confusion arises from the fact that the 4th output statement returns 0.5999999F instead of 0.6F.
    Why is this happening? I'm assuming it has to do with casting. Thank you in advance.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Casting Theory

    Basically, it comes down to how decimals are stored in binary. You can only get so much precision out of it, so it's oftentimes off buy just a tiny bit. For more information, google "what every computer scientist should know about floating point arithmetic" for the go-to article explaining exactly what's happening.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    BigDru (October 25th, 2012)

Similar Threads

  1. Dymaic Casting of List
    By jaypee81 in forum Collections and Generics
    Replies: 1
    Last Post: February 23rd, 2012, 04:46 PM
  2. Class Casting
    By Sana1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 9th, 2012, 08:13 AM
  3. HELP: I have problem casting from Vector to Integer in Java
    By tintin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 17th, 2011, 12:39 PM
  4. Performance of Type Casting and Conversions
    By fritzoid in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2009, 07:56 PM
  5. Type casting error in Java
    By Eric in forum Java Theory & Questions
    Replies: 3
    Last Post: December 13th, 2008, 04:11 PM

Tags for this Thread