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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:25:32+00:00 2026-06-15T08:25:32+00:00

What is the correct way to initialize GMP floating point variables (mpf_t or mpf_class,

  • 0

What is the correct way to initialize GMP floating point variables (mpf_t or mpf_class, does not matter) from double?

Code:

#include <iostream>
#include <gmpxx.h>

int main()
{
        double d=0.1;

        //1024 bits is more that 300 decimal digits
        mpf_set_default_prec(1024);

        mpf_class m(d);

        //after initializing mpf_class variable, set default output precision
        std::cout.precision(50);
        std::cout.setf(std::ios_base::scientific);

        std::cout << m << std::endl;

        return 0;
}

The output is:

1.00000000000000005551115123125782702118158340454102e-01

It would be okay, if I printed d directly, but in the m variable 300 decimal digits of mantissa are trusted! I use GMP for an iterative numerical method, so these non-zeros introduce mistake and make the method converge slowly.

If I initilize m as mpf_class m("0.1");, the output is:

1.00000000000000000000000000000000000000000000000000e-01

So the problem is not in operator<< overload for mpf_class. The problem exists not only for initializing, but for assigning too.

At present I use the following:

mpf_class linsys::DtoGMP(const double& arg)
{
        char buf[512];
        sprintf(buf,"%.15le\n",arg);
        return mpf_class(buf);
}

for correct conversion.

Is there a faster and/or more native way to do it?

My OS is OpenSUSE 12.1, compiler: gcc 4.6.2

  • 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-15T08:25:33+00:00Added an answer on June 15, 2026 at 8:25 am

    If you print out the double with that same precision, you should see the same strange-looking number. That’s simply because 0.1 can’t be accurately represented in floating point. The mpf_class is accurately reproducing the value stored in the double. It’s the double that isn’t matching your expectations.

    There’s probably a way to specify a precision to gmp or some way to round the input. I’m not sure where to look though.

    Edit

    mpf_class has a constructor with a precision parameter: http://www.gnu.org/software/gmp/manual/html_node/C—Interface-Floats.html

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

Sidebar

Related Questions

What would be the correct way to create a fully URL-encoded file:// URI from
I know the correct way to manage JQuery.dialog is to initialize with: $(#dialog).dialog({ autoOpen:
(I'm not even sure how to ask this in an correct way, sorry. My
How would you correct way to handle an exception in this situation? Initially I
What is the correct way to add a product variant? I create the product
What's the correct way to set a flash message for the current view but
Which is correct way to extend/subclass components? Ext.define('Holidays.Components.UserInfo', { extend: 'Ext.panel.Panel', alias: 'widget.UserInfo', region:
What is the correct way to use pygame.Color names when using unicode_literals ? Python
What is the correct way to draw isometric tiles in a 2D game? I've
What's the correct way to prevent invoking (creating an instance of) a C type

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.