Java Math 类分别为双曲正弦,余弦和切线提供了 sinh,cosh 和 tanh 函数。 名称类似于三角函数,只是在末尾添加字母“h”以表示双曲线。
双曲计算基于指数函数,非常简单。
cos x = (e^x + e^-x)/2
sin x = (e^x – e^-x)/2
tan x = sin/cos
还可以使用下面给出的 exp 方法来实现这些功能,而无需使用内置的 sinh,cosh 和 tanh 方法。
/*
* The cosh
*/
//using built-in method cosh
System.out.println("Buil-in cosh method: " + Math.cosh(2) );
System.out.println("Using exp method: " + (Math.exp(2) + Math.exp(-2))/2d );
/*
* The sinh
*/
//using built-in method sinh
System.out.println("Buil-in sinh method: " + Math.sinh(2) );
System.out.println("Using exp method: " + (Math.exp(2) - Math.exp(-2))/2d );
/*
* The tanh
*/
//using built-in method tanh
System.out.println("Buil-in tanh method: " + Math.tanh(2) );
double cosh = (Math.exp(2) + Math.exp(-2))/2d;
double sinh = (Math.exp(2) - Math.exp(-2))/2d;
System.out.println("Using exp method: " + sinh/cosh );
输出:
Buil-in cosh method: 3.7621956910836314
Using exp method: 3.7621956910836314
Buil-in sinh method: 3.626860407847019
Using exp method: 3.626860407847019
Buil-in tanh method: 0.9640275800758169
Using exp method: 0.964027580075817