C routines for Special Functions

R

Richard Heathfield

[Followups set to comp.lang.c]

Keith A. Lewis wrote:

Error Handling

I'm not a fan of the 'int status = gsl_function(...); if (status)
{...' method of error handling for functions that return doubles.
I am especially not a fan of having default error handlers call
abort(), like the assert macro in C. Of course I've had the head
of IT tear me a new asshole when a programmer of mine left an assert
in production code that got called 15 minutes before market open,
so I can't claim to be unbiased.

If the assertion was written properly (i.e. the condition was one that
/must/ be true if the program is correctly written), then the fault lies
not with the programmer who "left an assert in production code" but with
whoever failed to test the condition that led to the assertion being fired.

<snip>
 
K

Keith A. Lewis

[f'ups to comp.lang.c]

Brian Gough said:
Hmmm, odd looking and unnecessary and ... part of the C standard ;-)
e.g. see ANSI/ISO/IEC 9899:1990 section 5.1.2.2.1 for "int main(void)"

More so, C99 has deprecated ()...

I stand (sit actually) corrected.
 
M

Mark McIntyre

<OT>

As a non-native speaker of american-english, this leaves me to wonder:
when you want to come across real forceful and convincing, is that the
time where the spelling of "you" changes to "ya", or is there something
deeper that I'm missing?

no, he's just a prat who can't (or can't be bothered to) spell.
 
E

E. Robert Tisdale

Mark said:
no, he's just a prat who can't (or can't be bothered to) spell.

No he's just a troll like you. He uses
a troll handle: Joe Blow,
a disposable email address: (e-mail address removed),
attempts to provoke an emotional response --
using abusive language in this case.

If you and Joe Blow want to piss all over each other
please move your "discussion" to an appropriate troll newsgroup.
 
J

J. J. Farrell

Sidney Cadot said:
<OT>

As a non-native speaker of american-english, this leaves me to wonder:
when you want to come across real forceful and convincing, is that the
time where the spelling of "you" changes to "ya", or is there something
deeper that I'm missing?

No, it's for when you want to make sure that everyone knows
you are an ignorant boor, just in case they missed it in your
earlier behaviour.
 
A

Albert van der Horst

Keith A. Lewis said:
Error Handling

I'm not a fan of the 'int status = gsl_function(...); if (status)
{...' method of error handling for functions that return doubles.
I am especially not a fan of having default error handlers call
abort(), like the assert macro in C. Of course I've had the head
of IT tear me a new asshole when a programmer of mine left an assert
in production code that got called 15 minutes before market open,
so I can't claim to be unbiased.
If that is about automated buying of shares....
Would you have preferred the program to buy shares with a limit
of $4294967295 ($-1) ?
GSL does have 'natural form' calling conventions available (more
code on their side to maintain) but they do not permit error
checking. I prefer returning NaN's that have embedded error
information. If you don't check the return codes, at least your
program is more likely to return gibberish than to crash.

Which is a bad thing, of course. (I mean returning gibberish.)
The proper thing to do in a real time "show must go on scenario"
(like radar tracking of a missile ms before impact) is to
catch the error and substitute a reasonable value, and hope for
the best. Not to send your defense missile astray.

In the context of mathematical functions I just want to know
that the programmer goofed. If you as a project leader deleted
the programmers assert's you would have a very bad time with me.
If your head of IT agrees with you, I would take my business
elsewhere.
 
K

Keith A. Lewis

....

Which is a bad thing, of course. (I mean returning gibberish.)
The proper thing to do in a real time "show must go on scenario"
(like radar tracking of a missile ms before impact) is to
catch the error and substitute a reasonable value, and hope for
the best. Not to send your defense missile astray.

Any error protocol requires coordination between the library writer
and the library user. People often will simply ignore error codes
no matter how many lectures you deliver on the "proper thing" to do.

My point was that by defalt GSL calls abort on error and provides
no information in the case of "natural form" calling. I'm open
to better solutions than the one I suggested.
In the context of mathematical functions I just want to know
that the programmer goofed. If you as a project leader deleted
the programmers assert's you would have a very bad time with me.
If your head of IT agrees with you, I would take my business
elsewhere.

You sound like the kind of guy I wouldn't hire in the first place.

FWIW, here is an implementation of my proposed error protocol. It is not
portable, but it gives the idea.


/******************************************************************************

FILE ensure.h
DATE 2002-10-1
AUTHOR Keith A. Lewis [[email protected]]
SYNOPSIS

#include "ensure.h"

ensure (expr);
bool ensure_is_error(double);
const char* ensure_message(double);

DESCRIPTION

Simple error protocol using NaNs.

The macro ensure(expr) works much like assert(expr) but
it does not call abort and it can only be used in
functions that return a double. If expr is false, a NaN
is returned. Use ensure_is_error() to detect ensure()
failures and ensure_message() to extract the error
string.

The function ensure_make_nan() embeds a char* into a
NaN. It is your responsibility to make sure that the
argument to ensure_make_nan() exists until
ensure_message() is done using it.

Define USE_ENSURE or _DEBUG before including ensure.h
in order to make ensure does its job. Unlike assert, the
default behavior is to define ensure with an empty
body.

BUGS:

Only works on 32-bit machines.

******************************************************************************/
#ifndef ENSURE_H
#define ENSURE_H

#include <math.h>
#include <float.h>

#if defined(_WIN32) && !defined(isnan)
#define isnan _isnan
#endif

#define D_(x) #x
#define S_(x) D_(x)

#if defined(USE_ENSURE) || defined(_DEBUG)

// TODO: offer more flexibility. (no return???)
#define ensure(e) { if (!(e)) \
return ensure_make_nan( \
"file: " __FILE__ \
" line: " S_(__LINE__) \
" error: " #e); }

#else

#define ensure(x)

#endif

// Typedefs and constants

typedef double ensure_error;
const ensure_error ensure_ok = 0;

typedef union {
long l[2];
double d;
} ensure_union;

/* Endian tests */
inline int ensure_lo() { static int i = 1; return *(char*)&i != 1; }
inline int ensure_hi() { static int i = 1; return *(char*)&i == 1; }

/* Equivalent to isnan() function. */
inline bool
ensure_is_error(double x)
{
ensure_union u;

u.d = x;

return u.l[ensure_lo()] == 0x7FFFFFFF || 0 != isnan(x);
}

/* Convert NaN to error message string. */
inline const char*
ensure_message(double x)
{
ensure_union u;

u.d = x;

#pragma warning(push)
#pragma warning(disable: 4312)
return u.l[ensure_lo()] == 0x7FFFFFFF ? (const
char*)u.l[ensure_hi()]
: isnan(x) ? "NaN" : "ensure_message: argument is not a NaN";
#pragma warning(pop)
}

/* Insert char* into NaN. You are respsible for the lifetime of s. */
inline double
ensure_make_nan(const char* const s)
{
ensure_union u;

if (0 == s)
return ensure_ok;

u.l[ensure_lo()] = 0x7FFFFFFF;
#pragma warning(push)
#pragma warning(disable: 4311)
u.l[ensure_hi()] = (long)s;
#pragma warning(pop)

return u.d;
}

#ifdef isnan
#undef isnan
#endif

#endif /* ENSURE_H */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top