C language > Beginner questions
atof
Grincheux:
The function atof rouds my results!
I have strings with 15 numbers after the decimal point and I can't get correct double or float numer.
Is there a solution (without writing my own).
Grincheux:
I had a best result using strtold
--- Code: --- *(_lpszDatas + 17) = '\0' ;
// _dJulianDate = atof(_lpszDatas) ;
_dJulianDate = (double) strtold(_lpszDatas,NULL) ;
--- End code ---
Grincheux:
I suppose that Pelle make the same thing:
--- Code: ---/* Copyright (C) 1991-2019 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
#include <stdlib.h>
#undef atof
/* Convert a string to a double. */
double
atof (const char *nptr)
{
return strtod (nptr, (char **) NULL);
}
--- End code ---
John Z:
Hi Grincheux,
Not that I'm qualified to speak to this but I am aware that there was a bug report on using more than 11 or 12 digits after the decimal. I think the suggestion was to use the advance math lib for now.
John Z
Pelle:
Maybe there is a problem somewhere, I havn't really hunted for anything yet, but there could also be the common case of having to high expectations about the binary floating-point format in general. There are only so many decimals you can expect to be correct.
If you have trouble sleeping, this may (or may not) help:
"What Every Computer Scientist Should Know About Floating-Point Arithmetic"
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
Also, like Microsoft, there is no (at least shouldn't be any) difference between double and long double type.
Navigation
[0] Message Index
[#] Next page
Go to full version