This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Problem with scanf reading long types in scientific notation


Try strtold() perhaps.  It happily reads all the formats (15892938475,
1.5892938475E10 etc) you asked for.
If the significand part of the float is as large or larger than the
int type, then you can convert the result to an int type with no loss
of precision.  80 bit long double (common on x86) has a 64 bit
significand which can (and was designed to) store a 64 bit integer.
Double has a 53 bit significand which can safely hold a 32 bit
integer.

for example:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
int
main( int argc, char *argv[] )
{
  char *endp;
  long double x = strtold( argv[1], &endp );
  if( endp == argv[1] || x > LLONG_MAX || x < LLONG_MIN )
  {
    printf("bad or out of range number\n");
    exit(1);
  }
  long long int y = llround(x);
  printf("y = %lld\n", y );
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]