IB - Developer Center    Forums  Hop To Forum Categories  FOCUS/WebFOCUS    [CODE] Rounding packed fields
Go
New
Search
Notify
Tools
Reply
  
-star Rating Rate It!  Login/Join 
Platinum Member
Posted
In FOCUS for VMS, I have two P15.4 fields that need to be compared to see if they are equal. What's the best way to round them up to 2 decimal places for this comparison?

For example, if FIELD_A = 200.2149 and FIELD_B = 200.2200, then after the rounding they should be considered equal with both fields having the value of 200.22.

Thank you,

John

This message has been edited. Last edited by: JohnB,


WF 7.6.5, Windows XP, HTML, Excel, PDF
 
Posts: 100 | Location: San Francisco Bay Area, California | Registered: October 26, 2006Reply With QuoteEdit or Delete MessageReport This Post
Member
Posted Hide Post
Try this and then you can review the Tech Memo's

DEFINE FILE EMPDATA
FIELD_A/P15.4 = 200.2149 ;
FIELD_B/P15.4 = 200.2200;
FIELD_1/P15.2 = FIELD_A + .0001 ;
FIELD_2/P15.2 = FIELD_B ;
END
TABLE FILE EMPDATA
PRINT LN
FIELD_A
FIELD_B
FIELD_1
FIELD_2
WHERE FIELD_1 EQ FIELD_2
END
See these Tech Memo's
http://techsupport.informationbuilders.com/tech/ibm/ibm_tmo_TM7815_p1.html
http://techsupport.informationbuilders.com/tech/ibm/ibm_tmo_TM7815_p1.htm2
 
Posts: 9 | Registered: April 24, 2008Reply With QuoteEdit or Delete MessageReport This Post
Master
Posted Hide Post
I think the best and most certain way to do this is to:
1. add a small number to the field(s), to be sure it rounds to the right number.
2. convert the field(s) to alpha, using FTOA and the format of '(D15.2)'.
3. do the equality check on these converted alpha field(s).

Hope this helps....


GamP

- Using 764 on WinXP (sp2) - IE7.
- Also available are versions from 534 up to 766 on Windows (XP/2000/2003EE)
 
Posts: 518 | Location: Netherlands | Registered: September 25, 2007Reply With QuoteEdit or Delete MessageReport This Post
Master
Posted Hide Post
(Dane's post)
Those two tech memos are specifically labeled Focus for Mainframe, and dated 1990, so I'd be wary of relying on them for other platforms.

(GamP's)
Even if, on a given platform, a calculated P field were held in memory and stored as D without rounding, the formatting of D fields for display (whether in a report or invoked by FTOA) rounds the value. Adding an epsilon is unnecessary, and could only bring harm. I'd recommend just your steps 2 and 3 to compare rounded character representations (or an eqivalent cast to an I field after scaling and rounding)


- Jack Gross
WF 7.6.7, Win
 
Posts: 531 | Location: NYC | Registered: January 11, 2005Reply With QuoteEdit or Delete MessageReport This Post
Platinum Member
Posted Hide Post
Dane,

The company I'm with has no tech support from IBI. Is there some way you can post those tech memos?


GamP,

I did something similar to your approach, and in testing it worked fine. In yesterday's production run, it was inconsistent. I'll try your approach.


What happens when you do the following: FIELD_X/P15.2 = FIELD_A; (when FIELD_A is a P15.4)? Does packed round up correctly?


WF 7.6.5, Windows XP, HTML, Excel, PDF
 
Posts: 100 | Location: San Francisco Bay Area, California | Registered: October 26, 2006Reply With QuoteEdit or Delete MessageReport This Post
Virtuoso
Posted Hide Post
Play around with this code and you will see what happens when you alter SCALE4.
DEFINE FILE CAR
SCALE4/P15.4=12345.6555;
SCALE2/P15.2=SCALE4;
END
TABLE FILE CAR
PRINT SCALE4 SCALE2
BY COUNTRY
END

It rounds up according to the rounding rules for P fields.


Ginny
---------------------------------
Prod: WF 7.6.5 with 7.6.6 WFRS; AIX 5.2; WebSphere 6.1.0.15
Dev: WF 7.6.5 with 7.6.6 WFRS; AIX 5.3; WebSphere 6.1.0.15
Primarily self-service; adapters: Teradata, DB2, Oracle, SQL Server, Essbase, ESRI, FlexEnable
 
Posts: 1534 | Location: BNSF: Fort Worth, TX | Registered: April 05, 2006Reply With QuoteEdit or Delete MessageReport This Post
Virtuoso
Posted Hide Post
Here is additional information:
quote:
Rounding in Packed Decimal Format: Format P
In packed-decimal format (format type P), each byte contains two digits, except the last byte, which contains a digit and the sign (D for negative numbers, C for positive). Packed fields are comparable to COBOL COMP-3.

Packed field values are rounded to the number of digits specified in the field format before they are stored.

When the number of decimal places input is greater than the number that can be stored, P field values are rounded first, then stored or displayed.

Packed fields are precisely accurate when sufficient decimal places are available to store values. Otherwise, since values are rounded before being stored, accuracy cannot be improved by increasing the number of digits displayed. For example, if 123.78 is input to a packed field with 1 decimal place, 123.8 is stored. If the field's format is then changed to P6.2 using a COMPUTE or DEFINE, 123.80 will be displayed. If the field's format is changed to P6.2 in the Master File, 12.38 is displayed.


Ginny
---------------------------------
Prod: WF 7.6.5 with 7.6.6 WFRS; AIX 5.2; WebSphere 6.1.0.15
Dev: WF 7.6.5 with 7.6.6 WFRS; AIX 5.3; WebSphere 6.1.0.15
Primarily self-service; adapters: Teradata, DB2, Oracle, SQL Server, Essbase, ESRI, FlexEnable
 
Posts: 1534 | Location: BNSF: Fort Worth, TX | Registered: April 05, 2006Reply With QuoteEdit or Delete MessageReport This Post
Master
Posted Hide Post
quote:
For example, if FIELD_A = 200.2149 and FIELD_B = 200.2200, then after the rounding they should be considered equal with both fields having the value of 200.22.


Field_A/P15.2 rounded will equal 200.21, not 200.22. Packed fields round correctly.

 
DEFINE FILE EMPDATA
FIELD_A/P15.4 = 200.2149 ;
FIELD_B/P15.4 = 200.2200;
FIELD_C/P15.4 = 200.2150;
FIELD_D/P15.4 = 200.2249;
FIELD_E/P15.4 = 200.2301;
FIELD_A1/P15.2 = FIELD_A ;
FIELD_B1/P15.2 = FIELD_B ;
FIELD_C1/P15.2 = FIELD_C ;
FIELD_D1/P15.2 = FIELD_D ;
FIELD_E1/P15.2 = FIELD_E ;
END
TABLE FILE EMPDATA
PRINT LN
FIELD_A
FIELD_A1
FIELD_B
FIELD_B1
FIELD_C
FIELD_C1
FIELD_D
FIELD_D1
FIELD_E
FIELD_E1
IF RECORDLIMIT EQ 5
END
-EXIT

 
PAGE 1 
 
LASTNAME   FIELD_A  FIELD_A1 FIELD_B   FIELD_B1 FIELD_C   FIELD_C1 FIELD_D   FIELD_D1 FIELD_E   FIELD_E1 
VALINO    200.2149  200.21   200.2200  200.22   200.2150  200.22   200.2249  200.22   200.2301  200.23 
BELLA     200.2149  200.21   200.2200  200.22   200.2150  200.22   200.2249  200.22   200.2301  200.23 
CASSANOVA 200.2149  200.21   200.2200  200.22   200.2150  200.22   200.2249  200.22   200.2301  200.23 
ADAMS     200.2149  200.21   200.2200  200.22   200.2150  200.22   200.2249  200.22   200.2301  200.23 
ADDAMS    200.2149  200.21   200.2200  200.22   200.2150  200.22   200.2249  200.22   200.2301  200.23 



Tom Flynn
WebFOCUS 5.2.2 thru 7.6.x
Windows, Unix, MVS
 
Posts: 668 | Location: Richmond, VA | Registered: January 31, 2006Reply With QuoteEdit or Delete MessageReport This Post
Virtuoso
Posted Hide Post
TOM is correct. 200.2149 will never round to 200.22 in a single operation. Here is a two stepper that works:
DEFINE FILE CAR
SCALE4/P15.4=200.2149;
SCALE3/P15.3=SCALE4;
SCALE2/P15.2=SCALE3;
END
TABLE FILE CAR
PRINT SCALE4 SCALE2 SCALE3
BY COUNTRY
END


Ginny
---------------------------------
Prod: WF 7.6.5 with 7.6.6 WFRS; AIX 5.2; WebSphere 6.1.0.15
Dev: WF 7.6.5 with 7.6.6 WFRS; AIX 5.3; WebSphere 6.1.0.15
Primarily self-service; adapters: Teradata, DB2, Oracle, SQL Server, Essbase, ESRI, FlexEnable
 
Posts: 1534 | Location: BNSF: Fort Worth, TX | Registered: April 05, 2006Reply With QuoteEdit or Delete MessageReport This Post
Platinum Member
Posted Hide Post
Thank you all for the info. I will use your two-stepper, Ginny.

I am wondering if there's anything else with these fields that could affect the accuracy. They start out as alpha fields before becoming packed:

  
   FIELD_A_1/D15.4 = ATODBL(FIELD_IN_DATA_FILE, '14', FIELD_A_1);
   FIELD_A_2/P15.4 = FIELD_A_1;


Could this conversion from decimal to packed cause any inaccuracies that would affect the rounding?

This message has been edited. Last edited by: JohnB,


WF 7.6.5, Windows XP, HTML, Excel, PDF
 
Posts: 100 | Location: San Francisco Bay Area, California | Registered: October 26, 2006Reply With QuoteEdit or Delete MessageReport This Post
 Previous Topic | Next Topic powered by eve community  
 

IB - Developer Center    Forums  Hop To Forum Categories  FOCUS/WebFOCUS    [CODE] Rounding packed fields

Copyright © 1996-2008 Information Builders, leaders in enterprise business intelligence.