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 5 of 5

Thread: Need help with my double return function.

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with my double return function.

    public class Rectangle{
     
    	public int num;
    	public int den;
    	public double val;
     
    	public void display(){
    	System.out.println(num);
    	System.out.println(den);
    	}
     
    	public double doubleValue(){
    	val = num / den;
    	return val;
    	}
     
    	public static void main(String[] args) {
     
    	Rectangle x = new Rectangle();
    	x.num = 5;
    	x.den = 25;
     
    	x.display();
     
    	System.out.println(x.doubleValue());
     
    	}
    	}

    Apparently when I print out the doubleValue() function it just sends a 0.0 value and does not give the right answer which is 0.2
    I cant seen to find what is wrong in my code, care to help?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with my double return function.

    Check that your code is NOT doing integer arithmetic. For example 5/6 = 0 vs 5.0/6 = 0.833333
    To force the compiler to use floating point arithmetic multiply a value by by 1.0 before dividing.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my double return function.

    Quote Originally Posted by Norm View Post
    Check that your code is NOT doing integer arithmetic. For example 5/6 = 0 vs 5.0/6 = 0.833333
    To force the compiler to use floating point arithmetic multiply a value by by 1.0 before dividing.
    Thanks I tried it and it worked!

    im just wondering if thats the only way to force the compiler to use floating point arithmetic?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with my double return function.

    The compiler looks at the data types used in a computation and uses the arithmetic that matches those values.
    If there are mixed data types, there are rules the compiler uses to chose which type of arithmetic to use.
    Google or some search will tell you what they are. I don't know them by heart.

  5. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Need help with my double return function.

    im just wondering if thats the only way to force the compiler to use floating point arithmetic?
    It's not the only way - casting one of the ints in your expression to a floating point type might be safer. If a mathematician was involved in writing your compiler, she might 'optimise out' a constant multiplication by 1.0 as an identity operation (she hasn't and '* 1.0' does work, but I like to be explicit about what type I'm using). The Java Language Specification is the place to look for the definitive view, but it's not exactly light reading:

    Conversions and Promotions

Similar Threads

  1. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  2. function timeDiff
    By bobby321 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 25th, 2011, 02:23 PM
  3. Problem with Return Function
    By Tracy22 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 26th, 2010, 03:32 PM
  4. Need Help on the Interface Function
    By yel_hiei in forum Object Oriented Programming
    Replies: 12
    Last Post: July 29th, 2010, 07:27 AM
  5. Function of difference between two numbers
    By uplink600 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 13th, 2009, 05:57 AM