Midrange News for the IBM i Community

Posted by: Bob Cozzi
Rogue Programmer
Cozzi Productions, Inc.
Test for numeric overflow
has no ratings.
Published: 01 Nov 2012
Revised: 23 Jan 2013 - 4138 days ago
Last viewed on: 22 May 2024 (4990 views) 

Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.

Test for numeric overflow Published by: Bob Cozzi on 01 Nov 2012 view comments(3)

Wow! I can't believe the number of numeric overflow operations found in legacy code that is converted to /Free syntax.

This is when you have a target variable that is (for example) defined as 7p2 and the code moves a value to that field that is larger than 5-digits, such as: 99999.00 + 1.00

So much legacy code.... So I'm using the ChkDecOvr()  (check for decimal overflow) subprocedure to first validate if it will fit and then if it won't take an alternate course of action; rather than just blow up. Or I could wrap the code in MONITOR/on-error groups.


    if chkDecOvr( sales * qty : %len(ordTot) : %decPos(ordTot));
       // Value too big to fit in target variable!
       ordTot = sales * qty; // We're good.


Anyway, we've added ChkDecOvr() to the COZTOOLS runtime.


Return to midrangenews.com home page.
Sort Ascend | Descend


(Sign in to Post a Comment)
Posted by: neilrh
Premium member *
Jackson, MI
Comment on: Test for numeric overflow
Posted: 11 years 6 months 20 days 15 hours 31 minutes ago
Edited: Fri, 02 Nov, 2012 at 09:04:18 (4220 days ago)

Yup, just this last week, 2 programs, 2 calcs in each... 

// Calc percentage
  eval(h) Result = (Value1 * 100) / Value2;
  if Value1 < 0 and Value2 < 0 or       
     Value1 > 0 and Value2 > 0;         
    Result = 100;                          
    Result = -100;                         

 But they're breeding like orange barrels on a highway.

Posted by: TFisher
Premium member *
Comment on: Test for numeric overflow
Posted: 11 years 6 months 20 days 15 hours 8 minutes ago

I use the MONITOR approach for the best possible performance.

Posted by: DaleB
Premium member *
Reading, PA
Comment on: Test for numeric overflow
Posted: 11 years 6 months 20 days 14 hours 56 minutes ago

I've also used the Monitor variant, but the chkDecOvr() looks interesting.

When converting existing code, I needed to emulate what the fixed format would have done so the program behaved the same way. Early on I think I did something with %REM(x:SomePowerOf10), but that gets messy when you have non-zero decimal places. A much simpler alternative is to use a DS, OVERLAYing so the decimal points align. In some cases you can do this with Packed, but Zoned is a no-brainer.