$tanh (aggregation)
On this page本页内容
$tanhNew in version 4.2.4.2版新增。Returns the hyperbolic tangent of a value that is measured in radians.返回以弧度为单位测量的值的双曲正切。$tanhhas the following syntax:具有以下语法:{ $tanh: <expression> }$tanhtakes any valid expression that resolves to a number, measured in radians. If the expression returns a value in degrees, use the$degreesToRadiansoperator to convert the value to radians.$tanh接受任何解析为以弧度为单位的数字的有效表达式。如果表达式返回以度为单位的值,请使用$degreesToRadians运算符将该值转换为弧度。By default默认情况下$tanhreturns values as adouble.$tanh以double形式返回值。如果$tanhcan also return values as a 128-bit decimal if the<expression>resolves to a 128-bit decimal value.<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. null值或引用了缺失的字段,$tanh将返回null。If the argument resolves to 如果参数解析为NaN, $tanh returns NaN. NaN,$tanh将返回NaN。If the argument resolves to negative or positive 如果参数解析为负无穷大或正Infinity, $tanh returns -1 or 1 respectively.Infinity,$tanh将分别返回-1或1。
{ $tanh: NaN } | NaN |
{ $tanh: null } | null |
{ $tanh: -Infinity } | -1 |
{ $tanh: Infinity } | 1 |
Example实例
The following 以下trigonometry collection contains a document that stores an angle value measured in degrees:trigonometry集合包含一个文档,该文档存储以度为单位测量的angle值:
db.trigonometry.insertOne(
{
"_id" : ObjectId( "5c50782193f833234ba90d45" ),
"angle" : NumberDecimal( "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" : NumberDecimal("53.1301023541559787031443874490659"),
"tanh_output" : NumberDecimal("0.7293303448445332820512777329448416")
}
Because 由于angle is stored as a 128-bit decimal, the $tanh output is also a 128-bit decimal.angle存储为128位十进制,$tanh输出也是128位十进制。
The following 以下trigonometry collection contains a document that stores an angle value measured in radians:trigonometry集合包含一个文档,该文档存储以弧度为单位测量的angle值:
db.trigonometry.insertOne(
{
"_id" : ObjectId( "5c50782193f833234ba90d55" ),
"angle" : NumberDecimal( "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表达式计算angle的双曲正切,并使用$addFields管道阶段将其添加到输入文档:
db.trigonometry.aggregate( [
{
$addFields : {
"tanh_output" : { $tanh : "$angle" }
}
}
] )
Example output:输出示例:
{
"_id" : ObjectId("5c50782193f833234ba90d55"),
"angle" : NumberDecimal("1.6301023541559787031443874490659"),
"tanh_output" : NumberDecimal("0.9260761562750713360156803177935379")
}
Because 由于angle is stored as a 128-bit decimal, the $tanh output is also a 128-bit decimal.angle存储为128位十进制,$tanh输出也是128位十进制。