Strange unit conversions

Dirk Hohndel dirk at hohndel.org
Wed Feb 20 01:04:02 PST 2013


jukka.lind at kolumbus.fi writes:

> Pressure unit conversions in dive.h seem strange to me.
> What is the meaning of  "0.5" in psi_to_mba and int to_PS ? If they're there to minimize rounding error when moving from integer to real, shouldn't they be before the multiplying ?
> The error at normal tank pressures is minimal and at 1 bar just about 3 %, but anyhow.
>
>  191 static inline unsigned long psi_to_mbar(double psi)
>  192 {
>  193         return psi_to_bar(psi)*1000 + 0.5;
>  194 }
>  195 
>  196 static inline int to_PSI(pressure_t pressure)
>  197 {
>  198         return pressure.mbar * 0.0145037738 + 0.5;
>  199 }
>

Think about it - that's classic rounding. We multiply some random
floating point number and then convert to int. C simply truncates. By
adding 0.5 values after the decimal point of 0.5...0.99999 will get
rounded up, valiues < 0.5 will be rounded down.

So this actually minimizes the error. And since we are rounding to mbar,
on your typical 200bar tank that gives you a maximum error of 0.00025%

Let's not talk about the likelihood that your pressure measurement is
that accurate :-)

> Sorry I haven't the "development environment" set up yet.
> Signed-off-by: Jukka Lind <lino at iki.fi>

You don't need to Sign-off emails :-)

/D


More information about the subsurface mailing list