Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.112.0

overview

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 usinga local clone.

std.math.algebraic

This is a submodule ofstd.math.
It contains classical algebraic functions likeabs,sqrt, andpoly.
License:
Boost License 1.0.
Authors:
Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger

Sourcestd/math/algebraic.d

pure nothrow @nogc autoabs(Num)(Numx)
if (isIntegral!Num || is(typeof(Num.init >= 0)) && is(typeof(-Num.init)));
Calculates the absolute value of a number.
Parameters:
Num(template parameter) type of number
Numxreal number value
Returns:
The absolute value of the number. If floating-point or integral, the return type will be the same as the input.

LimitationsWhen x is a signed integral equal toNum.min the value of x will be returned instead. Note for 2's complement;-Num.min (=Num.max + 1) is not representable due to overflow.

Examples:
import std.math.traits : isIdentical, isNaN;assert(isIdentical(abs(-0.0L), 0.0L));assert(isNaN(abs(real.nan)));writeln(abs(-real.infinity));// real.infinitywriteln(abs(-56));// 56writeln(abs(2321312L));// 2321312Lwriteln(abs(23u));// 23u
pure nothrow @nogc @safe realfabs(realx);

pure nothrow @nogc @safe doublefabs(doublex);

pure nothrow @nogc @safe floatfabs(floatx);
Returns |x|
Special Values
xfabs(x)
±0.0+0.0
±∞+∞
Examples:
import std.math.traits : isIdentical;assert(isIdentical(fabs(0.0f), 0.0f));assert(isIdentical(fabs(-0.0f), 0.0f));writeln(fabs(-10.0f));// 10.0fassert(isIdentical(fabs(0.0), 0.0));assert(isIdentical(fabs(-0.0), 0.0));writeln(fabs(-10.0));// 10.0assert(isIdentical(fabs(0.0L), 0.0L));assert(isIdentical(fabs(-0.0L), 0.0L));writeln(fabs(-10.0L));// 10.0L
pure nothrow @nogc @safe floatsqrt(floatx);

pure nothrow @nogc @safe doublesqrt(doublex);

pure nothrow @nogc @safe realsqrt(realx);
Compute square root of x.
Special Values
xsqrt(x)invalid?
-0.0-0.0no
<0.0NANyes
+∞+∞no
Examples:
import std.math.operations : feqrel;import std.math.traits : isNaN;assert(sqrt(2.0).feqrel(1.4142) > 16);assert(sqrt(9.0).feqrel(3.0) > 16);assert(isNaN(sqrt(-1.0f)));assert(isNaN(sqrt(-1.0)));assert(isNaN(sqrt(-1.0L)));
pure nothrow @nogc @trusted realcbrt(realx);
Calculates the cube root of x.
Special Values
xcbrt(x)invalid?
±0.0±0.0no
NANNANyes
±∞±∞no
Examples:
import std.math.operations : feqrel;assert(cbrt(1.0).feqrel(1.0) > 16);assert(cbrt(27.0).feqrel(3.0) > 16);assert(cbrt(15.625).feqrel(2.5) > 16);
pure nothrow @nogc @safe Thypot(T)(const Tx, const Ty)
if (isFloatingPoint!T);
Calculates the length of the hypotenuse of a right-angled triangle with sides of length x and y. The hypotenuse is the value of the square root of the sums of the squares of x and y:
sqrt(x2 + y2)
Note that hypot(x, y), hypot(y, x) and hypot(x, -y) are equivalent.
Special Values
xyhypot(x, y)invalid?
x±0.0|x|no
±∞y+∞no
±∞NAN+∞no
NANy != ±∞NANno
Examples:
import std.math.operations : feqrel;import std.math.traits : isNaN;assert(hypot(1.0, 1.0).feqrel(1.4142) > 16);assert(hypot(3.0, 4.0).feqrel(5.0) > 16);writeln(hypot(real.infinity, 1.0L));// real.infinitywriteln(hypot(1.0L,real.infinity));// real.infinitywriteln(hypot(real.infinity,real.nan));// real.infinitywriteln(hypot(real.nan,real.infinity));// real.infinityassert(hypot(real.nan, 1.0L).isNaN);assert(hypot(1.0L,real.nan).isNaN);
pure nothrow @nogc @safe Thypot(T)(const Tx, const Ty, const Tz)
if (isFloatingPoint!T);
Calculates the distance of the point (x, y, z) from the origin (0, 0, 0) in three-dimensional space. The distance is the value of the square root of the sums of the squares of x, y, and z:
sqrt(x2 + y2 + z2)
Note that the distance between two points (x1, y1, z1) and (x2, y2, z2) in three-dimensional space can be calculated as hypot(x2-x1, y2-y1, z2-z1).
Parameters:
Txfloating point value
Tyfloating point value
Tzfloating point value
Returns:
The square root of the sum of the squares of the given arguments.
Examples:
import std.math.operations : isClose;assert(isClose(hypot(1.0, 2.0, 2.0), 3.0));assert(isClose(hypot(2.0, 3.0, 6.0), 7.0));assert(isClose(hypot(1.0, 4.0, 8.0), 9.0));
pure nothrow @nogc @trusted Unqual!(CommonType!(T1, T2))poly(T1, T2)(T1x, in T2[]A)
if (isFloatingPoint!T1 && isFloatingPoint!T2);

pure nothrow @nogc @safe Unqual!(CommonType!(T1, T2))poly(T1, T2, int N)(T1x, ref const T2[N]A)
if (isFloatingPoint!T1 && isFloatingPoint!T2 && (N > 0) && (N <= 10));
Evaluate polynomial A(x) = a0 + a1x + a2x2 + a3x3; ...
Uses Horner's rule A(x) = a0 + x(a1 + x(a2 + x(a3 + ...)))
Parameters:
T1xthe value to evaluate.
T2[]Aarray of coefficients a0, a1, etc.
Examples:
realx = 3.1L;staticreal[] pp = [56.1L, 32.7L, 6];writeln(poly(x, pp));// (56.1L + (32.7L + 6.0L * x) * x)
TnextPow2(T)(const Tval)
if (isIntegral!T);

TnextPow2(T)(const Tval)
if (isFloatingPoint!T);
Gives the next power of two afterval.T can be any built-in numerical type.
If the operation would lead to an over/underflow, this function will return0.
Parameters:
Tvalany number
Returns:
the next power of two afterval
Examples:
writeln(nextPow2(2));// 4writeln(nextPow2(10));// 16writeln(nextPow2(4000));// 4096writeln(nextPow2(-2));// -4writeln(nextPow2(-10));// -16writeln(nextPow2(uint.max));// 0writeln(nextPow2(uint.min));// 0writeln(nextPow2(size_t.max));// 0writeln(nextPow2(size_t.min));// 0writeln(nextPow2(int.max));// 0writeln(nextPow2(int.min));// 0writeln(nextPow2(long.max));// 0writeln(nextPow2(long.min));// 0
Examples:
writeln(nextPow2(2.1));// 4.0writeln(nextPow2(-2.0));// -4.0writeln(nextPow2(0.25));// 0.5writeln(nextPow2(-4.0));// -8.0writeln(nextPow2(double.max));// 0.0writeln(nextPow2(double.infinity));// double.infinity
TtruncPow2(T)(const Tval)
if (isIntegral!T);

TtruncPow2(T)(const Tval)
if (isFloatingPoint!T);
Gives the last power of two beforeval.<> can be any built-in numerical type.
Parameters:
Tvalany number
Returns:
the last power of two beforeval
Examples:
writeln(truncPow2(3));// 2writeln(truncPow2(4));// 4writeln(truncPow2(10));// 8writeln(truncPow2(4000));// 2048writeln(truncPow2(-5));// -4writeln(truncPow2(-20));// -16writeln(truncPow2(uint.max));// int.max + 1writeln(truncPow2(uint.min));// 0writeln(truncPow2(ulong.max));// long.max + 1writeln(truncPow2(ulong.min));// 0writeln(truncPow2(int.max));// (int.max / 2) + 1writeln(truncPow2(int.min));// int.minwriteln(truncPow2(long.max));// (long.max / 2) + 1writeln(truncPow2(long.min));// long.min
Examples:
writeln(truncPow2(2.1));// 2.0writeln(truncPow2(7.0));// 4.0writeln(truncPow2(-1.9));// -1.0writeln(truncPow2(0.24));// 0.125writeln(truncPow2(-7.0));// -4.0writeln(truncPow2(double.infinity));// double.infinity
Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Fri Feb 20 17:59:19 2026

[8]ページ先頭

©2009-2026 Movatter.jp