os/ossrv/genericopenlibs/cstdlib/LMATH/S_TANH.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* S_TANH.C
sl@0
     2
 * 
sl@0
     3
 * Portions Copyright (c) 1993-1999 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     4
 * All rights reserved.
sl@0
     5
 */
sl@0
     6
sl@0
     7
sl@0
     8
/* @(#)s_tanh.c 5.1 93/09/24 */
sl@0
     9
/*
sl@0
    10
 * ====================================================
sl@0
    11
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
sl@0
    12
 *
sl@0
    13
 * Developed at SunPro, a Sun Microsystems, Inc. business.
sl@0
    14
 * Permission to use, copy, modify, and distribute this
sl@0
    15
 * software is freely granted, provided that this notice 
sl@0
    16
 * is preserved.
sl@0
    17
 * ====================================================
sl@0
    18
 */
sl@0
    19
sl@0
    20
/*
sl@0
    21
sl@0
    22
FUNCTION
sl@0
    23
        <<tanh>>, <<tanhf>>---hyperbolic tangent
sl@0
    24
sl@0
    25
INDEX
sl@0
    26
tanh
sl@0
    27
INDEX
sl@0
    28
tanhf
sl@0
    29
sl@0
    30
ANSI_SYNOPSIS
sl@0
    31
        #include <math.h>
sl@0
    32
        double tanh(double <[x]>);
sl@0
    33
        float tanhf(float <[x]>);
sl@0
    34
sl@0
    35
TRAD_SYNOPSIS
sl@0
    36
        #include <math.h>
sl@0
    37
        double tanh(<[x]>)
sl@0
    38
        double <[x]>;
sl@0
    39
sl@0
    40
        float tanhf(<[x]>)
sl@0
    41
        float <[x]>;
sl@0
    42
sl@0
    43
sl@0
    44
DESCRIPTION
sl@0
    45
sl@0
    46
<<tanh>> computes the hyperbolic tangent of
sl@0
    47
the argument <[x]>.  Angles are specified in radians.  
sl@0
    48
sl@0
    49
<<tanh(<[x]>)>> is defined as 
sl@0
    50
. sinh(<[x]>)/cosh(<[x]>)
sl@0
    51
	
sl@0
    52
<<tanhf>> is identical, save that it takes and returns <<float>> values.
sl@0
    53
sl@0
    54
RETURNS
sl@0
    55
The hyperbolic tangent of <[x]> is returned.
sl@0
    56
sl@0
    57
PORTABILITY
sl@0
    58
<<tanh>> is ANSI C.  <<tanhf>> is an extension.
sl@0
    59
sl@0
    60
*/
sl@0
    61
sl@0
    62
/* Tanh(x)
sl@0
    63
 * Return the Hyperbolic Tangent of x
sl@0
    64
 *
sl@0
    65
 * Method :
sl@0
    66
 *				       x    -x
sl@0
    67
 *				      e  - e
sl@0
    68
 *	0. tanh(x) is defined to be -----------
sl@0
    69
 *				       x    -x
sl@0
    70
 *				      e  + e
sl@0
    71
 *	1. reduce x to non-negative by tanh(-x) = -tanh(x).
sl@0
    72
 *	2.  0      <= x <= 2**-55 : tanh(x) := x*(one+x)
sl@0
    73
 *					        -t
sl@0
    74
 *	    2**-55 <  x <=  1     : tanh(x) := -----; t = expm1(-2x)
sl@0
    75
 *					       t + 2
sl@0
    76
 *						     2
sl@0
    77
 *	    1      <= x <=  22.0  : tanh(x) := 1-  ----- ; t=expm1(2x)
sl@0
    78
 *						   t + 2
sl@0
    79
 *	    22.0   <  x <= INF    : tanh(x) := 1.
sl@0
    80
 *
sl@0
    81
 * Special cases:
sl@0
    82
 *	tanh(NaN) is NaN;
sl@0
    83
 *	only tanh(0)=0 is exact for finite argument.
sl@0
    84
 */
sl@0
    85
sl@0
    86
#include "FDLIBM.H"
sl@0
    87
sl@0
    88
static const double one=1.0, two=2.0, tiny = 1.0e-300;
sl@0
    89
sl@0
    90
/**
sl@0
    91
Calculate hyperbolic tangent.
sl@0
    92
@return hyperbolic tangent of x.
sl@0
    93
@param x Angle expressed in radians (180 degrees = PI radians).
sl@0
    94
*/	
sl@0
    95
EXPORT_C double tanh(double x) __SOFTFP
sl@0
    96
{
sl@0
    97
	double t,z;
sl@0
    98
	__int32_t jx,ix;
sl@0
    99
sl@0
   100
    /* High word of |x|. */
sl@0
   101
	GET_HIGH_WORD(jx,x);
sl@0
   102
	ix = jx&0x7fffffff;
sl@0
   103
sl@0
   104
    /* x is INF or NaN */
sl@0
   105
	if(ix>=0x7ff00000) { 
sl@0
   106
	    if (jx>=0) return one/x+one;    /* tanh(+-inf)=+-1 */
sl@0
   107
	    else       return one/x-one;    /* tanh(NaN) = NaN */
sl@0
   108
	}
sl@0
   109
sl@0
   110
    /* |x| < 22 */
sl@0
   111
	if (ix < 0x40360000) {		/* |x|<22 */
sl@0
   112
	    if (ix<0x3c800000) 		/* |x|<2**-55 */
sl@0
   113
		return x*(one+x);    	/* tanh(small) = small */
sl@0
   114
	    if (ix>=0x3ff00000) {	/* |x|>=1  */
sl@0
   115
		t = expm1(two*fabs(x));
sl@0
   116
		z = one - two/(t+two);
sl@0
   117
	    } else {
sl@0
   118
	        t = expm1(-two*fabs(x));
sl@0
   119
	        z= -t/(t+two);
sl@0
   120
	    }
sl@0
   121
    /* |x| > 22, return +-1 */
sl@0
   122
	} else {
sl@0
   123
	    z = one - tiny;		/* raised inexact flag */
sl@0
   124
	}
sl@0
   125
	return (jx>=0)? z: -z;
sl@0
   126
}