Issue 19991 - ERF and ERFC functions do not operate properly
Summary: ERF and ERFC functions do not operate properly
Status: CLOSED FIXED
Alias: None
Product: Calc
Classification: Application
Component: code (show other issues)
Version: OOo 1.1 RC4
Hardware: All All
: P3 Trivial (vote)
Target Milestone: ---
Assignee: frank
QA Contact: issues@sc
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-09-24 14:39 UTC by Unknown
Modified: 2013-08-07 15:13 UTC (History)
5 users (show)

See Also:
Issue Type: PATCH
Latest Confirmation in: ---
Developer Difficulty: ---


Attachments
Proposed patch (8.26 KB, patch)
2005-03-11 03:16 UTC, sragavan
no flags Details | Diff
test program (2.35 KB, text/plain)
2005-10-07 02:59 UTC, kyoshida
no flags Details
Comparison table (21.59 KB, application/vnd.oasis.opendocument.spreadsheet)
2005-10-07 03:22 UTC, kyoshida
no flags Details
Proposed patch (SRC680_m130) (2.84 KB, patch)
2005-10-07 03:39 UTC, kyoshida
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this issue.
Description Unknown 2003-09-24 14:39:28 UTC
The ERF and ERFC functions do not return correct values.  Example:
ERFC(5) should equal 1.54E-12
In OpenOffice: ERFC(5) = -7.28E-011

The valid range for ERFC is [0,1], so a negative number should not be possible.

1 - ERF(5) returns the same result, so the ERF function is broken as well.
Comment 1 frank 2003-09-24 15:30:21 UTC
Hi Daniel,

you have to set the numberformat to scientific to or the decimal
places to 16 or so.

Prio was much to high (Prio 1 is a totally unusefull application) I've
corrected this one.

Frank
Comment 2 oc 2003-10-08 11:50:36 UTC
According to the roadmap of OpenOffice.org 1.1
(http://tools.openoffice.org/releases/Openoffice_org_1_x.html) this
issue has been scheduled for 1.1.2.
Comment 3 oc 2004-03-10 15:33:33 UTC
Fix is to risky for OOo1.1.2 => retargeted to OOo2.0
Comment 4 oc 2004-05-28 11:15:22 UTC
OC: Due to high workload this issue is retargeted to office.later
Comment 5 stephenmontsmith 2004-08-22 01:05:36 UTC
I looked at the source code in scaddins/source/analysis/analysishelper.cxx, and
the computation for Erf uses Taylor's series - this is almost sure to get large
rounding errors of the kind reported here.  (Indeed Erf(7) gives an answer of
about 1362.92 which is way way off the true answer.)

My suggestion is to simply copy the code for erf and erfc that comes with
Berkeley Unix (which comes with a very unrestrictive license).  For example,
look in the FreeBSD source code at lib/libm/common_source/erf.c.

I think it would be really helpful to fix this bug - today I converted a friend
from Excel to Openoffice because his version of Excel didn't have erf.
Comment 6 sragavan 2005-03-11 03:16:34 UTC
Created attachment 23673 [details]
Proposed patch
Comment 7 sragavan 2005-03-11 03:17:35 UTC
Ive done, whatz reqd. Please review and comment if any changed reqd.
The berkley code has a liscense and as per that ive included that liscense too.
Comment 8 kyoshida 2005-09-19 03:18:36 UTC
A patch is attached to this issue.  Is anybody not going to review this for
inclusion?
Comment 9 stair 2005-09-19 18:59:00 UTC
Here here kohei.  I have to say that it is very disappointing that somemone has
taken the trouble to submit the patch and 6 months later it has still not been
accepted.

I appreciate that the OOO team has high workload and are doing a great job
however this kind of problem is complete showstopper for elements of the
engineering and scientific community that is the natural constituency for OOO
Comment 10 niklas.nebel 2005-09-30 08:59:37 UTC
Sorry about the delay.
Code to be integrated into the main OOo tree has to be covered by a JCA, we
can't use other people's BSD-licensed code.
Comment 11 kyoshida 2005-10-05 06:57:15 UTC
I think I've found a good algorithm for ERF that converges to 1 as x becomes
larger.  It's a clean-room implementation hence there will be no licensing issue
(the mathematical approximation formula used in the algorithm is from a
published source).

I'll upload a patch and a test program in a day or two.  Stay tuned.

Kohei
Comment 12 kyoshida 2005-10-07 02:59:38 UTC
Created attachment 30194 [details]
test program
Comment 13 kyoshida 2005-10-07 03:08:02 UTC
Compile and run the test program as follows:

[kohei@localhost Erf]$ g++ -Os -o erf erf.cxx
[kohei@localhost Erf]$ ./erf 5
<maclaurin>
erf:  1.000000000072773
erfc: -7.2773443412881e-11
<asymptotic>
erf:  0.9999999999984626
erfc: 1.537436844500917e-12

The maclaurin series expansion gives Calc's current answers for erf and erfc,
while the asymptotic series expansion (proposed new algorithm) give more
accurate answers.

You can also run this program with no arguments.  It will compute erf and erfc
using both the Maclaurin expansion (current algorithm) and the Asymptotic
expansion (new algorithm) for x between -10 and 10 incremented by 0.1.

[kohei@localhost Erf]$ ./erf
x       erf(x) <maclaurin>      erfc(x) <maclaurin>     erf(x) <asymptotic>    
erfc(x) <asymptotic>
-10     7.331381385975313e+21   -7.331381385975313e+21  -1      2
-9.9    -3.548111434363548e+19  3.548111434363548e+19   -1      2
-9.8    -1.744418020013381e+20  1.744418020013381e+20   -1      2
-9.7    -1.560844956904942e+19  1.560844956904942e+19   -1      2
<snip>
9.7     -7.659310690090643e+18  7.659310690090643e+18   1       0
9.8     3.709705234685252e+19   -3.709705234685252e+19  1       0
9.9     4.120589789696997e+20   -4.120589789696997e+20  1       0
10      9.970406838904948e+21   -9.970406838904948e+21  1       0

Comment 14 kyoshida 2005-10-07 03:20:39 UTC
There is however one caveat; the original Maclaurin expansion is more accurate
for values of x up to 4 while the Asymptotic expansion performs terribly.  But
for x larger than 4 the Asymptotic expansion is more accurate.  Therefore, in my
proposed patch, which will follow soon, the original Maclaurin expansion is used
for 0 < x < 4 and the asymptotic expansion for x > 4.

I have also compared against the values of erf and erfc computed by octave,
gnumeric, and Excel 2003.  My proposed algorithm performs comparably, though not
identically, with octave and gnumeric (these two spit the identical numbers so I
assume they use the same algorithm), but it converges to 1 more quickly than
octave/gnumeric.  However, when compared with Excel 2003's values they were
almost identical, so much so that I'm pretty sure that Excel must use this same
algorithm internally!

The comparison table follows...
Comment 15 kyoshida 2005-10-07 03:22:57 UTC
Created attachment 30195 [details]
Comparison table
Comment 16 kyoshida 2005-10-07 03:39:28 UTC
Created attachment 30196 [details]
Proposed patch (SRC680_m130)
Comment 17 kyoshida 2005-10-07 03:42:55 UTC
Setting target milestone to 2.0.1.  I hope there is still window for that
milestone. :-)

Kohei
Comment 19 daniel.rentz 2005-10-07 11:41:31 UTC
patch applied in SRC680/dr42 (OOo 2.0.1)
Comment 20 daniel.rentz 2005-10-07 11:43:00 UTC
Thanks for your effort, Kohei :-)
Comment 21 stair 2005-10-07 19:47:25 UTC
Thanks for your efforts Kohei.

I would not propose reopening this issue any time soon but would suggest
examining this and other complex, approximated functions in the future to see if
they can be improved. For at least ERFC(x) this is as good as MS Excel but I
believe the ambition should always to be better.

It also happens I routinely use values of 'x' = 4 to 6 and sometimes bigger in
my work so I have an interest.

I am neither mathematician nor coder but I am willing to do some digging if teh
results will be used.

Finally, and this might seem a silly question, but why not borrow the code from
Gnumeric or Octave?

Comment 22 kyoshida 2005-10-07 20:45:13 UTC
We can't use code from Gnumeric or octave because their codes are GPL'ed and
none of us are the original author of the ERF/ERFC code in octave/gnumeric.  GPL
wouldn't allow the OO.o code to be relicenced under a proprietary licence, which
Sun needs in order to release StarOffice.  Even less restrictive BSD licenced
code would not do.

If we can find out the mathematical formula used in octave (not the code), we
can devise our own code from it to produce the same result.  That's probably
what we would need to do if we wanted our ERF/ERFC to behave like octave.

Kohei
Comment 23 stair 2005-10-07 22:13:25 UTC
Thanks for the explanation, I did not understand the licensing issue for Sun.

I will see if I can find the algorithm
Comment 24 kyoshida 2005-10-10 18:59:51 UTC
I've created Issue 55735 to handle the algorithm improvement.  Feel free to put
yourself in CC for those who are interested.

kohei->dr: Daniel, I recommend we process this issue as RESOLVED FIXED as it is
currently tagged, and use Issue 55735 for the improvement.

Kohei
Comment 25 daniel.rentz 2005-10-11 10:56:28 UTC
kohei: Agreed.
Comment 26 daniel.rentz 2005-10-19 17:01:15 UTC
back to QA

re-open issue and reassign to fst@openoffice.org
Comment 27 daniel.rentz 2005-10-19 17:01:20 UTC
reassign to fst@openoffice.org
Comment 28 daniel.rentz 2005-10-19 17:01:26 UTC
reset resolution to FIXED
Comment 29 frank 2005-10-21 14:31:08 UTC
found fixed on cws dr42 using Linux, Solaris and Windows build
Comment 30 frank 2005-11-21 12:24:55 UTC
Found integrated on master m142 using Linux, Solaris and Windows Build