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 8274887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:53:06+00:00 2026-06-08T07:53:06+00:00

I’m doing some number crunching, which needs high precision arithmetic. I’m using the GNU

  • 0

I’m doing some number crunching, which needs high precision arithmetic. I’m using the GNU MP library, and according to the GMP manual:

“Floating point number or Float for short, is an arbitrary precision mantissa with a limited precision exponent.”

Although the mantissa is supposed to have an arbitrary precision, I’m still running into precision problems. Rather than bore you with my actual code, here is a near-minimal working example that illustrates my problem. The code calculates 9.3^15, 9.8^15 and (9.3*9.8)^15. On my machine, the values of (9.3^15)*(9.8^15) and (9.3*9.8)^15 start to differ from the 16-th digit onwards, in this case leading to an error of (around) 4.94*10^13.

Any help will be greatly appreciated. Code below.

#include <gmp.h>
#include <gmpxx.h>

#include <iostream>
#include <iomanip>

int main()
{
    mpf_class x, y, z;
    x = y = z = 1.0;

    for (int i = 0; i < 15; i++)
    {
        x *= 9.3;
        y *= 9.8;
        z *= 9.3*9.8;
    }

    std::cout << z - x*y << std::endl;

    return 0;
}
  • 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-08T07:53:07+00:00Added an answer on June 8, 2026 at 7:53 am

    The problem you are seeing is due to the fact that 9.3 * 9.8 is calculated approximately. Please change the literals to be instances of the mpf_class:

    mpf_class a, b;
    a = 9.3;
    b = 9.8;
    
    // ...
    
    x *= a;
    y *= b;
    z *= a * b;
    

    If you need infinite precision, consider using rational numbers instead:

    #include <gmp.h>
    #include <gmpxx.h>
    
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
        mpq_class x(1), y(1), z(1), a(93, 10), b(98, 10);
    
        for (int i = 0; i < 15; i++)
        {
            x *= a;
            y *= b;
            z *= (a * b);
        }
    
        std::cout << z - x*y << std::endl << z << std::endl;
    
        return 0;
    }
    

    prints

    0
    7589015305950762920038660273144124106674963183136666693/30517578125000000000000000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.