[Info-vax] VMS Cobol - GnuCOBOL

Arne Vajhøj arne at vajhoej.dk
Fri Mar 3 20:10:43 EST 2023


On 3/3/2023 8:45 AM, Arne Vajhøj wrote:
> On 3/3/2023 7:42 AM, bill wrote:
>> 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.
> 
> 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:
> 
> modulus_math_only_use_if_you_know_what_you_are_doing {
>      // code
> }

Compare:

C (on VMS as this is comp.os.vms !):

$ type ovf.c
#include <stdio.h>

int main(int argc, char *argv[])
{
     unsigned int pop;
     unsigned int gdppercap;
     unsigned int gdp;
     pop = 300000000U;
     gdppercap = 50000U;
     gdp = pop * gdppercap;
     (void)printf("%u people @ %u dollars = %u dollars\n", pop, 
gdppercap, gdp);
     return 0;
}
$ cc/warn=ena:all ovf
$ link ovf
$ run ovf
300000000 people @ 50000 dollars = 1974202368 dollars
$ type bit.c
#include <stdio.h>

int main(int argc, char *argv[])
{
     unsigned int word;
     word = 0xF0F0F0F0U;
     (void)printf("%08X\n", word);
     word = word * 2U;
     (void)printf("%08X\n", word);
     return 0;
}
$ cc/warn=ena:all bit
$ link bit
$ run bit
F0F0F0F0
E1E1E1E0

C# (not available on VMS so ...):

C:\Work>type ovf.cs
using System;

public class Program
{
     public static void Main(string[] args)
     {
         uint pop;
         uint gdppercap;
         uint gdp;
         pop = 300000000U;
         gdppercap = 50000U;
         gdp = pop * gdppercap;
         Console.WriteLine("{0} people @ {1} dollars = {2} dollars\n", 
pop, gdppercap, gdp);
     }
}

C:\Work>csc ovf.cs
Microsoft (R) Visual C# Compiler version 4.3.0-3.22470.13 (80a8ce8d)
Copyright (C) Microsoft Corporation. All rights reserved.

C:\Work>ovf
300000000 people @ 50000 dollars = 1974202368 dollars

C:\Work>csc /checked+ ovf.cs
Microsoft (R) Visual C# Compiler version 4.3.0-3.22470.13 (80a8ce8d)
Copyright (C) Microsoft Corporation. All rights reserved.

C:\Work>ovf
Unhandled Exception: System.OverflowException: Arithmetic operation 
resulted in
an overflow.
    at Program.Main(String[] args)

C:\Work>type bit.cs
using System;

public class Program
{
     public static void Main(string[] args)
     {
         uint word;
         word = 0xF0F0F0F0U;
         Console.WriteLine("{0,8:X}", word);
         word = unchecked(word * 2U);
         Console.WriteLine("{0,8:X}", word);
     }
}

C:\Work>csc bit.cs
Microsoft (R) Visual C# Compiler version 4.3.0-3.22470.13 (80a8ce8d)
Copyright (C) Microsoft Corporation. All rights reserved.

C:\Work>bit
F0F0F0F0
E1E1E1E0

C:\Work>csc /checked+ bit.cs
Microsoft (R) Visual C# Compiler version 4.3.0-3.22470.13 (80a8ce8d)
Copyright (C) Microsoft Corporation. All rights reserved.

C:\Work>bit
F0F0F0F0
E1E1E1E0

(note that /checked+ is not default)

Arne





More information about the Info-vax mailing list