lyx_mirror/src/support/lyxlib.h
Georg Baum c0ce79452f Introduce proper integer rounding function
This fixes a failing unit test with 32bit gcc 4.9.3 and -O2 optimization:
It computed 9953 instead of 9954 for Length::inPixels() of value 2342.
The reason for this is probably different rounding behaviour caused by storing
the unrounded value in a processor register (uses 80bit accuracy) vs. writing
it back to memory (uses 64bit accuracy). The unrounded value is very close to
9953.5 (which is not representable as an exact IEEE floating point value).

Apart from that, having a proper function for rounding makes the code more
readable, and has the nice side effect to make Length::inPB() work for
negative lengths as well.
2016-02-17 21:42:37 +01:00

61 lines
1.2 KiB
C++

// -*- C++ -*-
/**
* \file lyxlib.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* A selection of useful system functions made
* handy for C++ usage.
*
* \author Lars Gullik Bjønnes
*
* Full author contact details are available in file CREDITS.
*/
#ifndef LYX_LIB_H
#define LYX_LIB_H
// always include <math.h> (also with MSVC), to avoid compiler specific side effects
#include <math.h>
#ifdef _MSC_VER
/// Replacement for C99 round()
inline double round(double x)
{
if (x < 0)
return ceil(x - 0.5);
else
return floor(x + 0.5);
}
#endif
namespace lyx {
namespace support {
/// FIXME: some point to this hmm ?
int kill(int pid, int sig);
/**
* Returns true if var is approximately equal to number with allowed error
* of 'error'.
*
* Usage: if (float_equal(var, number, 0.0001)) { }
*
* This will check if 'var' is approx. equal to 'number' with error of 1/1000
*/
inline bool float_equal(double var, double number, double error)
{
return (number - error <= var && var <= number + error);
}
/// round \p x to nearest integer
inline int iround(double x)
{
return static_cast<int>(round(x));
}
} // namespace support
} // namespace lyx
#endif /* LYX_LIB_H */