Hey thanks alot for the explanation on constant pointers, great stuff. I played with strtod and checked errno for ERANGE and it works, and I also did it the other way with just comparing DBL_MAX and DBL_MIN. And yea Vber its a HUGE number on my computer also lol. About the overflow thing well I think it's something else and not overflow. Cause if I enter this
12345678901234567
I get this when I print it out.
12345678901234568.000000
It's always the 17th digit consistently that the discrepancy begins at. Overflow or Underflow doesn't happen until I enter a ton of stuff, so that works, but the above must not be considered that. Are floating point numbers just not exact after a certain amount of digits?
This is what I was using to test.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include <errno.h>
#include <string.h>
static int readdouble(double *number)
{
int value;
int ch;
if ((value = scanf("%lf", number)) == 1 && (ch = getchar()) == '\n') {
ungetc(ch, stdin);
if (*number <= DBL_MAX && *number >= DBL_MIN) {
return 1;
}
else {
return 0;
}
}
if (value == EOF)
return EOF;
if (value == 0)
{
ungetc(ch, stdin);
return 0;
}
}
int
main(void)
{
double number;
int value;
char array[BUFSIZ];
char *replace;
char *p;
/*
if ((value = readdouble(&number)) == 1)
printf("Valid number %f\n", number);
else if (value == EOF)
printf("end-of-file\n");
else if( value == 0)
printf("Invalid number, overflow, or underflow: %f\n", number);
*/
if (fgets(array, sizeof array, stdin) != NULL) {
if ((replace = strchr(array, '\n')) != NULL) {
*replace = '\0';
number = strtod(array, &p);
if (array[0] != '\0' && *p == '\0') {
if (errno != ERANGE)
printf("Valid number finally ! %f\n", number);
else
printf("We overflowed or underflowed: %f\n", number);
}
else {
printf("Only a newline was entered or we didn't read a number!\n");
}
}
else {
printf("Newline wasn't read!\n");
}
}
else {
printf("end-of-file encountered\n");
}
return (0);
}