Skip to content
qwt_math.h 3.29 KiB
Newer Older
pixhawk's avatar
pixhawk committed
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
pixhawk's avatar
pixhawk committed
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#ifndef QWT_MATH_H
#define QWT_MATH_H

#include "qwt_global.h"

Bryant's avatar
Bryant committed
#if defined(_MSC_VER)
/*
  Microsoft says:
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
  Define _USE_MATH_DEFINES before including math.h to expose these macro
  definitions for common math constants.  These are placed under an #ifdef
  since these commonly-defined names are not part of the C/C++ standards.
*/
#define _USE_MATH_DEFINES 1
pixhawk's avatar
pixhawk committed
#endif

Bryant's avatar
Bryant committed
#include <qmath.h>
#include "qwt_global.h"
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
#ifndef M_PI_2
// For Qt <= 4.8.4 M_PI_2 is not known by MinGW-w64 
// when compiling with -std=c++11
#define M_PI_2 (1.57079632679489661923)
pixhawk's avatar
pixhawk committed
#endif

#ifndef LOG_MIN
Bryant's avatar
Bryant committed
//! Minimum value for logarithmic scales
pixhawk's avatar
pixhawk committed
#define LOG_MIN 1.0e-100
#endif

#ifndef LOG_MAX
//! Maximum value for logarithmic scales
#define LOG_MAX 1.0e100
#endif

Bryant's avatar
Bryant committed
QWT_EXPORT double qwtGetMin( const double *array, int size );
QWT_EXPORT double qwtGetMax( const double *array, int size );
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
QWT_EXPORT double qwtNormalizeRadians( double radians );
QWT_EXPORT double qwtNormalizeDegrees( double degrees );
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
/*!
  \brief Compare 2 values, relative to an interval
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
  Values are "equal", when :
  \f$\cdot value2 - value1 <= abs(intervalSize * 10e^{-6})\f$
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
  \param value1 First value to compare
  \param value2 Second value to compare
  \param intervalSize interval size
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
  \return 0: if equal, -1: if value2 > value1, 1: if value1 > value2
*/
inline int qwtFuzzyCompare( double value1, double value2, double intervalSize )
{
    const double eps = qAbs( 1.0e-6 * intervalSize );
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    if ( value2 - value1 > eps )
        return -1;
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    if ( value1 - value2 > eps )
        return 1;
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    return 0;
}
pixhawk's avatar
pixhawk committed


Bryant's avatar
Bryant committed
inline bool qwtFuzzyGreaterOrEqual( double d1, double d2 )
{
    return ( d1 >= d2 ) || qFuzzyCompare( d1, d2 );
}
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
inline bool qwtFuzzyLessOrEqual( double d1, double d2 )
{
    return ( d1 <= d2 ) || qFuzzyCompare( d1, d2 );
}
pixhawk's avatar
pixhawk committed

//! Return the sign
Bryant's avatar
Bryant committed
inline int qwtSign( double x )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    if ( x > 0.0 )
Bryant's avatar
Bryant committed
    else if ( x < 0.0 )
        return ( -1 );
pixhawk's avatar
pixhawk committed
    else
pixhawk's avatar
pixhawk committed

//! Return the square of a number
Bryant's avatar
Bryant committed
inline double qwtSqr( double x )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    return x * x;
pixhawk's avatar
pixhawk committed
}

Bryant's avatar
Bryant committed
//! Approximation of arc tangent ( error below 0,005 radians )
inline double qwtFastAtan( double x )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    if ( x < -1.0 )
        return -M_PI_2 - x / ( x * x + 0.28 );
Bryant's avatar
Bryant committed
    if ( x > 1.0 )
        return M_PI_2 - x / ( x * x + 0.28 );
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    return x / ( 1.0 + x * x * 0.28 );
pixhawk's avatar
pixhawk committed
}

Bryant's avatar
Bryant committed
//! Approximation of arc tangent ( error below 0,005 radians )
inline double qwtFastAtan2( double y, double x )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    if ( x > 0 )
        return qwtFastAtan( y / x );
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    if ( x < 0 )
    {
        const double d = qwtFastAtan( y / x );
        return ( y >= 0 ) ? d + M_PI : d - M_PI;
    }
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    if ( y < 0.0 )
        return -M_PI_2;
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    if ( y > 0.0 )
        return M_PI_2;
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    return 0.0;
pixhawk's avatar
pixhawk committed
}

Bryant's avatar
Bryant committed
// Translate degrees into radians
inline double qwtRadians( double degrees )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    return degrees * M_PI / 180.0;
pixhawk's avatar
pixhawk committed
}

Bryant's avatar
Bryant committed
// Translate radians into degrees
inline double qwtDegrees( double degrees )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    return degrees * 180.0 / M_PI;
pixhawk's avatar
pixhawk committed
}

#endif