[PATCH] MOD of oxygen at pO2 of 1.6 is 20ft

Linus Torvalds torvalds at linux-foundation.org
Sun Jul 5 12:11:15 PDT 2015


On Sun, Jul 5, 2015 at 11:56 AM, Dirk Hohndel <dirk at hohndel.org> wrote:
> static inline depth_t gas_mod(struct gasmix *mix, pressure_t po2_limit, int roundto) {
>         depth_t depth;
>         depth.mm = ((po2_limit.mbar * 1000 / get_o2(mix) * 10 - 10000) / roundto) * roundto;
>         return depth;
> }
>
> So all we need to do is adjust this function to add (roundto - 1) to the
> enumerator before deviding by roundto, correct?

Ugh. I think it would be better to write it more readably first.
That's a particularly unreadable line of noise.

At the very least, split it up, and do the rounding separately. And
no, you shouldn't add "roundto-1" unless you always want to round
_up_. I'd assume you'd want to round-to-nearest, and add "roundto/2"
instead.

Or just use floating point and "rint()".

So something like

     depth.mm = po2_limit.mbar * 1000 / get_o2(mix) * 10 - 10000;
     depth.mm = rint(depth.mm / (double) roundto) * roundto;

might be more readable, and should get the rounding right.

Which just leaves the initial actual depth math, which is also qutie
debatably wrong.

Looking at that first line, it truncates the depth to cm because it
does that "*10" last, after having done a truncating divide. So it
does the math in steps of 10mm, which looks bogus too. Of course, that
math is just hardcoding the "one bar per 10m of water", which might be
a bit questionable to begin with. But I guess that's how people
calculate PO2 limits, so..

                        Linus


More information about the subsurface mailing list