Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute: Warn when low-order truncation occurs

Tags:

cobol

I have a result field specified as

01 MY-RESULT VALUE +0 USAGE COMP-3 PIC S9(13)V99

Imagine I multiply two factors:

COMPUTE MY-RESULT = A * B

What is the best way to detect low-order truncation in MY-RESULT? E.g. when A=B=2.01.

The ON SIZE ERRORclause is not triggered.

like image 509
Klaus Schulz Avatar asked Mar 06 '26 20:03

Klaus Schulz


1 Answers

Something that will work with all vendors and even the oldest compilers (as you did not specified any dialect the seems to be the most important part): if it matters use an additional target field with more decimal positions and check for equality afterwards:

COMPUTE MY-RESULT RESULT-WITH-MORE-DECIMALS = A * B
IF MY-RESULT NOT = RESULT-WITH-MORE DECIMALS
   ...
END-IF

ON SIZE ERROR will only be tracked for the upper truncation.

If this 2014 feature os available for your compiler you could use the INTERMEDIATE ROUNDING IS PROHIBITED (the draft had it in as ROUNDED-MODE PROHIBITED) which will show you this problem (if EC-SIZE-TRUNCATION exception is enabled). Beware of one part: this is an exception with a "fatal" category...

like image 133
Simon Sobisch Avatar answered Mar 10 '26 14:03

Simon Sobisch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!