signed vs unsigned

Robert Helling helling at lmu.de
Wed Mar 23 01:53:04 PDT 2016


Dirk,

you committed

commit 59299f0ab34ca4d75e0b02d9a83f23ec6348e1df
Author: Dirk Hohndel <dirk at hohndel.org>
Date:   Wed Mar 9 18:42:39 2016 -0800

    Clean up signedness confusion in planner.c

in which you declared several ints to be unsigned. This breaks at least the ascents in recreational mode, see #1030.

I located the problem to be that we have

unsigned int deltad

in line 1097 but we do the comparison

if (deltad - depth > 0)

in line 1103. Now, depth happens to be signed (and it needs to be since we stop ascending when we step through the surface, i.e. reach negative depth which is of course not possible for unsigned values). This above comparison, however, gets casted to unsigned int and thus clearly makes no longer sense. (You can see the ascent problem goes away when deltad is made signed again.

I could now simply send a patch that does exactly that. But I wonder, what you intended to do with your above patch. It is not the first time I run into troubles when mixing signed and unsigned integers in expressions and in particular in comparisons where the implicit cast is to unsigned (which for me almost never makes sense) and which does not even cause a compiler warning (I think it should, at least it would save me a lot of debugging time, I see, there is -Wconversion).

Anyway, my conclusion from all this trouble in the past was to _always_ use the signed version since this prevents the trouble. As far as I can see, the potential cost is that the maximally representable number is only half the size but this should not be a problem for us since we stay safely away from those limits.

So, could you please explain to me your rationale for adding the unsigned statements in your above patch as I am clearly missing something.

Best
Robert


--
.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oO
Robert C. Helling     Elite Master Course Theoretical and Mathematical Physics
                      Scientific Coordinator
                      Ludwig Maximilians Universitaet Muenchen, Dept. Physik
                      Phone: +49 89 2180-4523  Theresienstr. 39, rm. B339
                      http://www.atdotde.de

Enhance your privacy, use cryptography! My PGP keys have fingerprints
A9D1 A01D 13A5 31FA 6515  BB44 0820 367C 36BC 0C1D    and
DCED 37B6 251C 7861 270D  5613 95C7 9D32 9A8D 9B8F





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.subsurface-divelog.org/pipermail/subsurface/attachments/20160323/2f80c821/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 203 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://lists.subsurface-divelog.org/pipermail/subsurface/attachments/20160323/2f80c821/attachment-0001.sig>


More information about the subsurface mailing list