CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2008
    Posts
    8

    Question exponent to decimal conversion fn()

    Hi,
    I have the following program:

    int main()
    {
    float fVar = 1e+11;

    printf("%.10f", fVar);
    return (0);
    }

    When I print fVar, the output is 99999997952.0000000000

    This is understandably because the value 1e+11 is too large to be stored in fVar so it got truncated before getting assigned to fVar.

    But nevertheless if I print it with scientific notation it's prints fine as 1e+11

    Scientific notation:
    ---------------------------
    cout<<setiosflags(ios::scientific) << floatVar<<endl;


    Now I want that I should be able to print 1e+11 as 100000000000 (i.e. 1 follwed by 11 zeros).

    Is there some way I can print the output from fVar as 100000000000?
    Is there some library function that I can use to convert the exponent (1e+11) to decimal and then print it?

    PS: If you don't mind hopping to another Forum related to same discussion, please check out
    http://forums.devx.com/showthread.php?t=167270

    Regards
    Last edited by codekomlete; April 5th, 2008 at 12:18 PM.

  2. #2
    Join Date
    Dec 2006
    Posts
    166

    Re: exponent to decimal conversion fn()

    floating numbers have limited precision
    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    especially float's, which have only about 7(?) digits of precision; double's have about twice that

  3. #3
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: exponent to decimal conversion fn()

    You can't get the result you want with a float. You must use a double.

    A float has approx 7 decimal digits of accuracy. The result you are seeing is 99999997952.0000000000, and it has exactly 7 decimal digits of accuracy. It's the eighth decimal digit (i.e., the "7" following the string of 7 "9's") that has the error. That's what you should expect.

    Try again using a double instead.

    Note that the displayed value of a number is often different from its stored value. The printing routines often apply some rounding rules, so as to obtain a "pretty" result. That's one possible explanation of why the cout result "looks" different than the printf result. Using printf, you might also try the "e" or "g" format flags. Try the following code, to see various results. Note that exactly the same number is being output. The differences you see are strictly formatting differences, i.e., as mentioned above, there's a difference between the displayed value and the actual stored value:
    Code:
    float fVar = 1.0e11;
    printf("float accuracy (30 digits): \n %.30f \n %.30E \n %.30g", fVar, fVar, fVar );
    printf("\n\nfloat accuracy (6 digits): \n %.6f \n %.6E \n %.6g", fVar, fVar, fVar );
    
    double dVar = 1.0e11;
    printf("\n\ndouble accuracy (30 digits): \n %.30f \n %.30E \n %.30g", dVar, dVar, dVar );
    printf("\n\ndouble accuracy (15 digits): \n %.15f \n %.15E \n %.15g", dVar, dVar, dVar );
    
    
    Output:
    
    float accuracy (30 digits):
     99999997952.000000000000000000000000000000
     9.999999795200000000000000000000E+010
     99999997952
    
    float accuracy (6 digits):
     99999997952.000000
     1.000000E+011
     1e+011
    
    double accuracy (30 digits):
     100000000000.000000000000000000000000000000
     1.000000000000000000000000000000E+011
     100000000000
    
    double accuracy (15 digits):
     100000000000.000000000000000
     1.000000000000000E+011
     100000000000
    Last edited by MikeAThon; April 5th, 2008 at 06:32 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured