Difference between revisions of "cpp/numeric/math/sin"
m (correct link) |
m ({{closed range}}.) |
||
(7 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/title|sin}} | + | {{cpp/title|sin|sinf|sinl}} |
{{cpp/numeric/math/navbar}} | {{cpp/numeric/math/navbar}} | ||
− | {{ | + | {{cpp/numeric/math/declarations |
− | + | |family=sin | |
− | + | |param1=num | |
− | + | |constexpr_since=26 | |
+ | |desc=Computes the sine of {{c|num}} (measured in radians). | ||
}} | }} | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | | + | {{par|num|floating-point or integer value representing angle in radians}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | If no errors occur, the sine of {{ | + | If no errors occur, the sine of {{c|num}} ({{math|sin(num)}}) in the range {{closed range|-1|+1}}, is returned. |
− | {{ | + | {{rrev|until=c++11| |
− | + | The result may have little or no significance if the magnitude of {{c|num}} is large. | |
− | The result may have little or no significance if the magnitude of {{ | + | |
}} | }} | ||
− | |||
− | If a domain error occurs, an implementation-defined value is returned (NaN where supported) | + | If a domain error occurs, an implementation-defined value is returned (NaN where supported). |
If a range error occurs due to underflow, the correct result (after rounding) is returned. | If a range error occurs due to underflow, the correct result (after rounding) is returned. | ||
===Error handling=== | ===Error handling=== | ||
− | Errors are reported as specified in | + | Errors are reported as specified in {{lc|math_errhandling}}. |
If the implementation supports IEEE floating-point arithmetic (IEC 60559), | If the implementation supports IEEE floating-point arithmetic (IEC 60559), | ||
− | * if the argument is ±0, it is returned unmodified | + | * if the argument is ±0, it is returned unmodified. |
− | * if the argument is ±∞, NaN is returned and {{lc|FE_INVALID}} is raised <!-- not defined as a domain error in C or C++, although defined as a domain error in POSIX --> | + | * if the argument is ±∞, NaN is returned and {{lc|FE_INVALID}} is raised.<!--not defined as a domain error in C or C++, although defined as a domain error in POSIX--> |
− | * if the argument is NaN, NaN is returned | + | * if the argument is NaN, NaN is returned. |
===Notes=== | ===Notes=== | ||
− | The case where the argument is infinite is not specified to be a domain error in C (to which C++ defers), but it is defined as a [ | + | The case where the argument is infinite is not specified to be a domain error in C (to which C++ defers), but it is defined as a [https://pubs.opengroup.org/onlinepubs/9699919799/functions/sin.html domain error in POSIX]. |
− | POSIX also specifies that in case of underflow, {{ | + | POSIX also specifies that in case of underflow, {{c|num}} is returned unmodified, and if that is not supported, an implementation-defined value no greater than {{lc|DBL_MIN}}, {{lc|FLT_MIN}}, and {{lc|LDBL_MIN}} is returned. |
+ | |||
+ | {{cpp/numeric/math/additional integer overload note|sin}} | ||
===Example=== | ===Example=== | ||
− | {{example|code= | + | {{example |
− | + | |code= | |
− | + | ||
#include <cerrno> | #include <cerrno> | ||
#include <cfenv> | #include <cfenv> | ||
+ | #include <cmath> | ||
+ | #include <iomanip> | ||
+ | #include <iostream> | ||
+ | |||
+ | // #pragma STDC FENV_ACCESS ON | ||
+ | |||
+ | const double pi = std::acos(-1); // or std::numbers::pi since C++20 | ||
+ | |||
+ | constexpr double your_sin(double x) | ||
+ | { | ||
+ | double sin{0}, pow{x}; | ||
+ | for (auto fac{1LLU}, n{1ULL}; n != 20; fac *= ++n, pow *= x) | ||
+ | if (n & 1) | ||
+ | sin += (n & 2 ? -pow : pow) / fac; | ||
+ | return sin; | ||
+ | } | ||
− | |||
− | |||
int main() | int main() | ||
{ | { | ||
− | + | std::cout << std::setprecision(10) << std::showpos | |
− | std::cout << "sin(pi/6) = " << std::sin(pi/6) << '\n' | + | << "Typical usage:\n" |
− | << "sin(pi/2) = " << std::sin(pi/2) << '\n' | + | << "std::sin(pi/6) = " << std::sin(pi / 6) << '\n' |
− | << "sin(-3*pi/4) = " << std::sin(-3*pi/4) << '\n' | + | << "your sin(pi/6) = " << your_sin(pi / 6) << '\n' |
− | + | << "std::sin(pi/2) = " << std::sin(pi / 2) << '\n' | |
− | + | << "your sin(pi/2) = " << your_sin(pi / 2) << '\n' | |
− | << "sin(-0) = " << std::sin(-0.0) << '\n'; | + | << "std::sin(-3*pi/4) = " << std::sin(-3 * pi / 4) << '\n' |
− | // error handling | + | << "your sin(-3*pi/4) = " << your_sin(-3 * pi / 4) << '\n' |
+ | << "Special values:\n" | ||
+ | << "std::sin(+0) = " << std::sin(0.0) << '\n' | ||
+ | << "std::sin(-0) = " << std::sin(-0.0) << '\n'; | ||
+ | |||
+ | // error handling | ||
std::feclearexcept(FE_ALL_EXCEPT); | std::feclearexcept(FE_ALL_EXCEPT); | ||
− | std::cout << "sin(INFINITY) = " << std::sin(INFINITY) << '\n'; | + | |
− | if(std::fetestexcept(FE_INVALID)) std::cout << " FE_INVALID raised\n"; | + | std::cout << "std::sin(INFINITY) = " << std::sin(INFINITY) << '\n'; |
+ | if (std::fetestexcept(FE_INVALID)) | ||
+ | std::cout << " FE_INVALID raised\n"; | ||
} | } | ||
|p=true | |p=true | ||
|output= | |output= | ||
− | sin(pi/6) = 0.5 | + | Typical usage: |
− | sin(pi/2) = 1 | + | std::sin(pi/6) = +0.5 |
− | sin(-3*pi/4) = -0. | + | your sin(pi/6) = +0.5 |
− | sin(+0) = 0 | + | std::sin(pi/2) = +1 |
− | sin(-0) = -0 | + | your sin(pi/2) = +1 |
− | sin(INFINITY) = -nan | + | std::sin(-3*pi/4) = -0.7071067812 |
+ | your sin(-3*pi/4) = -0.7071067812 | ||
+ | Special values: | ||
+ | std::sin(+0) = +0 | ||
+ | std::sin(-0) = -0 | ||
+ | std::sin(INFINITY) = -nan | ||
FE_INVALID raised | FE_INVALID raised | ||
}} | }} | ||
Line 87: | Line 100: | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/numeric/math/dsc cos}} | + | {{dsc inc|cpp/numeric/math/dsc cos}} |
− | {{dsc inc | cpp/numeric/math/dsc tan}} | + | {{dsc inc|cpp/numeric/math/dsc tan}} |
− | {{dsc inc | cpp/numeric/math/dsc asin}} | + | {{dsc inc|cpp/numeric/math/dsc asin}} |
− | {{dsc inc | cpp/numeric/complex/dsc sin}} | + | {{dsc inc|cpp/numeric/complex/dsc sin}} |
− | {{dsc inc | cpp/numeric/valarray/dsc sin}} | + | {{dsc inc|cpp/numeric/valarray/dsc sin}} |
− | {{dsc see c | c/numeric/math/sin}} | + | {{dsc see c|c/numeric/math/sin}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}} | {{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}} |
Latest revision as of 08:45, 13 November 2023
Defined in header <cmath>
|
||
(1) | ||
float sin ( float num ); double sin ( double num ); |
(until C++23) | |
/* floating-point-type */ sin ( /* floating-point-type */ num ); |
(since C++23) (constexpr since C++26) |
|
float sinf( float num ); |
(2) | (since C++11) (constexpr since C++26) |
long double sinl( long double num ); |
(3) | (since C++11) (constexpr since C++26) |
Additional overloads (since C++11) |
||
Defined in header <cmath>
|
||
template< class Integer > double sin ( Integer num ); |
(A) | (constexpr since C++26) |
std::sin
for all cv-unqualified floating-point types as the type of the parameter.(since C++23)
A) Additional overloads are provided for all integer types, which are treated as double.
|
(since C++11) |
Contents |
[edit] Parameters
num | - | floating-point or integer value representing angle in radians |
[edit] Return value
If no errors occur, the sine of num (sin(num)) in the range [
-1,
+1]
, is returned.
The result may have little or no significance if the magnitude of num is large. |
(until C++11) |
If a domain error occurs, an implementation-defined value is returned (NaN where supported).
If a range error occurs due to underflow, the correct result (after rounding) is returned.
[edit] Error handling
Errors are reported as specified in math_errhandling.
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
- if the argument is ±0, it is returned unmodified.
- if the argument is ±∞, NaN is returned and FE_INVALID is raised.
- if the argument is NaN, NaN is returned.
[edit] Notes
The case where the argument is infinite is not specified to be a domain error in C (to which C++ defers), but it is defined as a domain error in POSIX.
POSIX also specifies that in case of underflow, num is returned unmodified, and if that is not supported, an implementation-defined value no greater than DBL_MIN, FLT_MIN, and LDBL_MIN is returned.
The additional overloads are not required to be provided exactly as (A). They only need to be sufficient to ensure that for their argument num of integer type, std::sin(num) has the same effect as std::sin(static_cast<double>(num)).
[edit] Example
#include <cerrno> #include <cfenv> #include <cmath> #include <iomanip> #include <iostream> // #pragma STDC FENV_ACCESS ON const double pi = std::acos(-1); // or std::numbers::pi since C++20 constexpr double your_sin(double x) { double sin{0}, pow{x}; for (auto fac{1LLU}, n{1ULL}; n != 20; fac *= ++n, pow *= x) if (n & 1) sin += (n & 2 ? -pow : pow) / fac; return sin; } int main() { std::cout << std::setprecision(10) << std::showpos << "Typical usage:\n" << "std::sin(pi/6) = " << std::sin(pi / 6) << '\n' << "your sin(pi/6) = " << your_sin(pi / 6) << '\n' << "std::sin(pi/2) = " << std::sin(pi / 2) << '\n' << "your sin(pi/2) = " << your_sin(pi / 2) << '\n' << "std::sin(-3*pi/4) = " << std::sin(-3 * pi / 4) << '\n' << "your sin(-3*pi/4) = " << your_sin(-3 * pi / 4) << '\n' << "Special values:\n" << "std::sin(+0) = " << std::sin(0.0) << '\n' << "std::sin(-0) = " << std::sin(-0.0) << '\n'; // error handling std::feclearexcept(FE_ALL_EXCEPT); std::cout << "std::sin(INFINITY) = " << std::sin(INFINITY) << '\n'; if (std::fetestexcept(FE_INVALID)) std::cout << " FE_INVALID raised\n"; }
Possible output:
Typical usage: std::sin(pi/6) = +0.5 your sin(pi/6) = +0.5 std::sin(pi/2) = +1 your sin(pi/2) = +1 std::sin(-3*pi/4) = -0.7071067812 your sin(-3*pi/4) = -0.7071067812 Special values: std::sin(+0) = +0 std::sin(-0) = -0 std::sin(INFINITY) = -nan FE_INVALID raised
[edit] See also
(C++11)(C++11) |
computes cosine (cos(x)) (function) |
(C++11)(C++11) |
computes tangent (tan(x)) (function) |
(C++11)(C++11) |
computes arc sine (arcsin(x)) (function) |
computes sine of a complex number (sin(z)) (function template) | |
applies the function std::sin to each element of valarray (function template) | |
C documentation for sin
|