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/
pgp1jmrpj9BFH.pgp
Description: PGP signature
_______________________________________________ gnucash-devel mailing list [email protected] https://lists.gnucash.org/mailman/listinfo/gnucash-devel
