lilypond-1.3.130
[lilypond.git] / lily / misc.cc
blob9ff95f2a020e9926ef66955f929d70d8f2c79bda
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"
14 Return the 2-log, rounded down
16 int
17 intlog2(int d)
19 assert (d);
20 int i=0;
21 while ((d != 1))
23 d/= 2;
24 i++;
27 assert (!(d/2));
28 return i;
31 double
32 log_2(double x)
34 return log (x) /log (2.0);
38 static int
39 comp (Real const &a, Real const &b)
41 return sign (a-b);
44 Interval
45 quantise_iv (Array<Real> positions, Real x)
47 positions.sort (comp);
48 Real period = positions.top () - positions[0];
50 int n = int ((x - positions[0]) / period);
51 Real frac = (x - positions[0] ) - n * period;
53 while (frac < 0)
55 frac += period;
56 n --;
59 Real px = frac + positions[0];
60 assert ( positions[0] <= px && px <= positions.top ());
61 int i=0;
62 for (; i < positions.size () - 1; i++)
64 if (positions[i] <= px && px <= positions[i+1])
65 break;
68 return Interval (positions[i] , positions[i+1]) + period * n;