Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Forums
  - IRC
  - Community Discord
  - Wiki
  - GitHub
  - Issues
  - Get involved
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# std.math.trigonometry

This is a submodule of std.math.
It contains several trigonometric functions.
License:
Boost License 1.0.
Authors:
Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger

Source std/math/trigonometry.d

pure nothrow @nogc @safe real `cos`(real `x`);
pure nothrow @nogc @safe double `cos`(double `x`);
pure nothrow @nogc @safe float `cos`(float `x`);
  Returns cosine of x. x is in radians.

  Special Values
  x | cos(x) | invalid?
  --- | --- | ---
  NAN | NAN | yes
  ±∞ | NAN | yes

  Bugs:
  Results are undefined if |x| >= 264.
  Examples:

      import std.math.operations : isClose;

      writeln(cos(0.0)); // 1.0
      assert(cos(1.0).isClose(0.5403023059));
      assert(cos(3.0).isClose(-0.9899924966));

pure nothrow @nogc @safe real `sin`(real `x`);
pure nothrow @nogc @safe double `sin`(double `x`);
pure nothrow @nogc @safe float `sin`(float `x`);
  Returns sine of x. x is in radians.

  Special Values
  x | sin(x) | invalid?
  --- | --- | ---
  NAN | NAN | yes
  ±0.0 | ±0.0 | no
  ±∞ | NAN | yes

  Parameters:

  real `x` | angle in radians (not degrees)

  Returns:
  sine of x
  See Also:
  cos , tan , asin
  Bugs:
  Results are undefined if |x| >= 264.
  Examples:

      import std.math.constants : PI;
      import std.stdio : writefln;

      void someFunc()
      {
        real x = 30.0;
        auto result = sin(x * (PI / 180)); // convert degrees to radians
        writefln("The sine of %s degrees is %s", x, result);
      }

pure nothrow @nogc @safe real `tan`(real `x`);
pure nothrow @nogc @safe double `tan`(double `x`);
pure nothrow @nogc @safe float `tan`(float `x`);
  Returns tangent of x. x is in radians.

  Special Values
  x | tan(x) | invalid?
  --- | --- | ---
  NAN | NAN | yes
  ±0.0 | ±0.0 | no
  ±∞ | NAN | yes

  Examples:

      import std.math.operations : isClose;
      import std.math.traits : isIdentical;
      import std.math.constants : PI;
      import std.math.algebraic : sqrt;

      assert(isIdentical(tan(0.0), 0.0));
      assert(tan(PI).isClose(0, 0.0, 1e-10));
      assert(tan(PI / 3).isClose(sqrt(3.0)));

pure nothrow @nogc @safe real `acos`(real `x`);
pure nothrow @nogc @safe double `acos`(double `x`);
pure nothrow @nogc @safe float `acos`(float `x`);
  Calculates the arc cosine of x, returning a value ranging from 0 to π.

  Special Values
  x | acos(x) | invalid?
  --- | --- | ---
  >1.0 | NAN | yes
  <-1.0 | NAN | yes
  NAN | NAN | yes

  Examples:

      import std.math.operations : isClose;
      import std.math.traits : isNaN;
      import std.math.constants : PI;

      assert(acos(0.0).isClose(1.570796327));
      assert(acos(0.5).isClose(PI / 3));
      assert(acos(PI).isNaN);

pure nothrow @nogc @safe real `asin`(real `x`);
pure nothrow @nogc @safe double `asin`(double `x`);
pure nothrow @nogc @safe float `asin`(float `x`);
  Calculates the arc sine of x, returning a value ranging from -π/2 to π/2.

  Special Values
  x | asin(x) | invalid?
  --- | --- | ---
  ±0.0 | ±0.0 | no
  >1.0 | NAN | yes
  <-1.0 | NAN | yes

  Examples:

      import std.math.operations : isClose;
      import std.math.traits : isIdentical, isNaN;
      import std.math.constants : PI;

      assert(isIdentical(asin(0.0), 0.0));
      assert(asin(0.5).isClose(PI / 6));
      assert(asin(PI).isNaN);

pure nothrow @nogc @safe real `atan`(real `x`);
pure nothrow @nogc @safe double `atan`(double `x`);
pure nothrow @nogc @safe float `atan`(float `x`);
  Calculates the arc tangent of x, returning a value ranging from -π/2 to π/2.

  Special Values
  x | atan(x) | invalid?
  --- | --- | ---
  ±0.0 | ±0.0 | no
  ±∞ | NAN | yes

  Examples:

      import std.math.operations : isClose;
      import std.math.traits : isIdentical;
      import std.math.constants : PI;
      import std.math.algebraic : sqrt;

      assert(isIdentical(atan(0.0), 0.0));
      assert(atan(sqrt(3.0)).isClose(PI / 3));

pure nothrow @nogc @trusted real `atan2`(real `y`, real `x`);
pure nothrow @nogc @safe double `atan2`(double `y`, double `x`);
pure nothrow @nogc @safe float `atan2`(float `y`, float `x`);
  Calculates the arc tangent of y / x, returning a value ranging from -π to π.

  Special Values
  y | x | atan(y, x)
  --- | --- | ---
  NAN | anything | NAN
  anything | NAN | NAN
  ±0.0 | >0.0 | ±0.0
  ±0.0 | +0.0 | ±0.0
  ±0.0 | <0.0 | ±π
  ±0.0 | -0.0 | ±π
  >0.0 | ±0.0 | π/2
  <0.0 | ±0.0 | -π/2
  >0.0 | ∞ | ±0.0
  ±∞ | anything | ±π/2
  >0.0 | -∞ | ±π
  ±∞ | ∞ | ±π/4
  ±∞ | -∞ | ±3π/4

  Examples:

      import std.math.operations : isClose;
      import std.math.constants : PI;
      import std.math.algebraic : sqrt;

      assert(atan2(1.0, sqrt(3.0)).isClose(PI / 6));

pure nothrow @nogc @safe real `cosh`(real `x`);
pure nothrow @nogc @safe double `cosh`(double `x`);
pure nothrow @nogc @safe float `cosh`(float `x`);
  Calculates the hyperbolic cosine of x.

  Special Values
  x | cosh(x) | invalid?
  --- | --- | ---
  ±∞ | ±0.0 | no

  Examples:

      import std.math.constants : E;
      import std.math.operations : isClose;

      writeln(cosh(0.0)); // 1.0
      assert(cosh(1.0).isClose((E + 1.0 / E) / 2));

pure nothrow @nogc @safe real `sinh`(real `x`);
pure nothrow @nogc @safe double `sinh`(double `x`);
pure nothrow @nogc @safe float `sinh`(float `x`);
  Calculates the hyperbolic sine of x.

  Special Values
  x | sinh(x) | invalid?
  --- | --- | ---
  ±0.0 | ±0.0 | no
  ±∞ | ±∞ | no

  Examples:

      import std.math.constants : E;
      import std.math.operations : isClose;
      import std.math.traits : isIdentical;

      enum sinh1 = (E - 1.0 / E) / 2;
      import std.meta : AliasSeq;
      static foreach (F; AliasSeq!(float, double, real))
      {
          assert(isIdentical(sinh(F(0.0)), F(0.0)));
          assert(sinh(F(1.0)).isClose(F(sinh1)));
      }

pure nothrow @nogc @safe real `tanh`(real `x`);
pure nothrow @nogc @safe double `tanh`(double `x`);
pure nothrow @nogc @safe float `tanh`(float `x`);
  Calculates the hyperbolic tangent of x.

  Special Values
  x | tanh(x) | invalid?
  --- | --- | ---
  ±0.0 | ±0.0 | no
  ±∞ | ±1.0 | no

  Examples:

      import std.math.operations : isClose;
      import std.math.traits : isIdentical;

      assert(isIdentical(tanh(0.0), 0.0));
      assert(tanh(1.0).isClose(sinh(1.0) / cosh(1.0)));

pure nothrow @nogc @safe real `acosh`(real `x`);
pure nothrow @nogc @safe double `acosh`(double `x`);
pure nothrow @nogc @safe float `acosh`(float `x`);
  Calculates the inverse hyperbolic cosine of x.
  Mathematically, acosh(x) = log(x + sqrt( x*x - 1))

  Domain X | Range Y
  --- | ---
  1..∞ | 0..∞

  Special Values
  x | acosh(x)
  --- | ---
  NAN | NAN
  <1 | NAN
  1 | 0
  +∞ | +∞

  Examples:

      import std.math.traits : isIdentical, isNaN;

      assert(isNaN(acosh(0.9)));
      assert(isNaN(acosh(real.nan)));
      assert(isIdentical(acosh(1.0), 0.0));
      writeln(acosh(real.infinity)); // real.infinity
      assert(isNaN(acosh(0.5)));

pure nothrow @nogc @safe real `asinh`(real `x`);
pure nothrow @nogc @safe double `asinh`(double `x`);
pure nothrow @nogc @safe float `asinh`(float `x`);
  Calculates the inverse hyperbolic sine of x.
  Mathematically,

      asinh(x) =  log( x + sqrt( x*x + 1 )) // if x >= +0
      asinh(x) = -log(-x + sqrt( x*x + 1 )) // if x <= -0

  Special Values
  x | asinh(x)
  --- | ---
  NAN | NAN
  ±0 | ±0
  ±∞ | ±∞

  Examples:

      import std.math.traits : isIdentical, isNaN;

      assert(isIdentical(asinh(0.0), 0.0));
      assert(isIdentical(asinh(-0.0), -0.0));
      writeln(asinh(real.infinity)); // real.infinity
      writeln(asinh(-real.infinity)); // -real.infinity
      assert(isNaN(asinh(real.nan)));

pure nothrow @nogc @safe real `atanh`(real `x`);
pure nothrow @nogc @safe double `atanh`(double `x`);
pure nothrow @nogc @safe float `atanh`(float `x`);
  Calculates the inverse hyperbolic tangent of x, returning a value from ranging from -1 to 1.
  Mathematically, atanh(x) = log( (1+x)/(1-x) ) / 2

  Domain X | Range Y
  --- | ---
  -∞..∞ | -1 .. 1

  Special Values
  x | acosh(x)
  --- | ---
  NAN | NAN
  ±0 | ±0
  -∞ | -0

  Examples:

      import std.math.traits : isIdentical, isNaN;

      assert(isIdentical(atanh(0.0), 0.0));
      assert(isIdentical(atanh(-0.0),-0.0));
      assert(isNaN(atanh(real.nan)));
      assert(isNaN(atanh(-real.infinity)));
      writeln(atanh(0.0)); // 0

Copyright © 1999-2023 by the D Language Foundation | Page generated by Ddoc on Wed Mar 1 14:57:25 2023