  lua-l archive

• Subject: Re: Float numbers equality.
• From: KHMan <keinhong@...>
• Date: Tue, 13 Aug 2013 19:34:59 +0800

```On 8/13/2013 6:56 PM, Karol Drożak wrote:
```
```2013/8/13 KHMan [wrote:]

I don't see any comparison anywhere... Where does the equality
comparison fit in?

Here is the code, extracted and simplified from my test case.
This is the case when measuring apparatus gives a normalized value.

--
-- input vales
--
O2 = 12.95
H2O = 100 - (100 / 1.2)
T = 136.575
P = 1013.25 / 1.25
--
O2_r = 10.95
H2O_r = 0
T_r = 273.15
P_r = 1013.25
--
SO2_in = 5.3333333333333
--

--
-- calculations
--

-- correction factor
O2_cf = (20.95 - O2_r) / (20.95 - O2)
H2O_cf = (100 - H2O_r) / (100 - H2O)
T_cf = (T + T_r) / T_r
P_cf = P_r / P
--
print (
O2_cf, O2_cf == 1.25,
H2O_cf, H2O_cf == 1.2,
T_cf, T_cf == 1.5,
P_cf, P_cf == 1.25
)

-- normalized value
SO2 = SO2_in * (O2_cf * H2O_cf * T_cf * P_cf)
print (
SO2, SO2_in * (O2_cf * H2O_cf * T_cf * P_cf),
SO2 == SO2_in * (O2_cf * H2O_cf * T_cf * P_cf)
)

-- inverse operation: from normalized value calculating measured value
-- !!!
print (
SO2_in, 15 / (1.25 * 1.2 * 1.5 * 1.25),
SO2_in == 15 / (1.25 * 1.2 * 1.5 * 1.25)
)
assert (SO2_in == 15 / (1.25 * 1.2 * 1.5 * 1.25))

2013/8/13 KHMan [wrote:]

So, assuming you now know about floating point, do you still
require an equality comparison?

Yes, I think this is the best way to test.
```
```
```
Ah well, your unit tests are never gonna be exact then. Some sort of epsilon thingy should make a reasonable equality tester.
```
```
Floating point has a limited precision and you're accumulating error doing calculations. So make sure your equality tester allows for the imprecision thingy while catching the human errors. Since double precision has 16-17 digits of precision, differentiating the two should not be a problem.
```
--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia

```