Results 1 to 4 of 4
  1. #1
    kiregad is offline Member
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Default Weird problem with doubles not being added to sum

    The following code adds together 1/3 + 3/5 + 5/7 etc.
    Atleast it should do that, but it doesnt. Whats wrong?

    Java Code:
    double sum = 0;
    for(int i=1;i < 98;i+=2)
          sum += i/(i+2);
    
    System.out.println(sum);
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    You're doing your calcultions with ints:
    Java Code:
    System.out.println(1/2);
    System.out.println(1.0/2);
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    kiregad is offline Member
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Default

    Yes ofcourse. Thank you :)
    Java beginner.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

Similar Threads

  1. Weird problem with JSTL
    By Diego_Dalmasso in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-02-2010, 02:51 PM
  2. weird problem
    By GPB in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 12:04 PM
  3. Weird msdos problem
    By dudejonne in forum New To Java
    Replies: 6
    Last Post: 11-02-2009, 08:39 PM
  4. Problem with division using doubles
    By chrismanahan in forum New To Java
    Replies: 3
    Last Post: 10-10-2009, 09:26 PM
  5. Weird problem upon calling same function twice
    By alin_ms in forum New To Java
    Replies: 2
    Last Post: 12-20-2008, 06:14 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
  •