0

I have used this :

  long double f =79228162514264337593543950336.0;//maximum ; 2 ^ 96 because f is 12 bytes
  cout.precision(30);
  cout<<f;

But some numbers turns wrong . why ?

Oliver
  • 23,072
  • 33
  • 138
  • 230
S.A.Parkhid
  • 2,772
  • 6
  • 28
  • 58
  • Please provide a complete program the demonstrates the problem. Please include the output that you expect, and the output that you actually receive. – Robᵩ Nov 04 '11 at 22:17
  • I have copied this 3 line from main. – S.A.Parkhid Nov 04 '11 at 22:19
  • You never mentioned what platform/compiler you used. Wintel x86 would use native 80-bit floating numbers for `long double`, Sun used to emulate `long double` in software using 128-bits. – Gene Bushuyev Nov 04 '11 at 22:26
  • `long double` is typically 10 bytes long, not 12. 12 or 16 includes padding. The mantissa is 64 bits (but truly 64, there's no invisible 1). – Kerrek SB Nov 04 '11 at 22:29

2 Answers2

2

The correct suffix for long double literals is L:

long double f =79228162514264337593543950336.0L;
K-ballo
  • 80,396
  • 20
  • 159
  • 169
2

What size of long double does your implementation provide (or, equivalently, what value does it show for LDBL_DIGITS)? It's often an 80-bit type with ~20 significant (decimal) digits. Note, in particular, that a floating point type will be divided between a mantissa (significand) and an exponent, so if it's 12 bytes overall, it will not have a 12-bit significand, so you can't expect to see 12 bytes worth of precision.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • how can I found LDBL_DIGITS ? – S.A.Parkhid Nov 04 '11 at 22:17
  • you mean that I have never used 96 bits of double ? – S.A.Parkhid Nov 04 '11 at 22:20
  • 1
    @Parkhid: `#include ` (or, for C++ you can use `#include ` and check `std::numeric_limits::digits10`. I mean a 96-bit floating point type will have a sign bit, something like (say) 16 bits of exponent, and the rest as the mantissa. Thanks to normalization, you may get 1 more bit of precision than the stored mantissa size indicates, but that's about the best you can hope for. – Jerry Coffin Nov 04 '11 at 22:21
  • cout<::digits10; => 15 cout<::digits10; => 18 means what ? – S.A.Parkhid Nov 04 '11 at 22:22
  • digits10 -> Number of digits (in decimal base) that can be represented without change – Bart Nov 04 '11 at 22:24
  • it means that I cannot store this number into a double ? am i true ? – S.A.Parkhid Nov 04 '11 at 22:25
  • @Parkhid: yes and no. You can store a number of that magnitude, but when you do so, it'll be rounded to ~18 significant digits. – Jerry Coffin Nov 04 '11 at 22:27