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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:48:03+00:00 2026-05-31T03:48:03+00:00

What is the correct syntax for using printf and its cousins sprintf and fprintf

  • 0

What is the correct syntax for using printf and its cousins sprintf and fprintf to display the value of mpreal-type variables? I have tried the naive casting to double:

printf ("... %g ...", (double) var);

only to receive this error message from g++:

error: invalid cast from type ‘mpfr::mpreal’ to type ‘double’

I had no problem using double-type variables elsewhere on the program.

I heard about the type mpreal as part of this library intended to enable the use of the usual binary operators to perform arbitrary precision arithmetic operations.

  • 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-31T03:48:04+00:00Added an answer on May 31, 2026 at 3:48 am

    The mpreal is arbitrary precision floating-point numerical type.

    As such, mpreal numbers might have much more significant digits (even hundreds or thousandths) than double. Which means it is pointless to round mpreal to double before display – you will lose all the original accuracy in this case.

    It is easy to display mpreal in C++:

    /* 100-digits accurate pi */
    mpreal pi = mpfr::const_pi(mpfr::digits2bits(100));
    
    /* Show 60-digits of pi */
    cout.precision(60);
    cout << pi;
    

    However you can use it with printf as well (by converting to string first):

    /* Display all digits (default formatting) */
    printf("pi = %s\n", pi.toString().c_str());         
    
    /* Custom format, 60 digits */
    printf("pi = %s\n", pi.toString("%.60RNf").c_str());
    
    /* Using native printf from MPFR*/
    mpfr_printf("pi = %.60RNf\n", pi.mpfr_srcptr());
    

    Format specification for multiple-precision numbers are the same as for standard with the exception to rounding specification. You can safely use rounding to nearest,RN as in examples above.

    More details on mp-formatting are given in MPFR documentation.

    (I’m author of mpreal class aka MPFR C++)

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

Sidebar

Related Questions

What is the correct syntax for me to specify the return type hints for
I want to print from a servlet. I believe I have the correct syntax.
I have been using mp3info to calculate my file length using the following syntax:
This should be very simple, but I cannot find the correct syntax using search.
ingHey guys. I am wonder the correct syntax for using a $_POST statement in
The syntax for the setTimeout function in javascript code below is correct setTimeout(document.getElementById('test').style.display='none',3000); I
I have a problem with the correct syntax to use UNION and GROUP_CONCAT in
What is the correct syntax for this: IList<string> names = Tom,Scott,Bob.Split(',').ToList<string>().Reverse(); What am I
What is the correct syntax to create objects in javascript that will work across
What is the correct syntax for applying string functions (e.g. normalize-space) on a attribute

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.