Sheck this simple programm

Discussion in 'Java' started by Excluded_Middle, Jul 13, 2004.

  1. public class check
    {
    public static void main(String[] args)
    {
    System.out.println("Result " + (10/13));
    System.out.println((10/13));
    }
    }

    the output of this programm is:
    result 0
    0

    I try it on linux and windows xp.

    Any idea what is wrong?
     
    Excluded_Middle, Jul 13, 2004
    #1
    1. Advertising

  2. Excluded_Middle wrote:

    > public class check
    > {
    > public static void main(String[] args)
    > {
    > System.out.println("Result " + (10/13));
    > System.out.println((10/13));
    > }
    > }
    >
    > the output of this programm is:
    > result 0
    > 0
    >
    > I try it on linux and windows xp.
    >
    > Any idea what is wrong?



    Nothing is wrong, you just expect a wrong result. Read
    http://mindprod.com/jgloss/division.html
    and
    http://mindprod.com/jgloss/floatingpoint.html
    to see why this is happening.

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Jul 13, 2004
    #2
    1. Advertising

  3. Excluded_Middle

    Andy Hill Guest

    (Excluded_Middle) wrote:
    >public class check
    >{
    > public static void main(String[] args)
    > {
    > System.out.println("Result " + (10/13));
    > System.out.println((10/13));
    > }
    >}
    >
    >the output of this programm is:
    >result 0
    >0
    >
    >I try it on linux and windows xp.
    >
    >Any idea what is wrong?
    >

    Classic symptom of the Intel "floating point" bug.
     
    Andy Hill, Jul 13, 2004
    #3
  4. Excluded_Middle

    Liz Guest

    "Excluded_Middle" <> wrote in message
    news:...
    > public class check
    > {
    > public static void main(String[] args)
    > {
    > System.out.println("Result " + (10/13));
    > System.out.println((10/13));
    > }
    > }
    >
    > the output of this programm is:
    > result 0
    > 0
    >
    > I try it on linux and windows xp.
    >
    > Any idea what is wrong?


    The result of the divide is 0 with remainder 10.
    When printing integers (which is the result type here),
    the remainder is ignored.
     
    Liz, Jul 14, 2004
    #4
  5. Excluded_Middle

    Hal Rosser Guest

    your using int operators - and that's the result you get
    try using (10.0/13.0) --- use doubles if you expect doubles.

    "Excluded_Middle" <> wrote in message
    news:...
    > public class check
    > {
    > public static void main(String[] args)
    > {
    > System.out.println("Result " + (10/13));
    > System.out.println((10/13));
    > }
    > }
    >
    > the output of this programm is:
    > result 0
    > 0
    >
    > I try it on linux and windows xp.
    >
    > Any idea what is wrong?



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.716 / Virus Database: 472 - Release Date: 7/5/2004
     
    Hal Rosser, Jul 14, 2004
    #5
  6. Excluded_Middle

    Fahd Shariff Guest

    one possible solution: System.out.println(((double)10/(double)13));

    --
    Fahd Shariff
    http://www.fahdshariff.cjb.net
    "Let the code do the talking... "
     
    Fahd Shariff, Jul 14, 2004
    #6
  7. Fahd Shariff <> scribbled the following:
    > one possible solution: System.out.println(((double)10/(double)13));


    It's enough to cast *one* of the operands to double. But take care not
    to do this:
    System.out.println((double)(10/13));
    This will still output 0, as 10/13 is 0, and no casting into anything
    will change it.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Hasta la Vista, Abie!"
    - Bart Simpson
     
    Joona I Palaste, Jul 14, 2004
    #7
  8. Excluded_Middle

    Roedy Green Guest

    On 14 Jul 2004 01:22:42 -0700, (Fahd Shariff)
    wrote or quoted :

    >one possible solution: System.out.println(((double)10/(double)13));


    Just apply a termnal D for double literals.

    System.out.println( 10D / 13D );

    see http://mindprod.com/jgloss/literals.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jul 14, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Freddy Fischer
    Replies:
    1
    Views:
    311
    Natty Gur
    Oct 23, 2003
  2. AlexPain

    Java Programm into Systray

    AlexPain, Jul 30, 2003, in forum: Java
    Replies:
    5
    Views:
    1,486
    Eugene Toporov
    Aug 5, 2003
  3. Romeo
    Replies:
    3
    Views:
    367
    Mickey Segal
    Nov 13, 2003
  4. Sam
    Replies:
    1
    Views:
    493
    Andrew Thompson
    Nov 10, 2004
  5. Andreas Horneff

    C++ Programm - Shutdown win2k

    Andreas Horneff, Aug 16, 2004, in forum: C++
    Replies:
    2
    Views:
    3,315
    Jack Klein
    Aug 17, 2004
Loading...

Share This Page