C - floating point rounding

I am trying to understand how floating point numbers work.

I think I would like to check what I know / need to know by evaluating the following: I would like to find the smallest x such that x + 1 = x , where x is a floating point number.

As I understand it, this will happen when x is big enough, so x + 1 closer to x than the next number is higher than x represented by floating point. So intuitively it seems that this will be so when I do not have enough digits in the value. Will this number x then be a number where the value is all 1. But then I cannot understand what was supposed to be an exponent. Obviously, it should have been large (relative to 10 ^ 0, in any case).

+6
c floating-point rounding
source share
3 answers

You just need an expression for the LS bit value in the mantissa in terms of exponent. When it is> 1, you will meet your condition. For one precision float, the LS bit is 2 ^ -24 * 2 ^ exp, so the condition would be satisfied if exp> 24, i.e. 25. The smallest (normalized) number where this condition will be satisfied is 1.0 * 2 ^ 25 = 33554432.0f.

I did not check this, so my math may be somewhere (for example, 2 times), and it is also possible that the FP block is rounded beyond the 24th bit, so it may take 2 more factors to explain this, but you get a general idea ...

+5
source share

Start with 1.0 and continue to double it until the test completes successfully:

 double x; for (x = 1.0; x + 1 != x; x *= 2) { } printf("%g + 1 = %g\n", x, x + 1); 
0
source share

I suggest that when you try to understand the numbers fp and fp arithmetic, you work in decimal form with 5 digits in value and 2 in exponent. (Or, if 5 and 2 do not suit you, 6 and 3 or any other small numbers that you like.) Problems:

  • limited set of numbers that can be represented;
  • non-commutative, non-associative and non-differentiable;
  • problems that may arise when processing fp numbers as real numbers;

it’s much easier to figure out the decimal system, and the lessons you learn are completely general. Once you find out, enhancing your knowledge with IEEE fp arithmetic will be relatively easy. You can also easily define other arithmetic systems fp.

0
source share

All Articles