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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:16:04+00:00 2026-06-05T06:16:04+00:00

I am writing a program that prints floating point literals to be used inside

  • 0

I am writing a program that prints floating point literals to be used inside another program.

How many digits do I need to print in order to preserve the precision of the original float?

Since a float has 24 * (log(2) / log(10)) = 7.2247199 decimal digits of precision, my initial thought was that printing 8 digits should be enough. But if I’m unlucky, those 0.2247199 get distributed to the left and to the right of the 7 significant digits, so I should probably print 9 decimal digits.

Is my analysis correct? Is 9 decimal digits enough for all cases? Like printf("%.9g", x);?

Is there a standard function that converts a float to a string with the minimum number of decimal digits required for that value, in the cases where 7 or 8 are enough, so I don’t print unnecessary digits?

Note: I cannot use hexadecimal floating point literals, because standard C++ does not support them.

  • 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-05T06:16:06+00:00Added an answer on June 5, 2026 at 6:16 am

    In order to guarantee that a binary->decimal->binary roundtrip recovers the original binary value, IEEE 754 requires

    
    The original binary value will be preserved by converting to decimal and back again using:[10]
    
        5 decimal digits for binary16
        9 decimal digits for binary32
        17 decimal digits for binary64
        36 decimal digits for binary128
    
    For other binary formats the required number of decimal digits is
    
        1 + ceiling(p*log10(2)) 
    
    where p is the number of significant bits in the binary format, e.g. 24 bits for binary32.
    

    In C, the functions you can use for these conversions are snprintf() and strtof/strtod/strtold().

    Of course, in some cases even more digits can be useful (no, they are not always “noise”, depending on the implementation of the decimal conversion routines such as snprintf() ). Consider e.g. printing dyadic fractions.

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

Sidebar

Related Questions

I am writing a program that uses prints a hex dump of its input.
I'm writing a program that needs to print off high resolution .tiff images. The
I'm writing a program that for one part asks for the program to print
I'm writing a silly program in python for a friend that prints We are
I'm just a beginner in C++. I'm writing small and simple program that prints
I'm writing a small program that prints a menu screen with a list of
i have a closed source program that prints output to standard output. i need
I'm writing a C program that prints something on terminal using ncurses. It should
currently I'm writing little program that reads elf file header and prints some information
I have a program that I'm writing where I am using another company's library

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.