0

I have a double x, resulting from a calculation, for which x > 1.0 returns true. I want to be able to print the exact representation in scientific notation of that number without rubbish digits. I know it has 64 bits therefore I'd like to see the exact number it represents.

I am having problems because for example if y = 1.01 and i do printf("%.20lf", y) I get 1.01000000000000000888, which has trash digits at the end.

I am fine with any notation (scientific or whatever) as long as I can read the exact double that x or y represents.

This could be trivial but I cannot find any solution. Thanks for tips.

lucmobz
  • 151
  • 1
  • 6
  • For a `double` anything over 16 decimals will be "rubbish". (15.95 decimals to be exact, assuming standard [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754) floating point representation). – Some programmer dude Dec 13 '21 at 09:25
  • 1
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Richard Critten Dec 13 '21 at 09:42
  • 1
    That is the exact value held in the double. Many (most) decimal values do not have the same floating point value. If you need fixed precision maths (eg for currency) then use scaled integers ie hold the value in cents not floating dollers. – Richard Critten Dec 13 '21 at 09:43
  • since `10 = 2 * 5` every binary number can be exactly represented in decimal form, but there are some decimal numbers which do not have exact representation in binary base. `1.01` is one of those numbers which can't be represented in base 2 so it has to be rounded. When you do conversion back to decimal rounding shows up. Note also that `long double` significant digits value is 18 not 20! – Marek R Dec 13 '21 at 10:15
  • I understand why it is broken, the thing is I do not understand if the those `8s` are part of the binary approximation of that number or if it is just rubbish that I am getting because I am asking for 20 significant digits. In the latter case how to just print the exact `64 bits` number that my `double` represents without having to figure out how many significant digits it has? – lucmobz Dec 13 '21 at 11:50
  • lucmobz, C answer for reference: To print "exact representation in scientific notation of that number without rubbish digits", use `printf("%a\n", x);` C++ has a [similar option](https://www.cplusplus.com/reference/ios/hexfloat/). [Print _exact_ as decimal](https://codereview.stackexchange.com/q/212490/29485) is work. `printf("%.16e\n", x);` is good enough to distinguish from other `double`, though not always _exact_. – chux - Reinstate Monica Dec 13 '21 at 12:05
  • 1
    Not rubbish. 1.01 is saved as the nearest encodable `double` which is _exactly_ 1.0100000000000000088817841970012523233890533447265625. AKA 0x1.028f5c28f5c29p+0. Print to no more than 15 significant digits to never see what you may call _rubbish_. Print to at least 17 significant digits to see _distinctiveness_. – chux - Reinstate Monica Dec 13 '21 at 12:10
  • Thank you for the tips. I am asking if there is a way given a `double x` (say 1.01) to print the nearest encodable double without having to figure out that it contains 52 significant digits in this case. If there is not thanks for all the tips. – lucmobz Dec 14 '21 at 08:06

0 Answers0