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

    Truncation of Digits with Double

    Hello,

    I have two functions. The output of the first is input to the second. The both return double type numbers.

    From within function one, I print out output of function one and the output of function two.

    In main, I call function one, and using its output, then call function two. Again, I print out the two numbers.

    In both cases the output of the first function appears to 6 decimal places only. The two numbers are identical for the digits presented. However, outputs of the second function are different.

    This leads me to suspect the following: the output of the first function is truncated before being saved by the double created in main. So, the input to the second function is different in the two cases and because of this, the outputs of the second function was different.

    Is this so? Or are there other possibilities for the outputs of the second function being different?

    Also, if it is a matter of truncation, is there a way to ensure that all the digits of double type are used?

    Thanks
    Sid.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Truncation of Digits with Double

    Truncation *only* occurs during outputs, or when a double is cast down to a float. In the output case, the value of the double remains unchanged no matter how many digits are written to the screen.

  3. #3
    Join Date
    Jul 2008
    Posts
    3

    Re: Truncation of Digits with Double

    Thanks Lindley.

    As it turned out, the double number (output of first function) was not truncated (I displayed all 64 digits to check). The problem was that a parameter to the second function was different for the two cases.
    Thats the reason for the different outputs of the second function.

    Thanks again.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Truncation of Digits with Double

    Doubles aren't accurate to 64 digits, FYI. The exact value of a double can be reconstructed with only 18 digits, and it's only accurate to something like 15.

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