Be (somewhat more) consistent about the value of pi

Use M_PI (and friends), where possible, in favor of hand-coded
approximations of various (in)accuracies.  Where that's not available
(e.g. fragment shaders), use the same value that qmath.h uses for
M_PI, for consistency.  Replaced math.h with qmath.h in places that
defined a fall-back in case math.h omits it (it's not in the C++
standard, although M_PI is in POSIX); or removed this entirely where
it wasn't used.

Reworked some code to reduce the amount of arithmetic needed, in the
process; e.g. pulling common factors out of loops.  Revised an
example's doc to not waste time talking about using a six-sig-fig
value for pi (which we no longer do) - it really wasn't relevant, or
anything to be proud of; nor did the doc mention its later use.

Task-number: QTBUG-58083
Change-Id: I5a31e3a2b6a823b97a43209bed61a37b9aa6c05f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2017-03-22 13:18:52 +01:00
parent 82deb0ad16
commit 02b7ec05d5
23 changed files with 116 additions and 162 deletions

View File

@ -52,15 +52,9 @@
#include <QtWidgets> #include <QtWidgets>
#include <QtNetwork> #include <QtNetwork>
#include <math.h>
#include "lightmaps.h" #include "lightmaps.h"
#include "slippymap.h" #include "slippymap.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
// how long (milliseconds) the user need to hold (after a tap on the screen) // how long (milliseconds) the user need to hold (after a tap on the screen)
// before triggering the magnifying glass feature // before triggering the magnifying glass feature
// 701, a prime number, is the sum of 229, 233, 239 // 701, a prime number, is the sum of 229, 233, 239

View File

@ -50,12 +50,7 @@
#include <QtCore> #include <QtCore>
#include <QtWidgets> #include <QtWidgets>
#include <qmath.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define WORLD_SIZE 8 #define WORLD_SIZE 8
int world_map[WORLD_SIZE][WORLD_SIZE] = { int world_map[WORLD_SIZE][WORLD_SIZE] = {

View File

@ -276,21 +276,20 @@ void Renderer::createGeometry()
extrude(x4, y4, y4, x4); extrude(x4, y4, y4, x4);
extrude(y4, x4, y3, x3); extrude(y4, x4, y3, x3);
const qreal Pi = 3.14159f;
const int NumSectors = 100; const int NumSectors = 100;
const qreal sectorAngle = 2 * qreal(M_PI) / NumSectors;
for (int i = 0; i < NumSectors; ++i) { for (int i = 0; i < NumSectors; ++i) {
qreal angle1 = (i * 2 * Pi) / NumSectors; qreal angle = i * sectorAngle;
qreal x5 = 0.30 * qSin(angle1); qreal x5 = 0.30 * qSin(angle);
qreal y5 = 0.30 * qCos(angle1); qreal y5 = 0.30 * qCos(angle);
qreal x6 = 0.20 * qSin(angle1); qreal x6 = 0.20 * qSin(angle);
qreal y6 = 0.20 * qCos(angle1); qreal y6 = 0.20 * qCos(angle);
qreal angle2 = ((i + 1) * 2 * Pi) / NumSectors; angle += sectorAngle;
qreal x7 = 0.20 * qSin(angle2); qreal x7 = 0.20 * qSin(angle);
qreal y7 = 0.20 * qCos(angle2); qreal y7 = 0.20 * qCos(angle);
qreal x8 = 0.30 * qSin(angle2); qreal x8 = 0.30 * qSin(angle);
qreal y8 = 0.30 * qCos(angle2); qreal y8 = 0.30 * qCos(angle);
quad(x5, y5, x6, y6, x7, y7, x8, y8); quad(x5, y5, x6, y6, x7, y7, x8, y8);

View File

@ -50,8 +50,7 @@
#include "glwidget.h" #include "glwidget.h"
#include <QtGui/QImage> #include <QtGui/QImage>
#include <qmath.h>
#include <math.h>
static GLint cubeArray[][3] = { static GLint cubeArray[][3] = {
{0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}, {0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0},
@ -253,6 +252,6 @@ void GLWidget::drawCube(int i, GLfloat z, GLfloat rotation, GLfloat jmp, GLfloat
xOffs[i] = xOffs[i] > 1.0f ? 1.0f : -1.0f; xOffs[i] = xOffs[i] > 1.0f ? 1.0f : -1.0f;
} }
xOffs[i] += xInc[i]; xOffs[i] += xInc[i];
yOffs[i] = qAbs(cos((-3.141592f * jmp) * xOffs[i]) * amp) - 1; yOffs[i] = qAbs(cos((-GLfloat(M_PI) * jmp) * xOffs[i]) * amp) - 1;
rot[i] += rotation; rot[i] += rotation;
} }

View File

@ -52,8 +52,7 @@
#include <QMouseEvent> #include <QMouseEvent>
#include <QTimer> #include <QTimer>
#include <qmath.h>
#include <math.h>
GLWidget::GLWidget(QWidget *parent) GLWidget::GLWidget(QWidget *parent)
: QGLWidget(parent) : QGLWidget(parent)
@ -190,8 +189,6 @@ GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius,
GLdouble outerRadius, GLdouble thickness, GLdouble outerRadius, GLdouble thickness,
GLdouble toothSize, GLint toothCount) GLdouble toothSize, GLint toothCount)
{ {
const double Pi = 3.14159265358979323846;
GLuint list = glGenLists(1); GLuint list = glGenLists(1);
glNewList(list, GL_COMPILE); glNewList(list, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, reflectance); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, reflectance);
@ -199,7 +196,8 @@ GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius,
GLdouble r0 = innerRadius; GLdouble r0 = innerRadius;
GLdouble r1 = outerRadius - toothSize / 2.0; GLdouble r1 = outerRadius - toothSize / 2.0;
GLdouble r2 = outerRadius + toothSize / 2.0; GLdouble r2 = outerRadius + toothSize / 2.0;
GLdouble delta = (2.0 * Pi / toothCount) / 4.0; GLdouble toothAngle = 2 * M_PI / toothCount;
GLdouble delta = toothAngle / 4.0;
GLdouble z = thickness / 2.0; GLdouble z = thickness / 2.0;
glShadeModel(GL_FLAT); glShadeModel(GL_FLAT);
@ -211,7 +209,7 @@ GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius,
glBegin(GL_QUAD_STRIP); glBegin(GL_QUAD_STRIP);
for (int j = 0; j <= toothCount; ++j) { for (int j = 0; j <= toothCount; ++j) {
GLdouble angle = 2.0 * Pi * j / toothCount; GLdouble angle = j * toothAngle;
glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z); glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z);
glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z); glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z);
glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z); glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z);
@ -221,7 +219,7 @@ GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius,
glBegin(GL_QUADS); glBegin(GL_QUADS);
for (int j = 0; j < toothCount; ++j) { for (int j = 0; j < toothCount; ++j) {
GLdouble angle = 2.0 * Pi * j / toothCount; GLdouble angle = j * toothAngle;
glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z); glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z);
glVertex3d(r2 * cos(angle + delta), r2 * sin(angle + delta), sign * z); glVertex3d(r2 * cos(angle + delta), r2 * sin(angle + delta), sign * z);
glVertex3d(r2 * cos(angle + 2 * delta), r2 * sin(angle + 2 * delta), sign * z); glVertex3d(r2 * cos(angle + 2 * delta), r2 * sin(angle + 2 * delta), sign * z);
@ -233,7 +231,7 @@ GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius,
glBegin(GL_QUAD_STRIP); glBegin(GL_QUAD_STRIP);
for (int i = 0; i < toothCount; ++i) { for (int i = 0; i < toothCount; ++i) {
for (int j = 0; j < 2; ++j) { for (int j = 0; j < 2; ++j) {
GLdouble angle = 2.0 * Pi * (i + j / 2.0) / toothCount; GLdouble angle = (i + j / 2.0) * toothAngle;
GLdouble s1 = r1; GLdouble s1 = r1;
GLdouble s2 = r2; GLdouble s2 = r2;
if (j == 1) if (j == 1)
@ -257,7 +255,7 @@ GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius,
glBegin(GL_QUAD_STRIP); glBegin(GL_QUAD_STRIP);
for (int i = 0; i <= toothCount; ++i) { for (int i = 0; i <= toothCount; ++i) {
GLdouble angle = i * 2.0 * Pi / toothCount; GLdouble angle = i * toothAngle;
glNormal3d(-cos(angle), -sin(angle), 0.0); glNormal3d(-cos(angle), -sin(angle), 0.0);
glVertex3d(r0 * cos(angle), r0 * sin(angle), +z); glVertex3d(r0 * cos(angle), r0 * sin(angle), +z);
glVertex3d(r0 * cos(angle), r0 * sin(angle), -z); glVertex3d(r0 * cos(angle), r0 * sin(angle), -z);

View File

@ -49,10 +49,9 @@
****************************************************************************/ ****************************************************************************/
#include <QtGui/QImage> #include <QtGui/QImage>
#include <qmath.h>
#include "glwidget.h" #include "glwidget.h"
#include <math.h>
#ifndef GL_MULTISAMPLE #ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE 0x809D #define GL_MULTISAMPLE 0x809D
#endif #endif
@ -291,8 +290,6 @@ void GLWidget::restoreGLState()
glPopAttrib(); glPopAttrib();
} }
#define PI 3.14159
void GLWidget::timerEvent(QTimerEvent *) void GLWidget::timerEvent(QTimerEvent *)
{ {
if (QApplication::mouseButtons() != 0) if (QApplication::mouseButtons() != 0)
@ -310,7 +307,7 @@ void GLWidget::timerEvent(QTimerEvent *)
rot_x += 0.1f; rot_x += 0.1f;
int dx, dy; // disturbance point int dx, dy; // disturbance point
float s, v, W, t; float s, v, W;
int i, j; int i, j;
static float wt[128][128]; static float wt[128][128];
const int width = logo.width(); const int width = logo.width();
@ -325,11 +322,11 @@ void GLWidget::timerEvent(QTimerEvent *)
for ( j = 0; j < width; ++j) { for ( j = 0; j < width; ++j) {
s = sqrt((double) ((j - dx) * (j - dx) + (i - dy) * (i - dy))); s = sqrt((double) ((j - dx) * (j - dx) + (i - dy) * (i - dy)));
wt[i][j] += 0.1f; wt[i][j] += 0.1f;
t = s / v; const double angle = 2 * M_PI * W * (wt[i][j] + s / v);
if (s != 0) if (s != 0)
wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t)) / (0.2*(s + 2)); wave[i * width + j] = AMP * sin(angle) / (0.2 * (s + 2));
else else
wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t)); wave[i * width + j] = AMP * sin(angle);
} }
} }
} }

View File

@ -49,7 +49,7 @@
****************************************************************************/ ****************************************************************************/
#include "glwidget.h" #include "glwidget.h"
#include <math.h> #include <qmath.h>
#ifndef GL_MULTISAMPLE #ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE 0x809D #define GL_MULTISAMPLE 0x809D
@ -116,7 +116,6 @@ void GLWidget::timerEvent(QTimerEvent *)
void GLWidget::makeObject() void GLWidget::makeObject()
{ {
QColor qtGreen(QColor::fromCmykF(0.40, 0.0, 1.0, 0.0)); QColor qtGreen(QColor::fromCmykF(0.40, 0.0, 1.0, 0.0));
const double Pi = 3.14159265358979323846;
const int NumSectors = 15; const int NumSectors = 15;
GLdouble x1 = +0.06; GLdouble x1 = +0.06;
GLdouble y1 = -0.14; GLdouble y1 = -0.14;
@ -130,18 +129,19 @@ void GLWidget::makeObject()
list = glGenLists(1); list = glGenLists(1);
glNewList(list, GL_COMPILE); glNewList(list, GL_COMPILE);
{ {
const double sectorAngle = 2 * M_PI / NumSectors;
for (int i = 0; i < NumSectors; ++i) { for (int i = 0; i < NumSectors; ++i) {
double angle1 = (i * 2 * Pi) / NumSectors; double angle = i * sectorAngle;
GLdouble x5 = 0.30 * sin(angle1); GLdouble x5 = 0.30 * sin(angle);
GLdouble y5 = 0.30 * cos(angle1); GLdouble y5 = 0.30 * cos(angle);
GLdouble x6 = 0.20 * sin(angle1); GLdouble x6 = 0.20 * sin(angle);
GLdouble y6 = 0.20 * cos(angle1); GLdouble y6 = 0.20 * cos(angle);
double angle2 = ((i + 1) * 2 * Pi) / NumSectors; angle += sectorAngle;
GLdouble x7 = 0.20 * sin(angle2); GLdouble x7 = 0.20 * sin(angle);
GLdouble y7 = 0.20 * cos(angle2); GLdouble y7 = 0.20 * cos(angle);
GLdouble x8 = 0.30 * sin(angle2); GLdouble x8 = 0.30 * sin(angle);
GLdouble y8 = 0.30 * cos(angle2); GLdouble y8 = 0.30 * cos(angle);
qglColor(qtGreen); qglColor(qtGreen);
quad(GL_QUADS, x5, y5, x6, y6, x7, y7, x8, y8); quad(GL_QUADS, x5, y5, x6, y6, x7, y7, x8, y8);

View File

@ -54,7 +54,7 @@
#include <QOpenGLShaderProgram> #include <QOpenGLShaderProgram>
#include <QOpenGLTexture> #include <QOpenGLTexture>
#include <QCoreApplication> #include <QCoreApplication>
#include <math.h> #include <qmath.h>
#include "mainwindow.h" #include "mainwindow.h"
#include "bubble.h" #include "bubble.h"
@ -455,21 +455,21 @@ void GLWidget::createGeometry()
extrude(x4, y4, y4, x4); extrude(x4, y4, y4, x4);
extrude(y4, x4, y3, x3); extrude(y4, x4, y3, x3);
const qreal Pi = 3.14159f;
const int NumSectors = 100; const int NumSectors = 100;
const qreal sectorAngle = 2 * qreal(M_PI) / NumSectors;
for (int i = 0; i < NumSectors; ++i) { for (int i = 0; i < NumSectors; ++i) {
qreal angle1 = (i * 2 * Pi) / NumSectors; qreal angle = i * sectorAngle;
qreal x5 = 0.30 * sin(angle1); qreal x5 = 0.30 * sin(angle);
qreal y5 = 0.30 * cos(angle1); qreal y5 = 0.30 * cos(angle);
qreal x6 = 0.20 * sin(angle1); qreal x6 = 0.20 * sin(angle);
qreal y6 = 0.20 * cos(angle1); qreal y6 = 0.20 * cos(angle);
qreal angle2 = ((i + 1) * 2 * Pi) / NumSectors; angle += sectorAngle;
qreal x7 = 0.20 * sin(angle2); qreal x7 = 0.20 * sin(angle);
qreal y7 = 0.20 * cos(angle2); qreal y7 = 0.20 * cos(angle);
qreal x8 = 0.30 * sin(angle2); qreal x8 = 0.30 * sin(angle);
qreal y8 = 0.30 * cos(angle2); qreal y8 = 0.30 * cos(angle);
quad(x5, y5, x6, y6, x7, y7, x8, y8); quad(x5, y5, x6, y6, x7, y7, x8, y8);

View File

@ -1,4 +1,4 @@
#define M_PI 3.1415926535897932384626433832795 #define M_PI 3.14159265358979323846
#define SPEED 10000.0 #define SPEED 10000.0
uniform int currentTime; uniform int currentTime;

View File

@ -49,7 +49,7 @@
****************************************************************************/ ****************************************************************************/
#include "glwidget.h" #include "glwidget.h"
#include <math.h> #include <qmath.h>
#include <QGuiApplication> #include <QGuiApplication>
GLWidget::GLWidget(QWidget *parent) GLWidget::GLWidget(QWidget *parent)
@ -282,21 +282,21 @@ void Renderer::createGeometry()
extrude(x4, y4, y4, x4); extrude(x4, y4, y4, x4);
extrude(y4, x4, y3, x3); extrude(y4, x4, y3, x3);
const qreal Pi = 3.14159f;
const int NumSectors = 100; const int NumSectors = 100;
const qreal sectorAngle = 2 * qreal(M_PI) / NumSectors;
for (int i = 0; i < NumSectors; ++i) { for (int i = 0; i < NumSectors; ++i) {
qreal angle1 = (i * 2 * Pi) / NumSectors; qreal angle = i * sectorAngle;
qreal x5 = 0.30 * sin(angle1); qreal x5 = 0.30 * sin(angle);
qreal y5 = 0.30 * cos(angle1); qreal y5 = 0.30 * cos(angle);
qreal x6 = 0.20 * sin(angle1); qreal x6 = 0.20 * sin(angle);
qreal y6 = 0.20 * cos(angle1); qreal y6 = 0.20 * cos(angle);
qreal angle2 = ((i + 1) * 2 * Pi) / NumSectors; angle += sectorAngle;
qreal x7 = 0.20 * sin(angle2); qreal x7 = 0.20 * sin(angle);
qreal y7 = 0.20 * cos(angle2); qreal y7 = 0.20 * cos(angle);
qreal x8 = 0.30 * sin(angle2); qreal x8 = 0.30 * sin(angle);
qreal y8 = 0.30 * cos(angle2); qreal y8 = 0.30 * cos(angle);
quad(x5, y5, x6, y6, x7, y7, x8, y8); quad(x5, y5, x6, y6, x7, y7, x8, y8);

View File

@ -53,11 +53,10 @@
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QPainter> #include <QPainter>
#include <QStyleOption> #include <QStyleOption>
#include <qmath.h>
#include <cmath> const qreal Pi = M_PI;
const qreal TwoPi = 2 * M_PI;
static const double Pi = 3.14159265358979323846264338327950288419717;
static double TwoPi = 2.0 * Pi;
static qreal normalizeAngle(qreal angle) static qreal normalizeAngle(qreal angle)
{ {

View File

@ -98,14 +98,9 @@
\section1 Window Class Implementation \section1 Window Class Implementation
In the implementation of the \c Window class we first declare the In the \c Window constructor, we define the various painter paths
constant \c Pi with six significant figures: and create corresponding \c RenderArea widgets which will render
the graphical shapes:
\snippet painting/painterpaths/window.cpp 0
In the constructor, we then define the various painter paths and
create corresponding \c RenderArea widgets which will render the
graphical shapes:
\snippet painting/painterpaths/window.cpp 1 \snippet painting/painterpaths/window.cpp 1

View File

@ -52,13 +52,9 @@
#include <QtWidgets> #include <QtWidgets>
#include <QtCore/qmath.h> #include <QtCore/qmath.h>
#include <qmath.h>
#include "blureffect.h" #include "blureffect.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
BlurPicker::BlurPicker(QWidget *parent): QGraphicsView(parent), m_index(0.0), m_animation(this, "index") BlurPicker::BlurPicker(QWidget *parent): QGraphicsView(parent), m_index(0.0), m_animation(this, "index")
{ {
setBackgroundBrush(QPixmap(":/images/background.jpg")); setBackgroundBrush(QPixmap(":/images/background.jpg"));
@ -84,9 +80,10 @@ void BlurPicker::setIndex(qreal index)
m_index = index; m_index = index;
qreal baseline = 0; qreal baseline = 0;
const qreal iconAngle = 2 * M_PI / m_icons.count();
for (int i = 0; i < m_icons.count(); ++i) { for (int i = 0; i < m_icons.count(); ++i) {
QGraphicsItem *icon = m_icons[i]; QGraphicsItem *icon = m_icons[i];
qreal a = ((i + m_index) * 2 * M_PI) / m_icons.count(); qreal a = (i + m_index) * iconAngle;
qreal xs = 170 * qSin(a); qreal xs = 170 * qSin(a);
qreal ys = 100 * qCos(a); qreal ys = 100 * qCos(a);
QPointF pos(xs, ys); QPointF pos(xs, ys);

View File

@ -52,7 +52,7 @@
#include "scene.h" #include "scene.h"
#include <QtGui/qmatrix4x4.h> #include <QtGui/qmatrix4x4.h>
#include <QtGui/qvector3d.h> #include <QtGui/qvector3d.h>
#include <cmath> #include <qmath.h>
#include "3rdparty/fbm.h" #include "3rdparty/fbm.h"
@ -868,11 +868,12 @@ void Scene::renderCubemaps()
QVector3D center; QVector3D center;
const float eachAngle = 2 * M_PI / m_cubemaps.size();
for (int i = m_frame % N; i < m_cubemaps.size(); i += N) { for (int i = m_frame % N; i < m_cubemaps.size(); i += N) {
if (0 == m_cubemaps[i]) if (0 == m_cubemaps[i])
continue; continue;
float angle = 2.0f * PI * i / m_cubemaps.size(); float angle = i * eachAngle;
center = m_trackBalls[1].rotation().rotatedVector(QVector3D(std::cos(angle), std::sin(angle), 0.0f)); center = m_trackBalls[1].rotation().rotatedVector(QVector3D(std::cos(angle), std::sin(angle), 0.0f));

View File

@ -63,8 +63,6 @@
#include "glbuffers.h" #include "glbuffers.h"
#include "qtbox.h" #include "qtbox.h"
#define PI 3.14159265358979
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QMatrix4x4; class QMatrix4x4;
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -53,11 +53,10 @@
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QPainter> #include <QPainter>
#include <QStyleOption> #include <QStyleOption>
#include <qmath.h>
#include <cmath> const qreal Pi = M_PI;
const qreal TwoPi = 2 * M_PI;
static const double Pi = 3.14159265358979323846264338327950288419717;
static double TwoPi = 2.0 * Pi;
static qreal normalizeAngle(qreal angle) static qreal normalizeAngle(qreal angle)
{ {

View File

@ -51,13 +51,10 @@
#include "arrow.h" #include "arrow.h"
#include <cmath> #include <qmath.h>
#include <QPen> #include <QPen>
#include <QPainter> #include <QPainter>
const qreal Pi = 3.14;
//! [0] //! [0]
Arrow::Arrow(DiagramItem *startItem, DiagramItem *endItem, QGraphicsItem *parent) Arrow::Arrow(DiagramItem *startItem, DiagramItem *endItem, QGraphicsItem *parent)
: QGraphicsLineItem(parent) : QGraphicsLineItem(parent)
@ -134,10 +131,10 @@ void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
double angle = std::atan2(-line().dy(), line().dx()); double angle = std::atan2(-line().dy(), line().dx());
QPointF arrowP1 = line().p1() + QPointF(sin(angle + Pi / 3) * arrowSize, QPointF arrowP1 = line().p1() + QPointF(sin(angle + M_PI / 3) * arrowSize,
cos(angle + Pi / 3) * arrowSize); cos(angle + M_PI / 3) * arrowSize);
QPointF arrowP2 = line().p1() + QPointF(sin(angle + Pi - Pi / 3) * arrowSize, QPointF arrowP2 = line().p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
cos(angle + Pi - Pi / 3) * arrowSize); cos(angle + M_PI - M_PI / 3) * arrowSize);
arrowHead.clear(); arrowHead.clear();
arrowHead << line().p1() << arrowP1 << arrowP2; arrowHead << line().p1() << arrowP1 << arrowP2;

View File

@ -51,12 +51,9 @@
#include "edge.h" #include "edge.h"
#include "node.h" #include "node.h"
#include <cmath> #include <qmath.h>
#include <QPainter> #include <QPainter>
static const double Pi = 3.14159265358979323846264338327950288419717;
//! [0] //! [0]
Edge::Edge(Node *sourceNode, Node *destNode) Edge::Edge(Node *sourceNode, Node *destNode)
: arrowSize(10) : arrowSize(10)
@ -140,14 +137,14 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
// Draw the arrows // Draw the arrows
double angle = std::atan2(-line.dy(), line.dx()); double angle = std::atan2(-line.dy(), line.dx());
QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * arrowSize, QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + M_PI / 3) * arrowSize,
cos(angle + Pi / 3) * arrowSize); cos(angle + M_PI / 3) * arrowSize);
QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * arrowSize, QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
cos(angle + Pi - Pi / 3) * arrowSize); cos(angle + M_PI - M_PI / 3) * arrowSize);
QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize, QPointF destArrowP1 = destPoint + QPointF(sin(angle - M_PI / 3) * arrowSize,
cos(angle - Pi / 3) * arrowSize); cos(angle - M_PI / 3) * arrowSize);
QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize, QPointF destArrowP2 = destPoint + QPointF(sin(angle - M_PI + M_PI / 3) * arrowSize,
cos(angle - Pi + Pi / 3) * arrowSize); cos(angle - M_PI + M_PI / 3) * arrowSize);
painter->setBrush(Qt::black); painter->setBrush(Qt::black);
painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2); painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2);

View File

@ -53,11 +53,7 @@
#include <QtWidgets> #include <QtWidgets>
#include <cmath> #include <qmath.h>
//! [0]
const float Pi = 3.14159f;
//! [0]
//! [1] //! [1]
Window::Window() Window::Window()
@ -133,8 +129,8 @@ Window::Window()
QPainterPath starPath; QPainterPath starPath;
starPath.moveTo(90, 50); starPath.moveTo(90, 50);
for (int i = 1; i < 5; ++i) { for (int i = 1; i < 5; ++i) {
starPath.lineTo(50 + 40 * std::cos(0.8 * i * Pi), starPath.lineTo(50 + 40 * std::cos(0.8 * i * M_PI),
50 + 40 * std::sin(0.8 * i * Pi)); 50 + 40 * std::sin(0.8 * i * M_PI));
} }
starPath.closeSubpath(); starPath.closeSubpath();
//! [9] //! [9]

View File

@ -50,13 +50,11 @@
#include <QtWidgets> #include <QtWidgets>
#include <cmath> #include <qmath.h>
#include <stdlib.h> #include <stdlib.h>
#include "basictoolsplugin.h" #include "basictoolsplugin.h"
const float Pi = 3.14159f;
//! [0] //! [0]
QStringList BasicToolsPlugin::brushes() const QStringList BasicToolsPlugin::brushes() const
{ {
@ -149,8 +147,8 @@ QPainterPath BasicToolsPlugin::generateShape(const QString &shape,
} else if (shape == tr("Star")) { } else if (shape == tr("Star")) {
path.moveTo(90, 50); path.moveTo(90, 50);
for (int i = 1; i < 5; ++i) { for (int i = 1; i < 5; ++i) {
path.lineTo(50 + 40 * std::cos(0.8 * i * Pi), path.lineTo(50 + 40 * std::cos(0.8 * i * M_PI),
50 + 40 * std::sin(0.8 * i * Pi)); 50 + 40 * std::sin(0.8 * i * M_PI));
} }
path.closeSubpath(); path.closeSubpath();
} else if (shape == tr("Text...")) { } else if (shape == tr("Text...")) {

View File

@ -47,6 +47,7 @@
** $QT_END_LICENSE$ ** $QT_END_LICENSE$
** **
****************************************************************************/ ****************************************************************************/
#include <qmath.h>
//! [0] //! [0]
QPair<QString, double> pair; QPair<QString, double> pair;
@ -55,7 +56,7 @@ QPair<QString, double> pair;
//! [1] //! [1]
pair.first = "pi"; pair.first = "pi";
pair.second = 3.14159265358979323846; pair.second = M_PI;
//! [1] //! [1]
//! [struct] //! [struct]
@ -65,10 +66,10 @@ struct Variable {
}; };
Variable v; Variable v;
v.name = "pi"; v.name = "pi";
v.value = 3.14159265358979323846; v.value = M_PI;
//! [struct] //! [struct]
//! [2] //! [2]
QList<QPair<int, double> > list; QList<QPair<int, double> > list;
list.append(qMakePair(66, 3.14159)); list.append(qMakePair(66, M_PI));
//! [2] //! [2]

View File

@ -30,8 +30,6 @@
#include <QtTest/QtTest> #include <QtTest/QtTest>
#include <qmath.h> #include <qmath.h>
static const double PI = 3.14159265358979323846264338327950288;
class tst_QMath : public QObject class tst_QMath : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -55,8 +53,9 @@ void tst_QMath::fastSinCos()
{ {
// Test evenly spaced angles from 0 to 2pi radians. // Test evenly spaced angles from 0 to 2pi radians.
const int LOOP_COUNT = 100000; const int LOOP_COUNT = 100000;
const qreal loopAngle = 2 * M_PI / (LOOP_COUNT - 1);
for (int i = 0; i < LOOP_COUNT; ++i) { for (int i = 0; i < LOOP_COUNT; ++i) {
qreal angle = i * 2 * PI / (LOOP_COUNT - 1); qreal angle = i * loopAngle;
QVERIFY(qAbs(qSin(angle) - qFastSin(angle)) < 1e-5); QVERIFY(qAbs(qSin(angle) - qFastSin(angle)) < 1e-5);
QVERIFY(qAbs(qCos(angle) - qFastCos(angle)) < 1e-5); QVERIFY(qAbs(qCos(angle) - qFastCos(angle)) < 1e-5);
} }
@ -69,18 +68,18 @@ void tst_QMath::degreesToRadians_data()
QTest::addColumn<double>("degreesDouble"); QTest::addColumn<double>("degreesDouble");
QTest::addColumn<double>("radiansDouble"); QTest::addColumn<double>("radiansDouble");
QTest::newRow( "pi" ) << 180.0f << float(M_PI) << 180.0 << PI; QTest::newRow( "pi" ) << 180.0f << float(M_PI) << 180.0 << M_PI;
QTest::newRow( "doublepi" ) << 360.0f << float(2*M_PI) << 360.0 << 2*PI; QTest::newRow( "doublepi" ) << 360.0f << float(2 * M_PI) << 360.0 << 2 * M_PI;
QTest::newRow( "halfpi" ) << 90.0f << float(M_PI_2) << 90.0 << PI/2; QTest::newRow( "halfpi" ) << 90.0f << float(M_PI_2) << 90.0 << M_PI_2;
QTest::newRow( "random" ) << 123.1234567f << 2.1489097058516724f << 123.123456789123456789 << 2.148909707407169856192285627; QTest::newRow( "random" ) << 123.1234567f << 2.1489097058516724f << 123.123456789123456789 << 2.148909707407169856192285627;
QTest::newRow( "bigrandom" ) << 987654321.9876543f << 17237819.79023679f << 987654321987654321.987654321987654321 << 17237819790236794.0; QTest::newRow( "bigrandom" ) << 987654321.9876543f << 17237819.79023679f << 987654321987654321.987654321987654321 << 17237819790236794.0;
QTest::newRow( "zero" ) << 0.0f << 0.0f << 0.0 << 0.0; QTest::newRow( "zero" ) << 0.0f << 0.0f << 0.0 << 0.0;
QTest::newRow( "minuspi" ) << -180.0f << float(-M_PI) << 180.0 << PI; QTest::newRow( "minuspi" ) << -180.0f << float(-M_PI) << 180.0 << M_PI;
QTest::newRow( "minusdoublepi" ) << -360.0f << float(-2*M_PI) << -360.0 << -2*PI; QTest::newRow( "minusdoublepi" ) << -360.0f << float(-2 * M_PI) << -360.0 << -2 * M_PI;
QTest::newRow( "minushalfpi" ) << -90.0f << float(-M_PI_2) << -90.0 << -PI/2; QTest::newRow( "minushalfpi" ) << -90.0f << float(-M_PI_2) << -90.0 << -M_PI_2;
QTest::newRow( "minusrandom" ) << -123.1234567f << -2.1489097058516724f << -123.123456789123456789 << -2.148909707407169856192285627; QTest::newRow( "minusrandom" ) << -123.1234567f << -2.1489097058516724f << -123.123456789123456789 << -2.148909707407169856192285627;
QTest::newRow( "minusbigrandom" ) << -987654321.9876543f << -17237819.79023679f << -987654321987654321.987654321987654321 << -17237819790236794.0; QTest::newRow( "minusbigrandom" ) << -987654321.9876543f << -17237819.79023679f << -987654321987654321.987654321987654321 << -17237819790236794.0;
@ -104,18 +103,18 @@ void tst_QMath::radiansToDegrees_data()
QTest::addColumn<double>("radiansDouble"); QTest::addColumn<double>("radiansDouble");
QTest::addColumn<double>("degreesDouble"); QTest::addColumn<double>("degreesDouble");
QTest::newRow( "pi" ) << float(M_PI) << 180.0f << PI << 180.0; QTest::newRow( "pi" ) << float(M_PI) << 180.0f << M_PI << 180.0;
QTest::newRow( "doublepi" ) << float(2*M_PI) << 360.0f << 2*PI << 360.0; QTest::newRow( "doublepi" ) << float(2 * M_PI) << 360.0f << 2 * M_PI << 360.0;
QTest::newRow( "halfpi" ) << float(M_PI_2) << 90.0f<< PI/2 << 90.0; QTest::newRow( "halfpi" ) << float(M_PI_2) << 90.0f << M_PI_2 << 90.0;
QTest::newRow( "random" ) << 123.1234567f << 7054.454427971739f << 123.123456789123456789 << 7054.4544330781363896676339209079742431640625; QTest::newRow( "random" ) << 123.1234567f << 7054.454427971739f << 123.123456789123456789 << 7054.4544330781363896676339209079742431640625;
QTest::newRow( "bigrandom" ) << 987654321.9876543f << 56588424267.74745f << 987654321987654321.987654321987654321 << 56588424267747450880.0; QTest::newRow( "bigrandom" ) << 987654321.9876543f << 56588424267.74745f << 987654321987654321.987654321987654321 << 56588424267747450880.0;
QTest::newRow( "zero" ) << 0.0f << 0.0f << 0.0 << 0.0; QTest::newRow( "zero" ) << 0.0f << 0.0f << 0.0 << 0.0;
QTest::newRow( "minuspi" ) << float(-M_PI) << -180.0f << -PI << -180.0; QTest::newRow( "minuspi" ) << float(-M_PI) << -180.0f << -M_PI << -180.0;
QTest::newRow( "minusdoublepi" ) << float(-2*M_PI) << -360.0f << -2*PI << -360.0; QTest::newRow( "minusdoublepi" ) << float(-2 * M_PI) << -360.0f << -2 * M_PI << -360.0;
QTest::newRow( "minushalfpi" ) << float(-M_PI_2) << -90.0f << -PI/2 << -90.0; QTest::newRow( "minushalfpi" ) << float(-M_PI_2) << -90.0f << -M_PI_2 << -90.0;
QTest::newRow( "minusrandom" ) << -123.1234567f << -7054.454427971739f << -123.123456789123456789 << -7054.4544330781363896676339209079742431640625; QTest::newRow( "minusrandom" ) << -123.1234567f << -7054.454427971739f << -123.123456789123456789 << -7054.4544330781363896676339209079742431640625;
QTest::newRow( "minusbigrandom" ) << -987654321.9876543f << -56588424267.74745f << -987654321987654321.987654321987654321 << -56588424267747450880.0; QTest::newRow( "minusbigrandom" ) << -987654321.9876543f << -56588424267.74745f << -987654321987654321.987654321987654321 << -56588424267747450880.0;

View File

@ -29,7 +29,6 @@
#include <QtTest/QtTest> #include <QtTest/QtTest>
#include "qtransform.h" #include "qtransform.h"
#include <math.h>
#include <qpolygon.h> #include <qpolygon.h>
#include <qdebug.h> #include <qdebug.h>
@ -67,10 +66,6 @@ private:
Q_DECLARE_METATYPE(QTransform) Q_DECLARE_METATYPE(QTransform)
#if defined(Q_OS_WIN) && !defined(M_PI)
#define M_PI 3.14159265897932384626433832795f
#endif
void tst_QTransform::mapRect_data() void tst_QTransform::mapRect_data()
{ {
mapping_data(); mapping_data();