Database Manual / Reference / Query Language / Expressions

$tanh (expression operator)(表达式运算符)

$tanh

Returns the hyperbolic tangent of a value that is measured in radians.返回以弧度为单位测量的值的双曲正切。

$tanh has the following syntax:具有以下语法:

{ $tanh: <expression> }

$tanh takes any valid expression that resolves to a number, measured in radians. If the expression returns a value in degrees, use the $degreesToRadians operator to convert the value to radians.$tanh接受任何解析为数字的有效表达式,以弧度为单位。如果表达式返回以度为单位的值,请使用$degreesToRadians运算符将该值转换为弧度。

By default $tanh returns values as a double. $tanh can also return values as a 128-bit decimal if the <expression> resolves to a 128-bit decimal value.默认情况下,$tanhdouble形式返回值。如果<expression>解析为128位十进制值,$tanh也可以返回128位十进制的值。

For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Behavior行为

null, NaN, and +/- Infinity

If the input argument resolves to a value of null or refers to a field that is missing, $tanh returns null. If the argument resolves to NaN, $tanh returns NaN. If the argument resolves to negative or positive Infinity, $tanh returns -1 or 1 respectively.如果输入参数解析为null值或引用缺少的字段,$tanh将返回null。如果参数解析为NaN$tanh将返回NaN。如果参数解析为负无穷大或正无穷大,$tanh将分别返回-11

Example示例Results结果
{ $tanh: NaN }NaN
{ $tanh: null }null
{ $tanh: -Infinity }-1
{ $tanh: Infinity }1

Example示例

Hyperbolic Tangent in Degrees以度为单位的双曲正切值

The following trigonometry collection contains a document that stores an angle value measured in degrees:以下trigonometry集合包含一个存储以度为单位测量的angle值的文档:

db.trigonometry.insertOne(
{
"_id" : ObjectId( "5c50782193f833234ba90d45" ),
"angle" : Decimal128( "53.1301023541559787031443874490659" )
}
)

The following aggregation operation uses the $tanh expression to calculate the hyperbolic tangent of angle and adds it to the input document using the $addFields pipeline stage:以下聚合操作使用$tanh表达式计算angle的双曲正切,并使用$addFields管道阶段将其添加到输入文档中:

db.trigonometry.aggregate( [
{
$addFields : {
"tanh_output" : { $tanh : { $degreesToRadians : "$angle" } }
}
}
] )

The $degreesToRadians expression converts the angle in degrees to radians.$degreesToRadians表达式将以度为单位的angle转换为弧度。

Example output:示例输出:

{
"_id" : ObjectId("5c50782193f833234ba90d45"),
"angle" : Decimal128("53.1301023541559787031443874490659"),
"tanh_output" : Decimal128("0.7293303448445332820512777329448416")
}

Because angle is stored as a 128-bit decimal, the $tanh output is also a 128-bit decimal.因为angle存储为128位十进制,所以$tanh输出也是128位十进制。

Hpyerbolic Tangent in Radians以弧度为单位的双曲正切值

The following trigonometry collection contains a document that stores an angle value measured in radians:以下trigonometry集合包含一个文档,其中存储了以弧度为单位测量的angle值:

db.trigonometry.insertOne(
{
"_id" : ObjectId( "5c50782193f833234ba90d55" ),
"angle" : Decimal128( "1.6301023541559787031443874490659" )
}
)

The following aggregation operation uses the $tanh expression to calculate the hyperbolic tangent of angle and adds it to the input document using the $addFields pipeline stage:以下聚合操作使用$tanh表达式计算角度的双曲正切,并使用$addFields管道阶段将其添加到输入文档中:

db.trigonometry.aggregate( [
{
$addFields : {
"tanh_output" : { $tanh : "$angle" }
}
}
] )

Example output:示例输出:

{
"_id" : ObjectId("5c50782193f833234ba90d55"),
"angle" : Decimal128("1.6301023541559787031443874490659"),
"tanh_output" : Decimal128("0.9260761562750713360156803177935379")
}

Because angle is stored as a 128-bit decimal, the $tanh output is also a 128-bit decimal.因为angle存储为128位十进制,所以$tanh输出也是128位十进制。