The first cycle determines the number of bits contributing to the value by finding the least power 2, so adding 1 to it (using floating point arithmetic) cannot change its value. If the value of n th is equal to two, then the value uses the bit n , because with n bits you can express all integers from 0 to 2 ^ n - 1, but not 2 ^ n . Therefore, the floating point representation 2 ^ n should have the exponent large enough so that the number of (binary) units is not significant.
Thus, having found the first degree 2, whose representation of float worse than unit precision, the maximum value of float , which has unit precision, is less than unity. This value is written to the flt_m variable.
Then the second cycle checks the maximum indicator, starting with the maximum value of the unit of measure and repeatedly increasing it (thereby increasing the indicator by 1) until it finds that the result cannot be converted back by halving it. The maximum float value is the value before this final doubling.
Note, by the way, that all of the above assumes a base-2 floating point representation. You are unlikely to encounter anything else, but C does not actually require any specific representation.
Regarding the second part of your question,
Does this program become more accurate as you need to find longer and non-integer numbers?
the program will take care to avoid loss of accuracy. It accepts the binary floating point representation that you described, but it will work correctly, regardless of the number of bits in the value or exponent of such a representation. Non-integer numbers are not involved, but the program already has numbers that are worse than unit precision and with numbers larger than can be represented by int .
source share