Why is the Range of Float in C Language 3.4E+38? Unveiling the Secrets of Floating Point ‘Hidden Bit’ and ‘Golden Rule’

Why is the Range of Float in C Language 3.4E+38? Unveiling the Secrets of Floating Point 'Hidden Bit' and 'Golden Rule'

In learning C language, float (single precision floating point) and double (double precision floating point) are two unavoidable hurdles. Many students memorize their value ranges—such as float being approximately ±3.4E+38—but do not understand where this astronomical number comes from.

Today’s lesson is an “elective content,” but it is highly valuable. We will dive into the computer’s underlying mechanisms to uncover the secrets of floating point ranges. Mastering this will help you understand why banking systems strictly prohibit the use of floating point numbers, and how to choose in actual development.

Are you ready? Let’s get started!

01

Hands-on Practice: Definition of Float and Double

Before delving into the principles, let’s first look at how to declare and use them in C language.

1. Code Declaration

In C language, when declaring a float type, it is customary to add an ‘f’ or ‘F’ suffix to the number to clearly inform the compiler that this is a single precision floating point number.

#include <stdio.h>
int main() {
    // Declare a float variable, must add 'f' suffix
    float temperature = 36.5f;
    // Declare a double variable (decimal is double by default in C)
    double precise_pi = 3.141592653589793;
    printf("体温: %.1f°C\n", temperature);
    printf("圆周率 (double): %.15f\n", precise_pi);
    return 0;
}

2. Core Differences: Space and Precision

As pointed out by Microsoft’s official documentation, the main difference between float and double lies in the memory occupied and the precision/range they can represent:

float (single precision): occupies 4 bytes (32 bits). Effective precision is about 6-7 decimal digits.

double (double precision): occupies 8 bytes (64 bits). Effective precision can reach 15-16 digits.

double is like a “Double Kill,” providing more space to store more precise numbers.

02

Exploring the Underlying Mechanism: How is 3.4E+38 calculated?

Now, let’s uncover the secret behind that “astronomical number.” This is thanks to a great standard: IEEE 754 (Institute of Electrical and Electronics Engineers standard).

A 32-bit float is actually divided into three parts:

  • Sign Bit (1 bit): determines the sign (positive or negative).

  • Exponent Bit (8 bits): determines the “magnitude” of the value.

  • Fraction Bit (23 bits): determines the “precision” of the value.

The secret of the range lies in the “exponent bit” and the “fraction bit.”

1. Fraction: The Magical “Hidden Bit”

When using decimals, we usually deal with numbers greater than 1 (like 3.14). The IEEE 754 standard cleverly utilizes this:

It stipulates that for normalized floating point numbers, the leading 1. is hidden!

This is like a ruler; we know the scale starts from 0, so we don’t need to write it out every time. By hiding this “1,” we effectively gain 24 bits of precision using the 23 bits of space. This is a fantastic optimization!

2. Exponent: The Decider of Range

The vast range of float is crucially dependent on that 8-bit exponent. 8 bits can represent 256 states.

To represent both very large and very small numbers, the standard introduces the concept of “bias.” For float, the bias is 127.

This means that the maximum exponent that float can represent is approximately 2^(255 – 127), which is 2^128. Ultimately, the maximum value is around 3.4E+38.

03

Code Verification: Limits of Float

“Words are not enough; code is the proof.” The C standard library <float.h> defines these limit values, and we can print them out directly to see.

#include <stdio.h>
#include <float.h>
int main() {
    // Verify the maximum value of float
    printf("Float 类型的最大值 (FLT_MAX): %e\n", FLT_MAX);
    // Verify the maximum value of double
    printf("Double 类型的最大值 (DBL_MAX): %e\n", DBL_MAX);
    // See how small the smallest positive float can be (close to 0)
    printf("Float 类型的最小正非零值 (FLT_MIN): %e\n", FLT_MIN);
    return 0;
}

Example of running results:

Float 类型的最大值 (FLT_MAX): 3.402823e+38
Double 类型的最大值 (DBL_MAX): 1.797693e+308
Float 类型的最小正非零值 (FLT_MIN): 1.175494e-38

Golden Rule: Choices in Development

Having understood the principles, it becomes very clear how to choose between float and double in actual development. Here are some key points:

Summary of Practical Development Experience:

  • If storage space is more important (e.g., embedded systems, massive data): prioritize float (4 bytes).

  • If computational precision is the primary concern (most cases): must use double (8 bytes).

Why don’t banks use floating point numbers?

Because both float and double can have slight errors when representing certain decimal fractions (like 0.1). These errors can accumulate, leading to the terrifying consequence of “money not being calculated accurately.”

04

Summary:

Today we delved into the underlying structure of floating point numbers, learned about the origin of 3.4E+38, and understood the basis for choosing between float and double.

Core Points Recap:

  • The range of float is determined by the 8-bit exponent, achieved through bias.

  • The fraction has a “hidden 24th bit,” improving precision utilization.

  • Choose double for precision, choose float for space.

So, how does floating point “precision loss” actually occur? What do banking systems use to replace it?

Why is the Range of Float in C Language 3.4E+38? Unveiling the Secrets of Floating Point 'Hidden Bit' and 'Golden Rule'

Leave a Comment