lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] MSVC warnings


From: Greg Chicares
Subject: Re: [lmi] MSVC warnings
Date: Mon, 6 Jun 2022 13:16:48 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0

On 6/6/22 03:37, Greg Chicares wrote:
> On 6/5/22 15:22, Vadim Zeitlin wrote:
> [...]
>> math_functions.hpp(149,1): warning C4146: unary minus operator applied to
>> unsigned type, result still unsigned

"Unary additive-inverse operation applied to a value for whose type
arithmetic is modular. This does not cause the type to change, or
the arithmetic to become other than modular. If you don't understand
modular arithmetic, then don't use modular types; if you do, then
turn off this warning."

> The logic of that message does seem compelling.

No it doesn't.

For   real  arithmetic, '-' is the   real  additive inverse.
For complex arithmetic, '-' is the complex additive inverse.
For modular arithmetic, '-' is the modular additive inverse.

The essential meaning of the unary '-' operator for integers is
"additive inverse", no matter how language standards describe it.

The essence of types that C calls 'unsigned' is not that they lack
an algebraic sign or a physical sign bit, but that they obey modular
arithmetic. The standard may describe the effect of operator-(int)
in terms of a negation performed as if in a wider type, or of a
bitwise complement offset by one, but those are just artificial
constructs for performing the modular-inverse operation.

The whole point of
    return (t < 0) ? -static_cast<U>(t) : static_cast<U>(t);
is to return the signed value 't' unchanged if it's positive,
else to return its modular-additive inverse. It's already perfect.

>> I am testing this with is the last line of the u_abs() function) and I
>> think it's a pretty useful warning that it would be a pity to disable
>> because it I already wrote incorrect code that was flagged by this warning
>> even if in this particular case it's almost certainly done intentionally
>> and works as intended.

If you like, we could surround that statement with an msvc
pragma to emphasize that we mean exactly what we say, which is
exactly what the standard promises.

> Off the top of my head

Please ignore the horrible idea I had proposed there.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]