support/fdlibm/s_tanh.c
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 16 Jan 2013 21:31:50 +0000
branchrefactoring-vmdata
changeset 1973 617e6a088dd1
parent 1818 2e5ed72e7dfd
child 2380 9195eccdcbd9
permissions -rw-r--r--
- JavaListInspectorView class: JavaListInspectorView - JavaFormalParameterNode class: JavaFormalParameterNode - JavaTypeNode class: JavaTypeNode - JavaScanner class: JavaScanner - JavaArrayTypeNode class: JavaArrayTypeNode - JavaParser class: JavaParser - JavaCommentNode class: JavaCommentNode - JavaIntTypeNode class: JavaIntTypeNode - JavaParseNodeBuilder class: JavaParseNodeBuilder - JavaMethodDeclaratorNode class: JavaMethodDeclaratorNode - JavaScannerBase class: JavaScannerBase - JavaFloatTypeNode class: JavaFloatTypeNode - JavaDocNode class: JavaDocNode - JavaClassOrInterfaceTypeNode class: JavaClassOrInterfaceTypeNode - stx_libjava_tools class: stx_libjava_tools - JavaSettingsApplication class: JavaSettingsApplication - JavaLongTypeNode class: JavaLongTypeNode - JavaParserII class: JavaParserII - JavaSourceReference class: JavaSourceReference - JavaCharTypeNode class: JavaCharTypeNode - JavaParser_Eclipse class: JavaParser_Eclipse - JavaSyntaxHighlighter class: JavaSyntaxHighlighter - JavaMethodNode class: JavaMethodNode - JavaBooleanTypeNode class: JavaBooleanTypeNode - JavaDoubleTypeNode class: JavaDoubleTypeNode - JavaSetInspectorView class: JavaSetInspectorView - JavaParseNode class: JavaParseNode - JavaVoidTypeNode class: JavaVoidTypeNode - JavaMapInspectorView class: JavaMapInspectorView - JavaParserI class: JavaParserI - extensions ...
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
986
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
     1
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
     2
/* @(#)s_tanh.c 1.3 95/01/18 */
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
     3
/*
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
     4
 * ====================================================
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
     5
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
     6
 *
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
     7
 * Developed at SunSoft, a Sun Microsystems, Inc. business.
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
     8
 * Permission to use, copy, modify, and distribute this
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
     9
 * software is freely granted, provided that this notice 
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    10
 * is preserved.
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    11
 * ====================================================
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    12
 */
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    13
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    14
/* Tanh(x)
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    15
 * Return the Hyperbolic Tangent of x
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    16
 *
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    17
 * Method :
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    18
 *				       x    -x
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    19
 *				      e  - e
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    20
 *	0. tanh(x) is defined to be -----------
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    21
 *				       x    -x
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    22
 *				      e  + e
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    23
 *	1. reduce x to non-negative by tanh(-x) = -tanh(x).
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    24
 *	2.  0      <= x <= 2**-55 : tanh(x) := x*(one+x)
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    25
 *					        -t
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    26
 *	    2**-55 <  x <=  1     : tanh(x) := -----; t = expm1(-2x)
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    27
 *					       t + 2
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    28
 *						     2
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    29
 *	    1      <= x <=  22.0  : tanh(x) := 1-  ----- ; t=expm1(2x)
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    30
 *						   t + 2
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    31
 *	    22.0   <  x <= INF    : tanh(x) := 1.
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    32
 *
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    33
 * Special cases:
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    34
 *	tanh(NaN) is NaN;
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    35
 *	only tanh(0)=0 is exact for finite argument.
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    36
 */
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    37
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    38
#include "fdlibm.h"
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    39
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    40
#ifdef __STDC__
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    41
static const double one=1.0, two=2.0, tiny = 1.0e-300;
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    42
#else
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    43
static double one=1.0, two=2.0, tiny = 1.0e-300;
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    44
#endif
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    45
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    46
#ifdef __STDC__
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    47
	double tanh(double x)
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    48
#else
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    49
	double tanh(x)
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    50
	double x;
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    51
#endif
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    52
{
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    53
	double t,z;
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    54
	int jx,ix;
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    55
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    56
    /* High word of |x|. */
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    57
	jx = __HI(x);
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    58
	ix = jx&0x7fffffff;
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    59
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    60
    /* x is INF or NaN */
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    61
	if(ix>=0x7ff00000) { 
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    62
	    if (jx>=0) return one/x+one;    /* tanh(+-inf)=+-1 */
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    63
	    else       return one/x-one;    /* tanh(NaN) = NaN */
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    64
	}
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    65
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    66
    /* |x| < 22 */
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    67
	if (ix < 0x40360000) {		/* |x|<22 */
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    68
	    if (ix<0x3c800000) 		/* |x|<2**-55 */
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    69
		return x*(one+x);    	/* tanh(small) = small */
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    70
	    if (ix>=0x3ff00000) {	/* |x|>=1  */
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    71
		t = expm1(two*fabs(x));
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    72
		z = one - two/(t+two);
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    73
	    } else {
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    74
	        t = expm1(-two*fabs(x));
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    75
	        z= -t/(t+two);
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    76
	    }
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    77
    /* |x| > 22, return +-1 */
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    78
	} else {
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    79
	    z = one - tiny;		/* raised inexact flag */
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    80
	}
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    81
	return (jx>=0)? z: -z;
a53b146a146d Added fdlibm for IEE754-style double arithmetics
vranyj1
parents:
diff changeset
    82
}