Recently I was implementing a validation checking that a number entered by user fits into a configured round-lot. And I decided to write few blog posts about it. In the previous post I described the problem and created naive implementation that didn't work in some cases. And I presented implementation that should handle those cases, but there is unanswered question about `acceptedError`

.

## Error of floating-point numbers

It was explained that a floating-point number may represent a nearby value. For example, binary floating-point number \(1.00011001100\) may represent decimal number 1.1. Actually there exist multiple real numbers, which would be represented by the same floating-point number with specific precision, because there is much more real numbers than floating-point numbers. It is possible to define relationship between the floating point numbers and real numbers that floating-point significand \(s\) represents interval of real numbers

where \(k\) is number of bits used to store the significand. We can also say that a floating-point number represents any real number \(v\) that satisfies following inequation

And including exponent it would be

## Error of division

Now after understanding of relationship between floating-point numbers and real numbers we can get back to our problem. The problem is to find out if there exists any integer number \(r\) that

But what is the \(\epsilon\)? Is it possible to calculate it or estimate it? Yes, it is. Before that, however, I have to present a lemma.

**Lemma 1**: For any positive real numbers \(x\), \(a\), \(b\), and integer \(n\), when \(a \geq 2^{n}b > 0\) then

Proof:

And that is the precondition. So following the implications in reverse order is the proof of Lemma 1.

With this small lemma in our toolset, it's possible to find out the \(\epsilon\) we are looking for. At first let's summarize, what we know about the numbers.

- Real number \(u\) is represented by floating-point number \(u_f = s_u 2^{e_u}\)
- Real number \(c\) is represented by floating-point number \(c_f = s_c 2^{e_c}\)
- \(1 \leq s_u < 2\)
- \(1 \leq s_c < 2\)
- \(\left( s_u - 2^{-k-1} \right) 2^{e_u} \leq u \leq \left( s_u + 2^{-k-1} \right) 2^{e_u}\)
- \(\left( s_c - 2^{-k-1} \right) 2^{e_c} \leq c \leq \left( s_c + 2^{-k-1} \right) 2^{e_c}\)
- \(u \geq c > 0\); if \(u\) would be lower than \(c\) it's certain that there is no integer \(m\) that \(u = mc\)

And let's define \(r = \frac{u}{c}\). Then following inequation is true

This is because the lowest possible \(r\) is the lowest possible \(u\) divided by the highest possible \(c\). And the same way the highest possible \(r\) is the highest possible \(u\) divided by the lowest possible \(c\).

Let's focus on the left side of the inequation.

Now we use Lemma 1 from our toolset. We substitute: \(x = s_u 2^{e_u}\), \(a = s_c 2^{e_c}\), \(b = 2^{-k-1} 2^{e_c}\), and \(n = k+1\). Keep in mind that \(s_c \geq 1\), and therefore \(a \geq 2^{k+1}b\).

And we know that \(s_u - 1 < 1\) and also \(s_c + 2^{-k-1} > 1\). Therefore,

We just proved that

And in very similar way it is possible to prove that

As some of my professors used to say: “We leave this proof as a homework for reader.”

Some of you may already see that, but to be clear, let's expand this expression.

Now it seems like dividing of two floating point numbers does not loose any precision. That it's possible to simply divide \(u_f\) by \(u_c\) and check if it is integer or not, exactly like we did with real numbers. However, that is not exactly true.

## Rounding off error

\(\frac{s_u}{s_c}\) is rational number. It may require more than \(k\) bits to be expressed exactly. Actually as rational number it may require infinite number of bits to be expressed exactly. But we have only \(k\) bits. And that is the point, where the precision is lost. But how much precision can be lost? Let's have a look at floating-point result of expression \(\frac{u_f}{u_c}\). The result should be

where \(f\) is a function that converts rational number to close enough floating-point number with significand of \(k\) bits. And what is precision of that function? That depends on the implementation of the function. The best scenario is that the result is rounded to the nearest value of \(k\) bits.

Or similar option is

Then the lost of precision is less than \(2^{-k-1}\)

There are 2 cases. When \(\lfloor x2^k + 2^{-1} \rfloor = \lfloor x2^k \rfloor\) then difference between \(\lfloor x2^k + 2^{-1} \rfloor\) and \(x2^k\) is less than \(\frac{1}{2}\). And when \(\lfloor x2^k + 2^{-1} \rfloor = \lfloor x2^k + 1 \rfloor\) then difference between \(\lfloor x2^k + 2^{-1} \rfloor\) and \(x2^k\) is also less than \(\frac{1}{2}\).

An attentive reader would notice that we didn't talk about normalization and its impact on the precision. Good thing is that it doesn't have any negative impact. When \(s_u \geq s_c\) then \(\frac{s_u}{s_c} \geq 1\), and thus the result is already normalized. When \(s_u < s_c\) then \(1 > s_r \geq \frac{1}{2}\) and it is possible to add one more bit to \(s_r\) with precision \(2^{-k-2}\). We can also look at it another way. Value of rounded significand is \(f \left( 2 \frac{s_u}{s_c} \right)\) and value of exponent is \(e_u-e_c-1\). Then the precision is \(2^{e_u-e_c-k-2} = 2^{e_r-k-1}\).

And that is true, because for any \(x\) (including \(2 \frac{s_u}{s_c}\)) following inequation is true

## Final error

And finally we should have a look at the relationship between floating-point result of division and integer that should exist, when the input is valid. This is, what we know about the floating-point result of division:

And we want to find an integer \(r\) that satisfies

We can take the highest possible \(r\) and the lowest possible \(r_f\) (or the other way around the lowest possible \(r\) and the highest possible \(r_f\)). Then it is easy to deduce that

## Implementation

We finally calculated that `acceptedError`

is \(2^{e_r-k}\). \(e_r\) is exponent of the floating point number and that can be extracted using Math.ILogB function. And \(k\) is precision of Double and that is 52 bits.

So the full function is

```
static bool IsValidRoundLot(double value, double roundLot)
{
value = Math.Abs(value);
roundLot = Math.Abs(roundLot);
if (value < roundLot)
{
return false;
}
var ratio = value / roundLot;
var integerRatio = Math.Round(ratio);
var acceptedError = Math.Pow(2, Math.ILogB(ratio) - 52);
return Math.Abs(ratio - integerRatio) <= acceptedError;
}
```

Same implementation can be done in other programming languages as functions `Abs`

, `Round`

, and `Pow`

are pretty standard. And the language should use IEEE 754 floating-point number data type with known precision. The only non-standard function is `ILogB`

. However, it is also possible to use:

```
var acceptedError = Math.Pow(2, Math.Truncate(Math.Log2(ratio)) - 52);
```

or

```
var acceptedError = Math.Pow(2, Math.Floor(Math.Log2(ratio)) - 52);
```

The function doesn't handle some exceptional cases, for example:

- Value or round-lot is positive or negative infinity or NaN
- Round-lot is 0 or very near zero.
- Ratio is positive infinity, because the round-lot is very small number and the value is very large number.

However, handling of these inputs depends on your application.

This post presented accurate implementation of the round-lot validation. In the next post I will have a look, how does this work with Decimal data type.

**Note**: It is also possible that the rounding function simply truncates bits that do not fit into the significand. Then the rounding function would be something like

Then precision of \(s_r\) would be \(2^{-k}\). And final range for our integer would be

In such case `acceptedError`

would be

```
var acceptedError = Math.Pow(2, Math.ILogB(ratio) - 51);
```

This should cover platforms and architectures, where round-off error can be higher than just 1 bit. Especially in cases, when your application can accept lower precision. Although, I am not an expert on IEEE 754 standard and its implementations, so I am not sure if such problematic implementation exists.

**Note 2**: This post defined the problem in space of real numbers and explained the relationship between real numbers and floating-point numbers. It didn't use any root operations or any functions not compatible with rational numbers. And thus, the post applies also for definition of the problem in space of rational numbers and for this problem the same relationship applies to rational numbers and floating-point numbers.