Thread: Few Questions

  1. #1
    .........
    Join Date
    Nov 2002
    Posts
    303

    Few Questions

    Hi I have a function that reads a double and I'm wondering how can i check for overflow and underflow? Like say I have double x; and theres a value stored in x that I want to check. I tryed comparing it to DBL_MAX and DBL_MIN but it doesn't work.

    Also another question
    I ran into this
    Code:
    const char * const  x;
    and
    char const *x;
    I read that these both declare the pointer x to be a constant and that the top one declares the value pointed to by x as constant. I understand the part about the value being constant but the part about the pointers being constant confuses me. What does that mean, that the pointer is constant and that its memory address doesn't change? If so is it good to declare pointers like this if I don't plan on changing their address? Just seems weird cause I've never seen that before.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    about your double question, use float.h to discover DBL_MAX;
    Code:
    #include <float.h> /* for DBL_MIN and DBL_MAX */
    #include <stdio.h>
    
    int main( void ) {
    	double db1 = 2500.2000;
    	int i;
    
    	for( i=0; i<50; i++)
    	{
    		db1 *= 5000000000.22;
    		if (db1 >= DBL_MAX)
    			puts( "overflow" );
    		else
    			puts( "still ok" );
    	}
    
    	return 0;
    }
    Sorry about the code, is dumbass but DBL_MAX in my system is gicantic huhauah, same thing with DBL_MIN;

    char* const declares a constant pointer which has both read and write access to a character (or character array). The pointer itself is a constant and you can not change it. Like all other constant variables, you must initialize it with a constant value at the same time when it is declared:

    const char* declares a pointer to a constant character (or a constant character array). The pointer can be changed, but the character (or array) to which it points can not be changed.

  3. #3
    .........
    Join Date
    Nov 2002
    Posts
    303
    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);
        
    }

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    >>Are floating point numbers just not exact after a certain amount of digits?

    Yes, with only 8 bytes available on most systems, there is some exponential equation, lol, i dont know it off hand but you only get about 17 precise number places.

    --edit--
    Did a search on google and found the exponential form: 1.7E+308. A good explination of the data types can be found here.
    Last edited by stumon; 04-16-2003 at 03:56 PM.
    The keyboard is the standard device used to cause computer errors!

  5. #5
    .........
    Join Date
    Nov 2002
    Posts
    303
    Hey stumon thanks

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    You are welcome.
    The keyboard is the standard device used to cause computer errors!

  7. #7
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    >>12345678901234567
    >>I get this when I print it out.
    >>12345678901234568.000000

    He is having precision problems.....I shouldn't have been confusing him.
    The keyboard is the standard device used to cause computer errors!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM