Results 1 to 4 of 4
  1. #1
    MrJargon is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Extremely new - confused why my code is giving me a negative result...

    So I am confused why this extremely simple program seems to be giving me a negative value for amount of calculations done within one minute ( Just using it as a bit of fun to see how different computers in the office perform).

    Here is the code:

    Java Code:
    class Benchmark {
    	public static void main(String[] args) {
    		long endTime = System.currentTimeMillis() + 60000;
    		int count = 0;
    		for (int i = 0; System.currentTimeMillis() < endTime; i++) {
    			double x = Math.sqrt(System.currentTimeMillis());
    			count = i;
    		}
    		System.out.print(count + " calculations per minute");
    	}
    }
    I am getting results between -2.1billion and -3.4billion which to me would make sense since they are not the best computers in the world but I would be expecting a positive value?

    If anyone can point me in the right direction that would be fantastic.

    Regards,

    Ben

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: Extremely new - confused why my code is giving me a negative result...

    You're calculating square roots in one minute; try to do the same for 1000 milli seconds (one second) or less and see the results (hint: an int overflows after +- 2 bilion or so).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    MrJargon is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Re: Extremely new - confused why my code is giving me a negative result...

    Ahh of course!

    That's brilliant thank you very much!

    Regards,

    Ben

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: Extremely new - confused why my code is giving me a negative result...

    Quote Originally Posted by MrJargon View Post
    Ahh of course!

    That's brilliant thank you very much!
    You're welcome of course; but brilliancy has nothing to do with it; computers are just fast nowadays; your counter overflowed at least once in a minute so it calculated at least 2 billion square roots in one minute; in the old days I worked with calculators where you could see them calculate one single square root because its display was flickering ...

    kind regards,

    Jos (<--- old sod ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Confused about the result of method
    By JayCut in forum New To Java
    Replies: 19
    Last Post: 11-01-2013, 04:29 PM
  2. nested for loop confused with result
    By sayswho in forum New To Java
    Replies: 2
    Last Post: 07-14-2013, 12:26 PM
  3. Replies: 0
    Last Post: 04-14-2013, 11:33 PM
  4. Replies: 1
    Last Post: 10-29-2012, 06:54 AM
  5. Replies: 2
    Last Post: 10-25-2011, 10:26 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
  •