lilypond-1.3.28
[lilypond.git] / lily / misc.cc
blob8523cddd86e76a5e30271619b74e7b83513fc2c4
1 /*
2 misc.cc -- implement various stuff
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
8 */
10 #include <math.h>
11 #include "misc.hh"
13 int
14 intlog2(int d)
16 int i=0;
17 while (!(d&1))
19 d/= 2;
20 i++;
22 assert (!(d/2));
23 return i;
26 double
27 log_2(double x)
29 return log (x) /log (2.0);
33 static int
34 comp (Real const &a, Real const &b)
36 return sign (a-b);
39 Interval
40 quantise_iv (Array<Real> positions, Real x)
42 positions.sort (comp);
43 Real period = positions.top () - positions[0];
45 int n = int ((x - positions[0]) / period);
46 Real frac = (x - positions[0] ) - n * period;
48 while (frac < 0)
50 frac += period;
51 n --;
54 Real px = frac + positions[0];
55 assert ( positions[0] <= px && px <= positions.top ());
56 int i=0;
57 for (; i < positions.size () - 1; i++)
59 if (positions[i] <= px && px <= positions[i+1])
60 break;
63 return Interval (positions[i] , positions[i+1]) + period * n;