I have a number and need to find the closest number in a given fixed set of numbers.

The fixed set of numbers are 1 (increasing by 6.5%) -> Infinity. (1.000,1.065,1.134,etc.)

For example, given the number 1.0641 I need to return 1.065.

If anyone can lend some thoughts that would be great.

    fnbcprog wrote:

    I have a number and need to find the closest number in a given fixed set of numbers.
    ...
    For example, given the number 1.0641 I need to return 1.065.
    ...

    Shouldnt be 1.064?

    Anyway, maybe this will help u number_format or sprintf

      What a cool question! Nice job.

      So what you really have is a series of numbers like this:

      1.065 to the 0 power (which equals one - anything to the 0 is one)
      1.065 to the 1st power = 1.065
      1.065 to the 2nd power = 1.134225
      etc.

      So then you want to pick any number (for example: 5032) and find which number in the list is closest (or the next highest).

      So what you have to find is 5032 = 1.065 to the X power. Once you know that then you can round X down to the nearst integer and you will know the next lowest number in your fixed list. And you can round X up to the nearest integer and you will know the next highest in the list. Then you can figure out which is closer - or you can pick the next highest, or whatever you want.

      Do you remember how to solve for X when 5032 = 1.065 to the X power?

      It has to do with logarithms. This page will help:

      http://mathforum.org/library/drmath/view/55561.html

        //  For fans of obfuscated code:
        
        $x = 5032;    // Any number you like
        $b = 1.065;   // Base
        
        $c = (($x-pow($b,floor(log($x,$b)))) < (pow($b,ceil(log($x,$b))))) ? pow($b,floor(log($x,$b))) : pow($b,ceil(log($x,$b)));
        
        print "The closest number is: $c\n\n";
        
        
          //  And for those who like readable code:
          
          $x = 5032;
          $base = 1.065;
          
          $power = log($x,$base);
          
          $lower = pow($base,floor($power));
          $higher = pow($base,ceil($power));
          
          if (($x - $lower) < ($higher-$x)) { $closest = $lower; }
          else { $closest = $higher; }
          
          print "The closest number is: $closest\n\n"; 
          

            etully, am I wrong in thinking that this would do just as well:

            $start = 5032;
            $base = 1.065;
            
            $nearest = pow($base, round(log10($start) / log10($base)));

            So that for the number 5032, 4922.628 is nearest (to 3 decimal places).

              I've just noticed that since PHP 4.3.0 there has been a base parameter in the log function. So the above could be simplified to:

              $start = 5032;
              $base = 1.065;
              
              $nearest = pow($base, round(log($start, $base)))

              so long as the server was running a newish version of PHP.

                Bobulous: Mathematically, no, you can't do that.

                Take, for example, this problem:

                We know that:
                102 = 100
                103 = 1000

                So you pick a number like 400 and you want to know which power of 10 it's closest too. Of course, by looking at the problem we know that the answer should be 100. (1000-400 is a difference of 600 but 400-100 is only 300 so 400 is much closer to 100 than 1000).

                So we must find the power that 10 needs to be raised to to get 400. In other words, 10 ^ X = 400.

                echo log(400,10)
                or
                echo log10(400)

                Either one gives you the answer of: 2.6. And if you round 2.6, it goes up to 3. 103 is 1000 which is the wrong answer.

                In other words. 102.5 is not half way between 100 and 1000. It's 316.

                So the only way I know how to do this problem is to find the log of $start in that $base. Then find the floor($baseThat log) and ceil($baseThat log) and see which one $start is closest to.

                I'm sure that there is a mathematical way to improve my algorithm but rounding the power isn't it.

                  Oh, I'm sorry.

                  I thought he wanted the nearest logarithmically.

                    This forum has got some of the best programmers in the industry!!!
                    Thanks a lot for all your help. This is exactly what I was looking for!!

                      Write a Reply...