I'm trying to determine the minimum range of floating-point types.

It's easy when using values from standard headers:

#include <stdio.h>
#include <float.h>

    printf("Minimum range of float variable: %e\n", FLT_MIN);
    printf("Minimum range of double variable: %e\n", DBL_MIN);
    return 0;

This code gives the following output:

Minimum range of float variable: 1.175494e-038
Minimum range of double variable: 2.225074e-308

How do I get to these same values using only direct computation?

5 Years
Discussion Span
Last Post by Adak

It's quite easy actually. ;)

The key is, when you exceed the maximum of a data type SOMETHING is going to go haywire. When you exceed a positive integer, it might go negative (or do something else).

A simple while loop (or for loop) will get you "close". Just keeping multiplying a test number of that data type, by 2. When you are "close", you might want to get even closer by using a smaller number, say 1.4, etc.

But you have to find out what "haywire" is for your compiler, and recognize it, in your code. When I did this, I found I could go quite a ways past that limit in the header, before it went "bonkers", with the number.

Don't be put off by the huge size of the floating point range - you multiple by 2 and you'll be as far as you want to go with x 2, pretty quickly.

Mine went x2, for N times, then another loop took the number x 1.6, for N2 times, then a third loop took the number x 1.4, and finally addition was used to zero in the maximum value (which was well beyond the value in limits.h).

I don't want to spoil it by posting my code for it - it's a great exercise to build up your problem solving skills.

Votes + Comments
Thanks, this helped me a lot.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.