We’d like to remind Forumites to please avoid political debate on the Forum.

This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.

📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!

Very simple Java help please

Hi all,

I'm working through a Java tutorial, and the tutorial says that

double num = 5 / 3;

should equal 1.66

however, I've tried in both Eclipse and NetBeans, and I get the answer 1.0

Can anyone please shed some light? I've done a google search and can't see anything about it.

Thanks

Comments

  • esuhl
    esuhl Posts: 9,409 Forumite
    Part of the Furniture 1,000 Posts Name Dropper
    I don't know anything much about Java, so this is just a stab in the dark... but... it sounds like the 1.67 result is being truncated or stored as an integer... I don't know if Java would generate an error like some languages or whether it would try to convert one data type to another without warning.

    Also, I remember in some languages "/" means "divide by" whereas "\" means "divide by and discard any non-integer remainder"... Not sure if that applies to Java...
  • tincat
    tincat Posts: 935 Forumite
    Thanks for your reply, but \ produces an error so doesn't look like it's accepted as code.

    I've tried searching for a solution, but can't come up with a good search term to show comparable searches.

    It's annoying because it should work dammit :p
  • bod1467
    bod1467 Posts: 15,214 Forumite
    How are you outputting the result to know that the value you are seeing is 1.0?
  • tincat
    tincat Posts: 935 Forumite
    edited 20 February 2013 at 8:02PM
    public class HelloWorld {

    public static void main(String[] args) {

    double num1 = 5 / 3;
    double num2 = 7;

    double num3 = num1 + num2;

    System.out.println(num1);
    System.out.println(num2);
    System.out.print("=");
    System.out.println(num3);
    System.out.println();


    double i = 13;
    double j = 12;

    System.out.println("i is " + i);
    System.out.println("j is " + j);

    double k = i/j;
    System.out.println("i/j is " + k);
    k = i * j;
    System.out.println("i * j is " + k);


    }

    }
    The above code gives the output as follows
    1.0
    7.0
    =9.0

    i is 13.0
    j is 12.0
    i/j is 1.0833333333333333
    i * j is 156.0
    Notice how the first calculation (num1) gives the answer of 1.0, yet when dealing with double k = i/j; I get the full answer.

    It's all compiled together in one bit of code, so I don't get how the first doesn't work and yet the second does,.

    Thanks
  • tincat wrote: »
    The above code gives the output as follows
    Notice how the first calculation (num1) gives the answer of 1.0, yet when dealing with double k = i/j; I get the full answer.

    It's all compiled together in one bit of code, so I don't get how the first doesn't work and yet the second does,.

    Thanks
    Ahhh not a java expert but
    double num1 = 5 / 3

    replace with
    double five = 5
    double three = 3
    double num1 = five/three



    4.8kWp 12x400W Longhi 9.6 kWh battery Giv-hy 5.0 Inverter, WSW facing Essex . Aint no sunshine ☀️ Octopus gas fixed dec 24 @ 5.74 tracker again+ Octopus Intelligent Flux leccy
  • RobTang
    RobTang Posts: 1,064 Forumite
    edited 20 February 2013 at 8:27PM
    You might want to read up on how Java interprets literal numbers.

    Numbers without a floating point are integer literals.

    7 (int) / 9 (int) = int result, thus it gets truncated it doesn't matter that you are storing the final in a double, the calculation is made then stored (note this is for clarification the compiler in reality probably does something different but with the same results.)

    however
    double num1 = 5.0 / 3.0;
    sop(num1); // prints 1.666666667

    This is because these tokens with floating points are automatically considered doubles (a floating point literal)

    You can explicitly say an integer (without a floating point) is a double like so

    double num1 = 5d / 3d;
    sop(num1); // prints 1.666666667

    Your second example with I and j work because the compiler is operating on 2 doubles not 2 integers

    I ran your code and I don't get 9 for num1+num2, I get 8 which is what it should be
  • tincat
    tincat Posts: 935 Forumite
    RobTang wrote: »

    I ran your code and I don't get 9 for num1+num2, I get 8 which is what it should be

    Sorry that's my fault. I pasted in the code as [num1 = 5/2] and got [2.0
    7.0
    =9.0] I changed the 2.0 to a 1.0 manually as when I first posted I said I'd put [num1 = 5/3]. I forgot to change the 9.0 to an 8.0.


  • tincat
    tincat Posts: 935 Forumite
    Ahhh not a java expert but
    double num1 = 5 / 3

    replace with
    double five = 5
    double three = 3
    double num1 = five/three


    Thanks, that works, although I'm still surprised that it doesn't work when putting it in normally. The tutorial said that using 'double' would allow decimal places.

    However, at least I have a workaround.
  • Ectophile
    Ectophile Posts: 8,049 Forumite
    Part of the Furniture 1,000 Posts Photogenic Name Dropper
    5 and 3 are both integers, not doubles. The division "5 / 3" is therefore an integer divide and returns an integer result - in this case 1 (the remainder is simply discarded). You then assign the integer value 1 to a double and get the value 1.0.

    If you want divide to return a double, you need to convert one or both numbers to doubles before you do the divide.

    Your workaround does this, though there are other ways.
    If it sticks, force it.
    If it breaks, well it wasn't working right anyway.
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 351.7K Banking & Borrowing
  • 253.4K Reduce Debt & Boost Income
  • 454K Spending & Discounts
  • 244.7K Work, Benefits & Business
  • 600.1K Mortgages, Homes & Bills
  • 177.3K Life & Family
  • 258.4K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16.2K Discuss & Feedback
  • 37.6K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.