~lib2geom-hackers/lib2geom/trunk

« back to all changes in this revision

Viewing changes to decimal-round.h

  • Committer: njh
  • Date: 2006-05-22 11:50:24 UTC
  • Revision ID: svn-v4:4601daaa-0314-0410-9a8b-c964a3c23b6b:trunk/lib2geom:1
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef SEEN_DECIMAL_ROUND_H
 
2
#define SEEN_DECIMAL_ROUND_H
 
3
 
 
4
#include <cmath>
 
5
 
 
6
#include "round.h"
 
7
 
 
8
 
 
9
namespace Inkscape {
 
10
 
 
11
/** Returns x rounded to the nearest \a nplaces decimal places.
 
12
 
 
13
    Implemented in terms of Inkscape::round, i.e. we make no guarantees as to what happens if x is
 
14
    half way between two rounded numbers.  Add a note to the documentation if you care which
 
15
    candidate is chosen for such case (round towards -infinity, +infinity, 0, or "even").
 
16
 
 
17
    Note: nplaces is the number of decimal places without using scientific (e) notation, not the
 
18
    number of significant figures.  This function may not be suitable for values of x whose
 
19
    magnitude is so far from 1 that one would want to use scientific (e) notation.
 
20
 
 
21
    The current implementation happens to allow nplaces to be negative: e.g. nplaces=-2 means
 
22
    rounding to a multiple of 100.  May or may not be useful.
 
23
**/
 
24
inline double
 
25
decimal_round(double const x, int const nplaces)
 
26
{
 
27
    double const multiplier = std::pow(10.0, nplaces);
 
28
    return Inkscape::round( x * multiplier ) / multiplier;
 
29
}
 
30
 
 
31
}
 
32
 
 
33
#endif /* !SEEN_DECIMAL_ROUND_H */
 
34
 
 
35
/*
 
36
  Local Variables:
 
37
  mode:c++
 
38
  c-file-style:"stroustrup"
 
39
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
 
40
  indent-tabs-mode:nil
 
41
  fill-column:99
 
42
  End:
 
43
*/
 
44
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :