[Info-vax] VMS Cobol - GnuCOBOL
Johnny Billquist
bqt at softjar.se
Mon Mar 6 08:28:04 EST 2023
On 2023-03-03 14:45, Arne Vajhøj wrote:
> On 3/3/2023 7:42 AM, bill wrote:
>> On 3/3/2023 6:59 AM, Johnny Billquist wrote:
>>> On 2023-03-01 15:54, Dan Cross wrote:
>>>> In article <ttnk0m$lg5$2 at news.misty.com>,
>>>> Johnny Billquist <bqt at softjar.se> wrote:
>>>>> Basically, if you multiply two numbers, and the result would be bigger
>>>>> than the types involved, the result is undefined.
>>>>
>>>> Well...no. _unsigned_ integer overflow in C is well-defined (it
>>>> has modular wrapping semantics; C11 sec 6.2.5 para 9).
>>>> Similarly, overflow of signed atomics is well-defined (C11 sec
>>>> 7.17.7.5 para 3), so this is not always true.
>>>
>>> I had actually missed that unsigned integers do have a defined
>>> overflow behavior.
>
>>> Yup. You were right. Sorry that I had to go and read the spec again.
>
>> And it really doesn't matter whether the result is truncation,
>> conversion to signed int, wrap around, throwing an exception or
>> undefined. The answer is still going to be wrong and the calculation
>> worthless. So, what's the point? Fix the damn code.
>>
>> Go ahead, blame the language again.
>
> I do.
>
> :-)
>
> The choice that provide the safest code is to throw
> an exception at integer overflow.
C don't have exceptions. And it can be argues if that is safe as well.
> For the cases where modulus 2^n math make sense, then
> either the developer would need to move data to a bigger
> data type and do an explicit modulus or the language
> could support:
The sad thing is that C is basically saying that this should be done
with 2^n modulus math. However, the rule saying that a unsigned short
can be propagated to a signed int is fucking you over. Technically, it's
ok since a signed int can fit all values that an unsigned short can.
However, a signed int is not defined to have a 2^n modulus behavior, so
in essence, behind your back, the compiler changed to a type with a
different behavior which invalidates something you expected/were
promised by the language.
Johnny
More information about the Info-vax
mailing list