$sin (aggregation)
Nesta página
Definição
$sin
Retorna o seno de um valor que é medido em radianos.
$sin
tem a seguinte sintaxe:{ $sin: <expression> } $sin
takes any valid expressão that resolves to a number. If the expression returns a value in degrees, use the$degreesToRadians
operator to convert the result to radians.Por padrão,
$sin
retorna valores comodouble
.$sin
também pode retornar valores como um decimal de 128 bits, desde que o<expression>
resolva para um valor decimal de 128 bits.Para mais informações sobre expressões, consulte Operadores de Expressão.
Comportamento
null
, NaN
e +/- Infinity
If the argument resolves to a value of null
or refers to a field
that is missing, $sin
returns null
. If the
argument resolves to NaN
, $sin
returns NaN
.
If the argument resolves to negative or positive infinity,
$sin
throws an error.
Exemplo | Resultados | |||
---|---|---|---|---|
|
| |||
|
| |||
ou
| Lança uma mensagem de erro semelhante à seguinte saída formatada:
|
Exemplo
The trigonometry
collection contains a document that
stores the hypotenuse and one angle in a right-angle triangle:
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "angle_a" : NumberDecimal("53.13010235415597870314438744090659"), "hypotenuse" : NumberDecimal("5") }
The following aggregation operation uses the
$sin
expression to calculate the side opposite
to angle_a
and add it to the input document using the
$addFields
pipeline stage.
db.trigonometry.aggregate([ { $addFields : { "side_b" : { $multiply : [ { $sin : {$degreesToRadians : "$angle_a"} }, "$hypotenuse" ] } } } ])
The $degreesToRadians
expression converts the
degree value of angle_a
to the equivalent value in radians.
O comando retorna a seguinte saída:
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "angle_a" : NumberDecimal("53.13010235415597870314438744090659"), "side_b" : NumberDecimal("4.000000000000000000000000000000000"), "hypotenuse" : NumberDecimal("5"), }
Como angle_a
e hypotenuse
são armazenados como decimais de 128bits, a saída de $sin
é um decimal de 128bits.
The trigonometry
collection contains a document that
stores the hypotenuse and one angle in a right-angle triangle:
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "angle_a" : NumberDecimal("0.9272952180016122324285124629224288"), "hypotenuse" : NumberDecimal("5") }
The following aggregation operation uses the
$sin
expression to calculate the side opposite
to angle_a
and add it to the input document using the
$addFields
pipeline stage.
db.trigonometry.aggregate([ { $addFields : { "side_b" : { $multiply : [ { $sin : "$angle_a" }, "$hypotenuse" ] } } } ])
O comando retorna a seguinte saída:
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "angle_a" : NumberDecimal("0.9272952180016122324285124629224288"), "side_b" : NumberDecimal("4.000000000000000000000000000000000"), "hypotenuse" : NumberDecimal("5"), }
Como angle_a
e hypotenuse
são armazenados como decimais de 128bits, a saída de $sin
é um decimal de 128bits.