0
int i;
long double y=1,z=10;
for(i=1;i<=100;i++)
{
  y=y*z;
}
  printf("%Lf\n",y);

segment of code in c gives output-> 10000000000000000000617922327239436892408592529019667420405379256132946170093880852611802198127411200.000000

Also when i try to initialize it like this y=10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0; on printing it does not give correct ans. instead of right ans.please help me in this.

3 Answers3

4

A 'long double' does not have the precision to accurately represent a 100 digit number. So what you are seeing is 'the right answer' - long doubles are typically 80 to 128 bits with precision around 20+ or so decimal digits, nowhere near 100.

pvg
  • 2,673
  • 4
  • 17
  • 31
  • i didn't know what precision means actually – Amogh Mishra Jul 12 '15 at 05:32
  • so we can only get near about 10^20 correctly using long double?? – Amogh Mishra Jul 12 '15 at 05:33
  • @AmoghMishra it's actually implementation-defined. on x86, which you're likely using, you're going to get something closer to 18-19. – pvg Jul 12 '15 at 05:37
  • @AmoghMishra, remember scientific notation? That's floating-point decimal. This is floating-point binary. Precision refers to the number of digits you are reporting. With a long double, it's about 20. So you can represent a number (almost) arbitrarily high, but you will only get the 20ish most significant digits exact; everything after that will be inexact. Which is exactly the behavior you're seeing above. – acwaters Jul 12 '15 at 05:41
  • thanks.can you guys tell me what is precision limit in long double for digits after decimal points? – Amogh Mishra Jul 12 '15 at 05:56
  • @AmoghMishra there's no such thing. That's actually why floating point numbers are called 'floating point'. The decimal point can be anywhere and there's set number of bits (and thus precision) allocated to the 'mantissa' or 'significand' and to the exponent. It's a representation that, roughly speaking can represent any number +/-DDDDDDD E+/-XXX where the number of Ds and Xs depends on the size/type of floating point representation. This is a highly over-simplified explanation but it should give you an idea of what you're dealing with. – pvg Jul 12 '15 at 06:02
  • if we have two long doubles k=0.7896, j=0.0354 we are multiplying k with j for lets say n times and store the result in k like this k=k*j; from which minimum value of n it will start giving wrong ans(that is not accurate according to mathematics)? – Amogh Mishra Jul 12 '15 at 06:11
  • @AmoghMishra at this point you're probably better off reading some introductory material on floating point arithmetic and then writing new questions. The general answer for 'if i keep iterating floating point operations, how quickly do I end up with numbers that cannot be perfectly represented by a float' is 'very quickly'. – pvg Jul 12 '15 at 06:28
1

The precision of long double is implementation defined, you need to check your system for the precision of long double. However, 10^100 is way too large to be stored without loosing precision.

Raman
  • 2,735
  • 1
  • 26
  • 46
0

The precision of numbers in computer science varies as normally you cannot represent a real number with a finite representation in decimal (or binary) form. There are two ways of representing real numbers in approximate form. The first is fixed point numbers, where you deserve a number of bits for the integer part and some for the fractional part. The only fixed point number supported in C are integer (with 0 fractional bits) If you want more, you have to manage yourself.

The other class of real numbers is floating point... in this class you have relative error (it depends on the magnitude or how big the number is, the error you have) so... explained in decimal, when you have five digits for the mantissa, you'll get a 0.00001 * the_number error or 0.001 % error.

If you try to represent 10^100 as an integer, you'll need around 300 bits to represent such a big number, so it's impossible to do it exactly with the basic types allowed in C (even a long long long, case it should be supported, is too small to allow such a number). If you try to represent it with the normal double type, you'll get an error (0.000000000000001 % over the magnitude of the result, of error)

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31