By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,804 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,804 IT Pros & Developers. It's quick & easy.

Explaining a little snippet

100+
P: 258
Hi everyone
I don't have any knowledge in JAVA.
Could anyone please explain what this JAVA class does?

What does "Double.POSITIVE_INFINITY" do?
Expand|Select|Wrap|Line Numbers
  1. public class Fraga7{
  2.     public static double min (double[] tal){
  3.         double min = Double.POSITIVE_INFINITY;
  4.         for (int i = 0; i < tal.length; i++){
  5.             if (tal[i] < min) min = tal[i];
  6.         }
  7.         return min;
  8.     }
  9. }
  10.  
Thanks
Jan 6 '13 #1

✓ answered by Anas Mosaad

The line in question (Line 3) assigns an initial min value positive infinity. It seemed weird to me but I like it.

Usually, I would set the first item of the list to min and start the loop with 1. However, I have to check if the list is empty and in that case I should return infinity. This snippet does all of this. It returns Infinity if the list is empty (i.e.
Expand|Select|Wrap|Line Numbers
  1. min(new double[]{})
). Really a very good one.

Share this Question
Share on Google+
3 Replies


100+
P: 185
The line in question (Line 3) assigns an initial min value positive infinity. It seemed weird to me but I like it.

Usually, I would set the first item of the list to min and start the loop with 1. However, I have to check if the list is empty and in that case I should return infinity. This snippet does all of this. It returns Infinity if the list is empty (i.e.
Expand|Select|Wrap|Line Numbers
  1. min(new double[]{})
). Really a very good one.
Jan 6 '13 #2

100+
P: 258
Thanks Anas Mosaad
I appreciate your help
Jan 6 '13 #3

100+
P: 185
You are always welcome :)
Jan 6 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.