Skip to content
Fix Code Error

How to use the PI constant in C++

March 13, 2021 by Code Error
Posted By: Anonymous

I want to use the PI constant and trigonometric functions in some C++ program. I get the trigonometric functions with include <math.h>. However, there doesn’t seem to be a definition for PI in this header file.

How can I get PI without defining it manually?

Solution

On some (especially older) platforms (see the comments below) you might need to

#define _USE_MATH_DEFINES

and then include the necessary header file:

#include <math.h>

and the value of pi can be accessed via:

M_PI

In my math.h (2014) it is defined as:

# define M_PI           3.14159265358979323846  /* pi */

but check your math.h for more. An extract from the “old” math.h (in 2009):

/* 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.
 */

However:

  1. on newer platforms (at least on my 64 bit Ubuntu 14.04) I do not need to define the _USE_MATH_DEFINES

  2. On (recent) Linux platforms there are long double values too provided as a GNU Extension:

    # define M_PIl          3.141592653589793238462643383279502884L /* pi */
    
Answered By: Anonymous

Related Articles

  • error LNK2005: ✘✘✘ already defined in…
  • Arrange date columns based on unique id column using R
  • M_PI works with math.h but not with cmath in Visual Studio
  • Dynamically update values of a chartjs chart
  • Form field border-radius is not working only on the…
  • Creating a pandas DataFrame from columns of other…
  • How to remove duplicate item form array of string…
  • What are the new features in C++17?
  • summing two columns in a pandas dataframe
  • Why does my Spring Boot App always shutdown…
  • Spring Boot application.properties value not populating
  • Difference between `constexpr` and `const`
  • How to calculate means for multiple groups of rows…
  • 'foo' was not declared in this scope c++
  • Pandas left outer join multiple dataframes on…
  • Openmp c++: error: collapsed loops not perfectly nested
  • Vis.js - how to hide the gridlines
  • jQuery.on() Delegation: Slightly complex selector…
  • Xamarin 2.0 vs Appcelerator Titanium vs PhoneGap
  • Name [jdbc/mydb] is not bound in this Context
  • Left join only selected columns in R with the…
  • Calculate Pandas DataFrame Time Difference Between…
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • What does "Fatal error: Unexpectedly found nil while…
  • Explaining the 'find -mtime' command
  • Java switch statement: Constant expression required,…
  • 3D Rotation of a camera using its own, new axes
  • Null pointer exception error as method parameter
  • How to use grpc-web in vue?
  • Smart way to truncate long strings
  • Why does C++ code for testing the Collatz conjecture…
  • Homebrew install specific version of formula?
  • How to combine the data from two different…
  • How to end C++ code
  • C++ OpenGL stb_image.h errors
  • How can I find the product GUID of an installed MSI setup?
  • What are type hints in Python 3.5?
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Mismatch Detected for 'RuntimeLibrary'
  • data.table vs dplyr: can one do something well the…
  • define() vs. const
  • Identifying and solving…
  • Vue CLI 3 / Webpack production build not working on…
  • Can't get data of computed state from store - Vue
  • Oracle listener not running and won't start
  • Best practices for circular shift (rotate) operations in C++
  • Ember model: one to many fixtures, server calls
  • Logging best practices
  • Vis.js timeline with polymer error : Something is…
  • #define macro for debug printing in C?
  • problem with client server unix domain stream…
  • How to create a signed APK file using Cordova…
  • Vue JS - How do I get WebGL to render via Vue…
  • For-each over an array in JavaScript
  • What is an optional value in Swift?
  • Why is it common to put CSRF prevention tokens in cookies?
  • How to define partitioning of DataFrame?
  • What is the difference between NULL, '' and 0?
  • How to display gpg key details without importing it?
  • Backbone marionette composite view?
  • useSWR conditional fetch and react-boostrap Accordion
  • Java ElasticSearch None of the configured nodes are…
  • Ember Data. Save model with "belongsTo" relationship
  • Replacing a 32-bit loop counter with 64-bit…
  • "Thinking in AngularJS" if I have a jQuery background?
  • Why my "title" bloc doesn't go to bottom?
  • How to get the data posted in vuejs?
  • Left Join without duplicate rows from left table
  • Where and why do I have to put the "template" and…
  • Ember-data: loading one-to-one related model
  • PHP - Failed to open stream : No such file or directory
  • Ukkonen's suffix tree algorithm in plain English
  • How to add fonts to create-react-app based projects?
  • How to achieve state normalization in Vuex?
  • Classpath resource not found when running as jar
  • Pandas groupby month and year
  • commandButton/commandLink/ajax action/listener…
  • How to evaluate Vue.js component props in the data property?
  • What does if __name__ == "__main__": do?
  • Why does modulus division (%) only work with integers?
  • What are forward declarations in C++?
  • Ember updating array does not update template
  • What is an application binary interface (ABI)?
  • What is a NullReferenceException, and how do I fix it?
  • Difference between variable declaration syntaxes in…
  • "Undefined reference to" template class constructor
  • Is it possible to print a variable's type in standard C++?
  • What are the undocumented features and limitations…
  • dotnet restore on build server says package is not…
  • XMLHttpRequest cannot load ✘✘✘ No…
  • error: strcpy was not declared in this scope
  • What are the nuances of scope prototypal /…
  • SASS compiler not adding to include path
  • How can I create an executable to run a kernel in a…
  • Backbone and Rails associations: Avoiding JSON…
  • Why is ggplot geom_area empty when attempting to…
  • Download multiple stocks with pandas yahoo finance…
  • How can I access and process nested objects, arrays or JSON?
  • How can I manually compile a svelte component down…
  • How to generate a random string of a fixed length in Go?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to move an element into another element?

Next Post:

Is there a way to get the source code from an APK file?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error