Results 1 to 20 of 20
  1. #1
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default Dividing in Java

    Hello

    Is there anyway to divide a smaller number by a larger number in java. I'm trying to work out some probabilities

    like for instance...... 364/365

    thanks.

    i keep getting 0.0 when i do this.

  2. #2
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    yes you can...

    when you have an integer it doesnt use decimals so your getting 0.0 because your rounding.

    all you gotta do it declare them as float or double.

    hope i could help

  3. #3
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    do..
    {
    float bob=364;
    float jim=365;

    bob/jim
    }

  4. #4
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    Hi ramsrocker. thanks for the help. it works great. thank you!

  5. #5
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    No Problem glad i could help!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    ramsrocker is right in that int division will always truncate the result to return an int as you've found out. The solution is as he described.

    The only thing I'd like to add is that you should avoid using float unless necessary. The double primitive type is much more accurate than the float type without much in the way of significant overhead for most calculations.

  7. #7
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    agreed! i was falling back in to old c++ habits where i made everything floats lol. its a nice safe-fall variable. ; ]]

    nice catch fubarable.

  8. #8
    veens4444 is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default

    make them to float numbers
    float var_name3=(float)var_name1;
    float var_name4=(float)var_name2;

    now divide var_name3/var_name4;

    u will get correct value

  9. #9
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    haha veens your a lil slow ; ]

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by veens4444 View Post
    make them to float numbers
    veens, You may wish to re-read what was written above.

    To the original poster, I again disagree with this. Please read my post above to see why.

  11. #11
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    hey fub.. doubles go up to like 16 bits right? so cant they slow down a program if over used?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If they slow the program down, it's negligible. I suppose if you have a huge array of them vs an array of floats, you could run out of memory with the doubles sooner though.

  13. #13
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    ahh kk got it.

    btw right now im using Bluej to program. is that good/bad?

    its what we use in school so i kinda just use it cause.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't know as I don't use it. I use Eclipse, but it has a bit of a learning curve associated with it, and so I don't recommend it if you're just starting out. Later on, it's a good bet.

  15. #15
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    ahh ok.. blueJ seems pretty simple to use. it definitely hogs my computer tho.

  16. #16
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default Performance of double generally the same as float

    Quote Originally Posted by ramsrocker View Post
    hey fub.. doubles go up to like 16 bits right? so cant they slow down a program if over used?
    A double uses 8 bytes, compared to a float that uses 4 bytes. As has been mentioned, floats offer very poor precision and it's very rare that you need to shave 4 measly bytes off the storage size of a number.

    The basic operations on floats and doubles compile directly to machine instructions. For example, on Intel machines under Hotspot, adding floats compiles to an ADDSS instruction, and adding doubles to an ADDSD instruction. If you look at the Intel optimisation manual, you'll see that these instructions have the same latency and throughput: i.e. an operation on a float has (almost) precisely the same performance as an operation on a double.*

    The exception is floating point division, whose time is proportional to the byte width of the number, so dividing a double takes around twice as long as dividing a float.

    * Bar potential subtle differences, e.g. moving doubles around means you'll hit more of the CPU cache (because you're moving more bytes!) than with floats.
    Last edited by neilcoffey; 02-15-2009 at 07:38 AM. Reason: Add clarification

  17. #17
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    ok yea. i knew most of that. thanks for clarifying the byte count.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ramsrocker View Post
    ok yea. i knew most of that.
    I didn't. Thanks as always, Neil.

  19. #19
    motorkar123 is offline Member
    Join Date
    Mar 2011
    Location
    Czech republic
    Posts
    1
    Rep Power
    0

    Default

    Hi,

    I am new to Java and I can not solve out similar problem. I get zero, could you tell me why, please?

    public static void main(String[] args) {
    float a;
    int b = 50, c = 80;
    a = vypocetHodnoceni (b, c);
    System.out.println("Hodnoceni testu je " + a);
    }
    static float vypocetHodnoceni (int spravneOdpovedi, int pocetOtazek)
    {
    return (spravneOdpovedi / pocetOtazek) * 100;
    }

    }

  20. #20
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default

    2. Don't hijack an old thread, get your own -- they're free.
    1. The answer is already given in this thread, so there's no need for you to start a new one in this case. Just read all the responses.

    db

Similar Threads

  1. Dividing numbers with remainders showing
    By denisdoherty in forum New To Java
    Replies: 16
    Last Post: 04-24-2008, 05:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •