Emulate 'log1p()' and 'expm1()' using 'log()' and 'exp()'

(for MSVC which didn't implement those functions until very recent versions).
This commit is contained in:
John Emmas 2016-05-28 19:07:22 +01:00
parent ebeffdd294
commit 22e031135a
3 changed files with 60 additions and 4 deletions

View File

@ -262,6 +262,47 @@ trunc(double x)
return (floor(x));
}
#if defined(_MSC_VER) && (_MSC_VER < 1800)
//***************************************************************
//
// expm1()
//
// Emulates C99 expm1() using exp().
//
// Returns:
//
// On Success: (('e' raised to the power of 'x') - 1)
// (e.g. expm1(1) == 1.7182818).
// On Failure: None, except that calling exp(x) should generate
// an appropriate error for us (such as INF etc).
//
LIBPBD_API double PBD_APICALLTYPE
expm1(double x)
{
return (exp(x) - (double)1.0);
}
//***************************************************************
//
// log1p()
//
// Emulates C99 log1p() using log().
//
// Returns:
//
// On Success: The natural logarithm of (1 + x)
// (e.g. log1p(1) == 0.69314718).
// On Failure: None, except that calling log(x) should generate
// an appropriate error for us (such as ERANGE etc).
//
LIBPBD_API double PBD_APICALLTYPE
log1p(double x)
{
return (log(x + (double)1.0));
}
#endif
#if defined(_MSC_VER) && (_MSC_VER < 1900)
//***************************************************************
//
// log2()
@ -280,6 +321,7 @@ log2(double x)
{
return (log(x) / log((double)2.0));
}
#endif
namespace PBD {

View File

@ -232,7 +232,15 @@ LIBPBD_API ssize_t PBD_APICALLTYPE pwrite(int handle, const void *buf, size_t
LIBPBD_API int PBD_APICALLTYPE poll(struct pollfd *fds, nfds_t nfds, int timeout);
LIBPBD_API double PBD_APICALLTYPE round(double x);
LIBPBD_API double PBD_APICALLTYPE trunc(double x);
LIBPBD_API double PBD_APICALLTYPE log2(double x);
#if defined(_MSC_VER) && (_MSC_VER < 1800)
LIBPBD_API double PBD_APICALLTYPE expm1(double x);
LIBPBD_API double PBD_APICALLTYPE log1p(double x);
#endif
#if defined(_MSC_VER) && (_MSC_VER < 1900)
LIBPBD_API double PBD_APICALLTYPE log2 (double x);
#endif
namespace PBD {

View File

@ -254,9 +254,15 @@ inline int64_t abs(int64_t val) throw()
#if !defined(LIBPBD_API) || defined(PBD_IS_IN_WIN_STATIC_LIB)
extern double round(double x);
// log2().... MSVC doesn't offer the C99 function 'log2()'
// so let's emulate it.
extern double log2(double x);
// Emulate some C99 math functions which MSVC itself didn't
// implement until later in life.
#if defined(_MSC_VER) && (_MSC_VER < 1800)
extern double expm1(double x);
extern double log1p(double x);
#endif
#if defined(_MSC_VER) && (_MSC_VER < 1900)
extern double log2 (double x);
#endif
#endif
#endif /* __ardour_msvc_extensions_h__ */