Thread: floating point in c

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    5

    floating point in c

    sir how we can calculate the range of floats and doubles.......
    as we can calculate ints and chars.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is a standard header <limits.h> that provides macros like INT_MAX that represent the maximum value of an int. No need to calculate the maximum value of an int at all - your compiler and library are not standard compliant if they don't provide that header.

    Similarly there is a standard header <float.h> that provides properties of floating point types. In that header FLT_MAX and DBL_MAX provide the maximum value of a float and double respectively.

    Each of those headers provide a means of finding other information about their respective types as well.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    5
    i want to know the calculation of ranges as float ranges from -3.4 e38 to +3.4 e38 how can we calculate this.........

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    My point is, you don't calculate it. Repeating your question doesn't change that.

    To calculate the maximum value of a floating point variable, you somehow need to detect when it overflows (i.e. detect when you do some operation that attempts to exceed its maximum). The problem is, the result of a floating point overflow is undefined behaviour (anything can happen, including abnormal program termination). The means of detecting a floating point overflow and recovering from it are compiler specific - and not all compilers provide a facility to do that.

    You therefore need to make use of information provided by your compiler/library. And that is exactly the information supplied in the <float.h> header. The compiler vendor knows EXACTLY how their compiler (or the host system) represents float and double, and can precompute values such as the maximum for you. Unless you have access to such information, you can't do that computation. And, in standard C, that other "such information" is also provided in <float.h>.

    Incidentally, contrary to your assertion in your first post, you can't compute the maximum value of a signed int either, without reference to one or more values specified in <limits.h> (overflowing or underflowing a signed int also yields undefined behaviour). You can can do the calculation for unsigned integral types (since the standard specifies they work by modulo arithmetic - adding one to the maximum yields the minimum, etc).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    We can calculate maximum values by understanding the way floating point numbers are represented. 3.4e38 is approx. 2^128.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. floating point help!
    By NoobieGecko in forum C Programming
    Replies: 7
    Last Post: 03-09-2008, 02:51 PM
  2. Floating point
    By anirban in forum C Programming
    Replies: 4
    Last Post: 08-16-2007, 07:11 AM
  3. Floating point
    By Flip in forum C++ Programming
    Replies: 7
    Last Post: 12-05-2005, 12:29 PM
  4. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM