Thread: An exercise from K&R

  1. #1
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88

    An exercise from K&R

    Hello all. Here's the subject:
    Exercise 2-1. Write a program to determine the ranges of char, short, int, and long
    variables, both signed and unsigned, by printing appropriate values from standard headers
    and by direct computation. Harder if you compute them: determine the ranges of the various
    floating-point types.
    Okay, 'direct computation' for (unsigned) integer numbers means 2^n where n is a number of bits for type. But what would it look like for floating-point numbers? Also I would very appreciate any good pointers to where I can find out about machine floating-point representation in great detail, since I'm very short in knowledge about it.
    Last edited by GL.Sam; 05-07-2010 at 04:19 AM.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The size of the data type, depends a lot on the OS and compiler you're using. Check your compiler's help pages for the details.

    But can you actually code something that calculates the largest possible range of these data types? I don't mean 2^n or 2^n-1, but actually code up a solution using basic arithmetic?

  3. #3
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    >The size of the data type, depends a lot on the OS and compiler you're using.
    Computing the range of unsigned int:
    Code:
    unsigned range = pow(2.0, sizeof(int) * 8) - 1;
    So what are you trying to say? That it wouldn't work in any envieronment?

    >But can you actually code something that calculates the largest possible range of these data types? I don't mean 2^n or 2^n-1, but actually code up a solution using basic arithmetic?
    It's just that I urge to know about how float is represented internally.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I thought you were going to pull out the MAX_INT type macro's. That equation is a good one.

    I was working on this with a beginner a few months back, and we found out that you can go beyond the range that the MAX_(DATA TYPE), give you, for the larger unsigned one's and for floats and doubles.

    We were both surprised.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. K&R Exercise 1-9
    By Anarchy in forum C Programming
    Replies: 6
    Last Post: 01-10-2010, 12:42 PM
  2. Help with K&R Book Exercise
    By Alejandrito in forum C Programming
    Replies: 5
    Last Post: 03-11-2008, 01:24 PM
  3. K&R Exercise 1-14
    By Lee134 in forum C Programming
    Replies: 3
    Last Post: 02-16-2006, 11:20 AM
  4. Help with K&R Exercise 1.13
    By Yannis in forum C Programming
    Replies: 2
    Last Post: 09-21-2003, 02:51 PM
  5. k&r exercise 1-9
    By xion in forum C Programming
    Replies: 14
    Last Post: 07-15-2003, 06:01 PM

Tags for this Thread