none
how to find the closest positve number from a variable? RRS feed

  • Question

  • Hello,

    I have this code i would like to modify. It return the closest number from my variable fibo1.

    But it return any number positive or not.

    I would like to know if any one could tell me how to modify it to get the closest number above fibo1.

    For exemple, fibo1 = 6 but closest is 7. When actually i want the closest above 6. That would be 4.

    Thank you

    double minDistance = list.Min(no => Math.Abs(fibo1- no));

    double closest = list.First(no => Math.Abs(fibo1- no) == minDistance);



    • Edited by Frankdot Monday, December 9, 2019 7:09 PM
    Monday, December 9, 2019 6:56 PM

Answers

  • Hello,

    I am sorry i did edit the text but it took to long to refresh in the system.

    I am looking for the closest number above fibo1.

    But your approach is interesting. Ill keep that in mind. 

    I could just replace x>= 0 by >= fibo1

    Thank you

    • Marked as answer by Frankdot Monday, December 9, 2019 7:52 PM
    Monday, December 9, 2019 7:40 PM

All replies

  • Just construct another list containing only the positive numbers from your original list.

    double minDistance = list.Where( x => x >= 0).Min(no => Math.Abs(fibo1 - no));
    double closest = list.Where( x => x >= 0).First(no=>Math.Abs(fibo1-no) == minDistance);
    
    

    However, I can't help but think this is a perversion.  It's creating the intermediate list twice, and running through the whole list twice.  It's fun that Linq can sometimes eliminate loops, but there are cases where loops are better, and this is one such case.  This code only traverses the list once and requires no intermediate list:

            double mindist = 99;
            double closest = -99;
            foreach( double el in list )
                if( el >= 0 && Math.Abs(fibo1-el) < mindist )
                {
                    mindist = Math.Abs(fibo1-el);
                    closest = el;
                }
            Console.WriteLine( closest );


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, December 9, 2019 7:25 PM
  • Hello,

    I am sorry i did edit the text but it took to long to refresh in the system.

    I am looking for the closest number above fibo1.

    But your approach is interesting. Ill keep that in mind. 

    I could just replace x>= 0 by >= fibo1

    Thank you

    • Marked as answer by Frankdot Monday, December 9, 2019 7:52 PM
    Monday, December 9, 2019 7:40 PM
  • Well, then, why not this:

        double closest = list.Where(x => x>= fibo1).Sort().First();


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, December 9, 2019 8:09 PM
  • I tried 

    double minDistance = list.Where( x => x >= fibo1).Min(no => Math.Abs(fibo1 - no));

    Its seems to return the right answer, will it return the right answer every time? Tests will tell.

    Will try this one too:

    double closest = list.Where(x => x>= fibo1).Sort().First();

    Unfortunately, i have to wait until a particular event occur to confirm this.

    Thanks

    Monday, December 9, 2019 8:33 PM