[Gnucash-changes] r13248 - gnucash/trunk/src/calculation - Fix
Bug 107876 - financial calculator would call exit(1) if calculation
resulted in an interest rate of zero.
Neil Williams
linux at codehelp.co.uk
Mon Feb 13 06:24:10 EST 2006
On Monday 13 February 2006 9:51 am, Christian Stimming wrote:
> Hi Neil,
>
> > I'm setting up an error state that is perpetuated through the code.
>
> Oh no. This means you are using the code pattern "one special
> calculation return value corresponds to an error value". This code
> pattern should better be avoided, if at all possible.
I wish it was. However, I can't get assertions to do the job, we just go back
into the infinite loop - probably because assert doesn't change the value
being returned. What I needed was:
#include <glib.h>
...
g_return_val_if_fail(eint == 0.0, 0.0);
But when I looked at this last night, I didn't want to have to add glib.h just
for the error case.
The assert commands - like the fprintf before it - fail to actually print to
the terminal unless the calculation code is allowed to call exit. I've tried
fflush with no success.
With g_return_... I get the following entries in /tmp/gnucash.trace:
_C: assertion `eint == 0.0' failed
_C: assertion `eint == 0.0' failed
_C: assertion `eint == 0.0' failed
Warning: PrintAmountInternal(): Problem with remainder.
_C: assertion `eint == 0.0' failed
_C: assertion `eint == 0.0' failed
Warning: PrintAmountInternal(): Problem with remainder.
So even with the assertions, the code continues to try to calculate - this is
the quickest I can get the code to abort - even using the error state.
:-(
> In this fin.c
> case, I don't understand why a different solution isn't possible --
Because unless the return value is set to the error state, the recursive loop
becomes infinite.
Hopefully, using g_return_val_if_fail will make it clear that returning 0.0
from functions that calculate interest rates indicates a failure.
> The calling function _C() obviously will then have the restriction that
> eint must be nonzero as well, and it is called a few more times, but it
> shouldn't be too difficult to add these checks to all calling places as
> well. Please replace your "returned special error code" by a check for
> nonzero-ness up front.
I tried it but could not get it to work with assert() from <assert.h>
It does work when the return value is set to the error state. Using
g_return_val_if_fail just makes that clearer when reading the code later -
the error state itself persists.
Done in r13254.
--
Neil Williams
=============
http://www.data-freedom.org/
http://www.nosoftwarepatents.com/
http://www.linux.codehelp.co.uk/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.gnucash.org/pipermail/gnucash-devel/attachments/20060213/dddbb9e6/attachment.bin
More information about the gnucash-devel
mailing list