Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8957185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:55:45+00:00 2026-06-15T14:55:45+00:00

Currently in one my scientific computation codes I have the following: //Include M_PI constant

  • 0

Currently in one my scientific computation codes I have the following:

//Include M_PI constant
#define _USE_MATH_DEFINES
//Needed for square root function.
#include <math.h>

This works… on Linux at least… I have not tested it for C compilers on all platforms. However, when surveying some older Fortran codes, I recently came across this seemingly clever way of defining pi in another code (not my own):

<angle in deg.>*8.0d0 * datan(1.0d0) / 360.0d0

Of course this is perfectly feasible in C, right? So I could define my conversion function something like:

inline double DegToRad(const double A)
{
   return A * atan(1.0) / 45.0;
}

Which approach is more portable? Are there any numeric (e.g. rounding) considerations that would merit using one approach over another?

I do like that the M_PI constants make the code more readable. Of course, I could happily just assign my own PI constant using the above approach.

What is considered best practice in C/C++ codes that are going to be targeted at multiple platforms (Windows, Linux, etc.)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:55:46+00:00Added an answer on June 15, 2026 at 2:55 pm

    Don’t minimize the readability issue; personally, I’d do something like this:

    #ifndef M_PI
    // Source: http://www.geom.uiuc.edu/~huberty/math5337/groupe/digits.html
    #define M_PI 3.141592653589793238462643383279502884197169399375105820974944592307816406 
    #endif
    
    #define DEG_TO_RAD (M_PI/180.0)
    #define RAD_TO_DEG (180.0/M_PI)
    

    and then declare

    inline double DegToRad(const double deg) {
       return deg * DEG_TO_RAD;
    }
    
    inline double RadToDeg(const double rad) {
       return rad * RAD_TO_DEG;
    }
    

    This is probably not going to be any more or less portable (both atan and M_PI are standard in both C and C++). However it will be more readable than using atan and depending on your compiler’s optimization settings, may save you a costly trig function call.

    UPDATE: It looks like M_PI isn’t as standard as I thought. Inclusion of the #ifndef above should take care of the instances where it’s not available.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have one project that currently contains multiple packages. These packages make up
We currently have one publisher and four subscribers using merge replication. Due to a
I currently have one iPad app, but it now needs to become two. The
I have two relative layouts that are currently displayed one below the other. I
We currently have one dynamic web project (our main web application) already setup on
I have this code currently in one of my websites, $(a.navlink).click(function (ev) { ev.preventDefault();
I currently have one record (with various values) and three user constants with specific
I currently have one .war running on tomcat, I wonder if I put another
I am running JSF 2.0 I currently have one java controller that is called
Brief: I'm fairly new to databases and currently have one with two tables for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.