Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 225693 - BIRT's percent function, percent format don't agree on meaning
Summary: BIRT's percent function, percent format don't agree on meaning
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: BIRT (show other bugs)
Version: 2.2.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 2.3.1   Edit
Assignee: Rick Lu CLA
QA Contact:
URL:
Whiteboard: Non-Auto
Keywords:
Depends on: 226409 227644
Blocks:
  Show dependency tree
 
Reported: 2008-04-04 01:16 EDT by Paul Rogers CLA
Modified: 2009-09-09 04:58 EDT (History)
5 users (show)

See Also:


Attachments
Product Sales by Country.rptdesign (82.95 KB, text/plain)
2008-04-04 01:16 EDT, Paul Rogers CLA
no flags Details
Revised Product Sales by Country.rptdesign. Can see data in BIRT 2.3 (83.69 KB, application/octet-stream)
2008-04-10 03:57 EDT, Rick Lu CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Paul Rogers CLA 2008-04-04 01:16:22 EDT
Created attachment 94810 [details]
Product Sales by Country.rptdesign

Open the attached report (or build your own.) Look at the Change column for a country, say Australia. This field has a value computed using the

Finance.percent( )

function provided by BIRT. The result is a percent. That is 100 percent means 1 (number). Finance.percent( 2, 1 ) returns 50. (Note the strange reversal of numerator and denominator: one thinks of division as num/denom, but the percent function uses percent( denom, num ). Hard to remember, but it is too late to change.)

Now, look at the style for this item. It uses BIRT's percent formatting.

Preview the report. The field should show the percentage change as a percent. But, it shows 100 times the change. The reason appears to be that the percentage formatting feature assumes percents are represented as ratios: .5 for 50% and so on.

The result is that one can choose to use one or the other feature, but not both together. This gets tricky because:

* One cannot specify a custom format so I can do my own percentage. (See Bug 225692.)

* I used percent because I need a "safe divide", one that handles a zero in the denominator. I assumed that Finance.percent handled this case.

The workaround is to use percent formatting (since I can't easily do custom formatting without writing messy code), and to do a roll-my-own safe divide:

if ( denom == 0 ) then 0;
else num / denom;

A suggestion for resolution is to add a new method:

BirtComp.safeDivide( num, denom [, if0 ] )

Example:

BirtComp.safeDivide( 10 /* change */, 0 /* base */, 100 );

Here, I want to treat an increase from zero as a 100% increase. (Not mathematically correct, but a useful approximation for business people.)

BirtComp.safeDivide( a, b, null );

Here I want a null (undefined) value if b is 0.
Comment 1 Rick Lu CLA 2008-04-10 03:55:27 EDT
In current implementation, if denom is null, the IllegalArgumentException is thrown. Do we expect 0 if the denom is null?

It is OK to support BirtComp.safeDivide( num, denom [, if0 ] ) in the report part. Any comment from Data team? 

BTW, the design file cannot be previewed correctly in BIRT 2.3. Please refer to the bug 226409. 
Comment 2 Rick Lu CLA 2008-04-10 03:57:52 EDT
Created attachment 95489 [details]
Revised Product Sales by Country.rptdesign. Can see data in BIRT 2.3
Comment 3 Paul Rogers CLA 2008-04-10 17:01:17 EDT
(In reply to comment #1)
> In current implementation, if denom is null, the IllegalArgumentException is
> thrown. Do we expect 0 if the denom is null?

FWIW, my suggestion is to not change the current function to avoid breaking existing code. You could, if you wanted, add an optional third argument. If the third argument exists, return that for a divide-by-zero.

> BTW, the design file cannot be previewed correctly in BIRT 2.3. Please refer to
> the bug 226409. 

I'm using BIRT 2.2. If the change to 2.3 breaks designs, some users may be a bit upset...

Comment 4 Gary Xue CLA 2008-04-18 18:56:38 EDT
SafeDivide function is available in the BirtMath object. It will be made available after BUG 227644 is resolved.

I think the consensus is to leave the behavior of Finance.percent() unchanged to preserve backward compatibility.
Comment 5 Rick Lu CLA 2008-04-24 01:27:48 EDT
See dependency for bug 227644.
Comment 6 Rick Lu CLA 2008-07-15 01:24:10 EDT
Uses BirtMath.safeDivide( ). This is now available on the BIRT expression builder. Details refer to depend bugs.
Comment 7 Maggie Shen CLA 2008-08-12 22:41:46 EDT
verified.