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

  • Home
  • SEARCH
  • 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 196719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:47:29+00:00 2026-05-11T16:47:29+00:00

I am coding several reference algorithms in both Java and C/C++. Some of these

  • 0

I am coding several reference algorithms in both Java and C/C++. Some of these algorithms use π. I would like for the two implementations of each algorithm to produce identical results, without rounding differently. One way to do this that has worked consistently so far is to use a custom-defined pi constant which is exactly the same in both languages, such as 3.14159. However, it strikes me as silly to define pi when there are already high-precision constants defined in both the Java and GCC libraries.

I’ve spent some time writing quick test programs, looking at documentation for each library, and reading up on floating-point types. But I haven’t been able to convince myself that java.lang.Math.PI (or java.lang.StrictMath.PI) is, or is not, equal to M_PI in math.h.

GCC 3.4.4 (cygwin) math.h contains:

#define M_PI            3.14159265358979323846
                                         ^^^^^

but this

printf("%.20f", M_PI);

produces

3.14159265358979311600
                 ^^^^^

which suggests that the last 5 digits cannot be trusted.

Meanwhile, Javadocs say that java.lang.Math.PI is:

The double value that is closer than
any other to pi, the ratio of the
circumference of a circle to its
diameter.

and

public static final double PI  3.141592653589793d

which omits the questionable last five digits from the constant.

System.out.printf("%.20f\n", Math.PI);

produces

3.14159265358979300000
                 ^^^^^

If you have some expertise in floating-point data types, can you convince me that these library constants are exactly equal? Or that they are definitely not equal?

  • 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-05-11T16:47:29+00:00Added an answer on May 11, 2026 at 4:47 pm

    Yes, they are equal, and using them will insure that GCC and Java implementations of the same algorithm are on the same footing – at least as much as using a hand-defined pi constant would†.

    One caveat, hinted by S. Lott, is that the GCC implementation must hold M_PI in a double data type, and not long double, to ensure equivalence. Both Java and GCC appear to use IEEE-754’s 64-bit decimal representation for their respective double data types. The bytewise representation (MSB to LSB) of the library value, expressed as a double, can be obtained as follows (thanks to JeeBee):

    pi_bytes.c:

    #include <math.h>
    #include <stdio.h>
    int main()
    {
       double pi = M_PI;
       printf("%016llx\n", *((uint64_t*)&pi));
    }
    

    pi_bytes.java:

    class pi_bytes
    {
       public static void main(String[] a)
       {
          System.out.printf("%016x\n", Double.doubleToRawLongBits( Math.PI ) );
       }
    }
    

    Running both:

    $ gcc -lm -o pi_bytes pi_bytes.c && ./pi_bytes
    400921fb54442d18
    
    $ javac pi_bytes.java && java pi_bytes
    400921fb54442d18
    

    The underlying representations of M_PI (as a double) and Math.PI are identical, down to their bits.

    † – As noted by Steve Schnepp, the output of math functions such as sin, cos, exp, etc. is not guaranteed to be identical, even if the inputs to those computations are bitwise identical.

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

Sidebar

Ask A Question

Stats

  • Questions 209k
  • Answers 209k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It'd probably be pretty hard to find guidelines specific to… May 12, 2026 at 9:50 pm
  • Editorial Team
    Editorial Team added an answer The way to solve your problem is to make your… May 12, 2026 at 9:50 pm
  • Editorial Team
    Editorial Team added an answer $() takes a 2nd parameter which limits the search scope… May 12, 2026 at 9:50 pm

Related Questions

I've created a 3 screen wizard using the Zend_Form_SubForm example from the online reference
I have been dancing around this issue for awhile but it keeps coming up.
I want to build a lightweight linux configuration to use for development. The first
I am coding up a program for automated testing which randomly clicks an open

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.