_matherr

Gestisce gli errori matematici.

Sintassi

int _matherr(struct _exception *except);

Parametri

except
Puntatore alla struttura contenente le informazioni sull'errore.

Valore restituito

_matherr restituisce 0 per indicare un errore o un valore diverso da zero per indicare l'esito positivo:

  • Se _matherr restituisce 0, è possibile visualizzare un messaggio di errore e errno impostare su un valore di errore appropriato.
  • Se _matherr restituisce un valore diverso da zero, non viene visualizzato alcun messaggio di errore e errno rimane invariato.

Per altre informazioni sui codici restituiti, vedere errno, _doserrno, _sys_errliste _sys_nerr.

Osservazioni:

La _matherr funzione elabora gli errori generati dalle funzioni a virgola mobile della libreria matematica. Queste funzioni chiamano _matherr quando viene rilevato un errore. Questa interazione non è influenzata dalla modalità a virgola mobile del compilatore o dalla parola di controllo a virgola mobile. Poiché _matherr è una funzione di libreria, le funzioni intrinseche matematiche non lo chiamano.

Per una gestione speciale degli errori, è possibile specificare una definizione diversa di _matherr. Se si usa la versione collegata dinamicamente della libreria di runtime C (CRT), è possibile sostituire la routine predefinita _matherr in un eseguibile client con una versione definita dall'utente. Tuttavia, non è possibile sostituire la routine predefinita _matherr in un client DLL della DLL CRT.

Quando si verifica un errore in una routine matematica, _matherr viene chiamato con un puntatore a una _exception struttura di tipo (definita in <math.h>) come argomento. La _exception struttura contiene gli elementi seguenti.

struct _exception
{
    int    type;   // exception type - see below
    char*  name;   // name of function where error occurred
    double arg1;   // first argument to function
    double arg2;   // second argument (if any) to function
    double retval; // value to be returned by function
};

Il type membro specifica il tipo di errore matematico. È uno dei valori seguenti, definiti in <math.h>:

Macro Descrizione
_DOMAIN Errore del dominio dell'argomento
_SING Singolarità dell'argomento
_OVERFLOW Errore di intervallo di overflow
_PLOSS Perdita parziale di significatività
_TLOSS Perdita totale di significatività
_UNDERFLOW Il risultato è troppo piccolo per essere rappresentato. Questa condizione non è attualmente supportata.

Il membro name della struttura è un puntatore a una stringa con terminazione Null contenente il nome della funzione che ha causato l'errore. Membri della struttura arg1 e arg2 specificare i valori che hanno causato l'errore. Se viene specificato un solo argomento, viene archiviato in arg1.

Il valore restituito predefinito per l'errore specificato è retval. Se si modifica il valore restituito, deve specificare se si è verificato un errore effettivamente.

Requisiti

Ciclo Intestazione obbligatoria
_matherr <math.h>

Per altre informazioni sulla compatibilità, vedere Compatibility (Compatibilità).

Esempio

/* crt_matherr.c
* Illustrates writing an error routine for math
* functions. 
* The error handling function must be named _matherr
*/

#include <math.h>
#include <string.h>
#include <stdio.h>

int main()
{
    /* Do several math operations that cause errors. The _matherr
     * routine handles _DOMAIN errors, but lets the system handle
     * other errors normally.
     */
    printf( "log( -2.0 ) = %e\n", log( -2.0 ) );
    printf( "log10( -5.0 ) = %e\n", log10( -5.0 ) );
    printf( "log( 0.0 ) = %e\n", log( 0.0 ) );
}

/* Handle several math errors caused by passing a negative argument
* to log or log10 (_DOMAIN errors). When this happens, _matherr
* returns the natural or base-10 logarithm of the absolute value
* of the argument and suppresses the usual error message.
*/
int _matherr(struct _exception *except)
{
    /* Handle _DOMAIN errors for log or log10. */
    if (except->type == _DOMAIN)
    {
        if (strcmp(except->name, "log") == 0)
        {
            except->retval = log(-(except->arg1));
            printf("Special: using absolute value: %s: _DOMAIN "
                     "error\n", except->name);
            return 1;
        }
        else if (strcmp(except->name, "log10") == 0)
        {
            except->retval = log10(-(except->arg1));
            printf("Special: using absolute value: %s: _DOMAIN "
                     "error\n", except->name);
            return 1;
        }
    }
    printf("Normal: ");
    return 0;    /* Else use the default actions */
}
Special: using absolute value: log: _DOMAIN error
log( -2.0 ) = 6.931472e-01
Special: using absolute value: log10: _DOMAIN error
log10( -5.0 ) = 6.989700e-01
Normal: log( 0.0 ) = -inf

Vedi anche

Supporto matematico e a virgola mobile