Float overflow behavior in C

I'm new to C, so far I've only used OOP languages โ€‹โ€‹and haven't had to focus a lot on memory or how bits work and need help that wraps me around what's going on here.

I need to check the minimum and maximum bounds of each datatype in C by initializing a variable with the min / max of the datatype and trying to add or subtract 1. I've gone past integer types and now I'm confused about the behavior of the Float type. A lot of what I've read is a little over my head, I would really appreciate it if someone could help me understand how these types work.

This is what I have for testing Float:

void DisplayFloatMinimumAndMaximum()
{
// Declare variables
float fltMinimum = 0;
float fltMaximum = 0;

// Set min/max values
fltMinimum = -3.4e38;
fltMaximum = 3.4e38;

printf("Float Minimum and Maximum\n");
printf("------------------------------------------------\n");
printf("Float Minimum : %.10e\n", fltMinimum);
printf("Float Maximum : %.10e\n", fltMaximum);
printf("\n");

fltMinimum -= 1.0f;
fltMaximum += 1.0f;

printf("Confirmation\n");
printf("Float Minimum : %.10e\n", fltMinimum);
printf("Float Maximum : %.10e\n", fltMaximum);
printf("\n");
}

      

and I get the result:

Float Minimum and Maximum
------------------------------------------------
Float Minimum : -3.3999999521e+38
Float Maximum : 3.3999999521e+38

Confirmation
Float Minimum : -3.3999999521e+38
Float Maximum : 3.3999999521e+38

      

It looks like nothing happened.
Is this the correct behavior? If so, why? If not, why not?

(I am using Visual Studio 2015)

+3


source to share


2 answers


Due to the nature of floating point data, adding / substituting 1 to such a large number does nothing, because the value is "swallowed" (as Weather noted, before adding the two operands, they must be normalized so that both mantissas are expressed of equal cardinality 2. When this, if the smaller operand rolls down to 0, all of its value is lost)

The value remains the same and you cannot achieve overflow / underload.

http://www.fact-index.com/f/fl/floating_point_1.html

The minimum value added to change the number is determined by the epsilon machine

The best way to play with a limit is not to add, but to multiply by (1+FLT_EPSILON)

(number adds number of times FLT_EPSILON

)

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

int main()
{
 float f = FLT_MAX;

 f *= (1+FLT_EPSILON/2);
 printf("%f\n",f);

return 0;
}

      



this number is printed unchanged. Absorption ignores multiplication.

Now do this:

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

int main()
{
 float f = FLT_MAX;

 f *= (1+FLT_EPSILON);
 printf("%f\n",f);

return 0;
}

      

and the output is

1.#INF00

      

In the case FLT_MAX

on my machine, the limit number added to trigger the overflow goes past 1: this 40564816789451702000000000000000.000000

!

+5


source


Floating point limits

There are macros that define the limits. FLT_MAX

, FLT_MIN

are the maximum and minimum values โ€‹โ€‹for the type , respectively float

.

There are also other macros such as FLT_MAX_EXP

(maximum score defined in terms FLT_RADIX

, which is usually rated at 2) and FLT_MAX_10_EXP

(maximum score defined in base 10).

The smallest number representable in float

is the floating point epsilon defined in the macro FLT_EPSILON

, evaluated in 1E-5

.

These macros can be found in float.h

Accuracy

Note . In this explanation, I will use a radius of 10 for illustration. However, C uses the radius defined by the macro FLT_RADIX

mentioned above (usually evaluates to 2).



Floating point numbers are represented through value and exponent:

1234

= 1.234 x 10^3

In C, single-precision floating point numbers will give you 24 bits of significance and 8 bits for exponent. This means that if your value has more digits than what can be encoded in significant bits, it starts to lose precision.

A very large number will represent a significant and indicator.

1000000000000001

will become 1.000000000000001 x 10^15

This value will be rounded off as it is too large to be encoded in significant bits. Likewise, adding small amounts to this number will also round the sign, so it will not increase.

+2


source







All Articles