strtod and loss of precision

Comments

8 comments

  • Avatar
    Paul Curtis

    strtod will generate a double for you.  Make sure you are not compiling with "Treat Doubles as Floats" set in Code Generation Options for the project.

    0
    Comment actions Permalink
  • Avatar
    Lakshmi Mannem

    I've checked my project settings and "Treat Doubles as Floats" is set to false.

    For example,

    When I convert  char* coeff = "0.0006105080923458"   to double using  strtod,

    double temp = strtod(coeff, NULL);

    I see temp as  0.000610508

    0
    Comment actions Permalink
  • Avatar
    Paul Curtis

    > I see temp as  0.000610508

    In the debugger?

    0
    Comment actions Permalink
  • Avatar
    Lakshmi Mannem

    Yeah. Thanks

    0
    Comment actions Permalink
  • Avatar
    Paul Curtis

    Change Tools > Options > Debugging > Display Floating Point Number In?

    What is displayed is rounded as appropriate without excess precision.  If you want to know what it is exactly, take a look at its binary representation.

    0
    Comment actions Permalink
  • Avatar
    Lakshmi Mannem

    Hi Paul,

    That worked.

    Thanks for your help.

    0
    Comment actions Permalink
  • Avatar
    Lakshmi Mannem

    I encountered another problem when testing doubles. Here is my code,

    char *  temp  = "0.0005961726048061494"

    I converted above string to double using strtod, it showed in debugger as 5.961726048061e-004

    and when I converted back to string using sprintf with format parameter "%.15le",

    I see the string as 5.96172604806150e-04.

    Could you please let me know why the last digits are rounded and missing out?

    Thank you.

     

     

    0
    Comment actions Permalink
  • Avatar
    Paul Curtis

    ...because that's the format you asked for...  Why not try reading the ISO standard or a reference book on C?

    0
    Comment actions Permalink

Please sign in to leave a comment.