英語版
このページの英語版を見る
std.bigint
任意精度('bignum')算術。
性能は小数点以下1000桁以下に最適化されている。
X86マシンでは、高度に最適化されたアセンブリルーチンが使用される。
現在、以下のアルゴリズムが実装されている:
- カラツバ乗算
- 二乗は乗算とは独立して最適化される
- 除算
- バイナリ指数
License:
Authors:
Don Clugston
ソース std/bigint.d
- struct
BigInt
; - 任意精度の整数を表す構造体。符号なし右シフト (>>>) を除くすべての算術演算がサポートされている。 ビット演算 (|,&,^,~) がサポートされ、BigInt が無限長の 2 の補数であるかのように動作する。 無限長の 2 の補数であるかのように動作する。 BigInt は copy-on-write を使って値セマンティクスを実装している。つまり のような操作はヒープ割り当てを引き起こす。 の割り当てが発生する。(しかし、ほとんどのBigInt演算では、ヒープ割り当てが避けられないことに注意されたい。 が避けられないことに注意されたい)。Examples:
BigInt a = "9588669891916142"; BigInt b = "7452469135154800"; auto c = a * b; writeln(c); // BigInt("71459266416693160362545788781600") auto d = b * a; writeln(d); // BigInt("71459266416693160362545788781600") writeln(d); // c d = c * BigInt("794628672112"); writeln(d); // BigInt("56783581982794522489042432639320434378739200") auto e = c + d; writeln(e); // BigInt("56783581982865981755459125799682980167520800") auto f = d + c; writeln(f); // e auto g = f - c; writeln(g); // d g = f - d; writeln(g); // c e = 12345678; g = c + e; auto h = g / b; auto i = g % b; writeln(h); // a writeln(i); // e BigInt j = "-0x9A56_57f4_7B83_AB78"; BigInt k = j; j ^^= 11; writeln(k^^11); // j
- this(Range)(Range
s
)
if (isBidirectionalRange!Range && isSomeChar!(ElementType!Range) && !isInfinite!Range && !isNarrowString!Range);
pure this(Range)(Ranges
)
if (isNarrowString!Range); - 10進数または16進数の文字列からBigInt 。数値は 数値は10進数または16進数リテラルの形式でなければならない。先頭に+ - 16進数の場合は、0x または0X が続く。アンダースコアは アンダースコアは、0x または数値の符号の後であれば、どの位置でも許される。Parameters:
Range s
任意の文字型の有限双方向範囲 Throws:std.conv.ConvException文字列が有効な数値を表していない場合 - this(Range)(bool
isNegative
, Rangemagnitude
)
if (isInputRange!Range && isUnsigned!(ElementType!Range) && (hasLength!Range || isForwardRange!Range) && !isInfinite!Range); - 符号と大きさからBigInt 。大きさは を満たす符号なし整数の入力範囲である。 std.range.primitives.hasLength または std.range.primitives.isForwardRange.を満たす符号なし整数である。 の要素が最上位とみなされる。Parameters:
bool isNegative
負なら真、負以外なら偽 (大きさが0の場合は無視される) Range magnitude
符号なし整数の有限範囲 Examples:ubyte[] magnitude = [1, 2, 3, 4, 5, 6]; auto b1 = BigInt(false, magnitude); writeln(cast(long)b1); // 0x01_02_03_04_05_06L auto b2 = BigInt(true, magnitude); writeln(cast(long)b2); // -0x01_02_03_04_05_06L
- pure nothrow @safe this(T)(T
x
)
if (isIntegral!T); - 組み込みの積分型からBigInt を構築する。Examples:
ulong data = 1_000_000_000_000; auto bigData = BigInt(data); writeln(bigData); // BigInt("1_000_000_000_000")
- pure nothrow @safe this(T)(T
x
)
if (is(immutable(T) == immutable(BigInt))); - 別のBigInt からBigInt を構築する。Examples:
const(BigInt) b1 = BigInt("1_234_567_890"); BigInt b2 = BigInt(b1); writeln(b2); // BigInt("1_234_567_890")
- pure nothrow @safe BigInt
opAssign
(T)(Tx
)
if (isIntegral!T); - 組み込み整数型からの代入。Examples:
auto b = BigInt("123"); b = 456; writeln(b); // BigInt("456")
- pure @nogc @safe BigInt
opAssign
(T : BigInt)(Tx
); - 別の BigInt からの代入。Examples:
auto b1 = BigInt("123"); auto b2 = BigInt("456"); b2 = b1; writeln(b2); // BigInt("123")
- pure nothrow scope @safe BigInt
opOpAssign
(string op, T)(Ty
) return
if ((op == "+" || op == "-" || op == "*" || op == "/" || op == "%" || op == ">>" || op == "<<" || op == "^^" || op == "|" || op == "&" || op == "^") && isIntegral!T); - 形式の組み込み整数からの代入演算子を実装している。 BigInt op= integer.Examples:
auto b = BigInt("1_000_000_000"); b += 12345; writeln(b); // BigInt("1_000_012_345") b /= 5; writeln(b); // BigInt("200_002_469")
- pure nothrow scope @safe BigInt
opOpAssign
(string op, T)(Ty
) return
if ((op == "+" || op == "-" || op == "*" || op == "|" || op == "&" || op == "^" || op == "/" || op == "%") && is(T : BigInt)); - BigInt op= BigInt という形式の代入演算子を実装している。Examples:
auto x = BigInt("123"); auto y = BigInt("321"); x += y; writeln(x); // BigInt("444")
- const pure nothrow scope @safe BigInt
opBinary
(string op, T)(Ty
) return
if ((op == "+" || op == "*" || op == "-" || op == "|" || op == "&" || op == "^" || op == "/" || op == "%") && is(T : BigInt)); - BigInts 間の二項演算子を実装する。Examples:
auto x = BigInt("123"); auto y = BigInt("456"); BigInt z = x * y; writeln(z); // BigInt("56088")
- const pure nothrow scope @safe BigInt
opBinary
(string op, T)(Ty
) return
if ((op == "+" || op == "*" || op == "-" || op == "/" || op == "|" || op == "&" || op == "^" || op == ">>" || op == "<<" || op == "^^") && isIntegral!T); - BigInt'と組み込み整数との間の二項演算子を実装する。Examples:
auto x = BigInt("123"); x *= 300; writeln(x); // BigInt("36900")
- const pure nothrow @safe auto
opBinary
(string op, T)(Ty
)
if (op == "%" && isIntegral!T); - 組み込み整数型による狭義剰余演算を実装する。この二項演算子は、以下の表に従って、より狭い組込み整数型を返す。 を返す。
BigInt % uint → long BigInt % long → long BigInt % ulong → BigInt BigInt % その他の型 → int Examples:auto x = BigInt("1_000_000_500"); long l = 1_000_000L; ulong ul = 2_000_000UL; int i = 500_000; short s = 30_000; assert(is(typeof(x % l) == long) && x % l == 500L); assert(is(typeof(x % ul) == BigInt) && x % ul == BigInt(500)); assert(is(typeof(x % i) == int) && x % i == 500); assert(is(typeof(x % s) == int) && x % s == 10500);
- const pure nothrow @safe BigInt
opBinaryRight
(string op, T)(Ty
)
if ((op == "+" || op == "*" || op == "|" || op == "&" || op == "^") && isIntegral!T);
const pure nothrow @safe BigIntopBinaryRight
(string op, T)(Ty
)
if (op == "-" && isIntegral!T);
const pure nothrow @safe TopBinaryRight
(string op, T)(Tx
)
if ((op == "%" || op == "/") && isIntegral!T); - 左辺と右辺に組み込み整数を持つ演算子を実装している。 BigInt 演算子を実装している。Examples:
auto x = BigInt("100"); BigInt y = 123 + x; writeln(y); // BigInt("223") BigInt z = 123 - x; writeln(z); // BigInt("23") // 組み込みの整数型をBigIntで割った場合、 // 常に組み込み型に収まる値が返されるため、 // BigIntではなく組み込み型が返される。 assert(is(typeof(1000 / x) == int)); writeln(1000 / x); // 10
- const pure nothrow @safe BigInt
opUnary
(string op)()
if (op == "+" || op == "-" || op == "~");
pure nothrow @safe BigIntopUnary
(string op)()
if (op == "++" || op == "--"); - BigInt 単項演算子を実装している。Examples:
auto x = BigInt("1234"); writeln(-x); // BigInt("-1234") ++x; writeln(x); // BigInt("1235")
- const pure @nogc @safe bool
opEquals
()(auto const ref BigInty
);
const pure nothrow @nogc @safe boolopEquals
(T)(const Ty
)
if (isIntegral!T);
const pure nothrow @nogc boolopEquals
(T)(const Ty
)
if (isFloatingPoint!T); - 他のBigInt'および組み込みの数値型とのBigInt 等号検定を実装する。 数値型との等式テストを実装する。Examples:
// BigIntをfloatまたはdoubleと比較する場合は、 // intとfloat、またはlongとdoubleを比較する場合とは異なり、 // BigIntの完全な精度が常に考慮されることに注意すること。 assert(BigInt(123456789) != cast(float) 123456789);
- const pure nothrow @nogc @safe T
opCast
(T : bool)(); - bool へのキャストを実装している。Examples:
// ゼロでない値は真とみなされる auto x = BigInt("1"); auto y = BigInt("10"); assert(x); assert(y); // ゼロ値は偽とみなされる auto z = BigInt("0"); assert(!z);
- const pure @safe T
opCast
(T : ulong)(); - 整数型へのキャストを実装する。Throws:std.conv.ConvOverflowException数値が がターゲット型の範囲を超えた場合。Examples:
import std.conv : to, ConvOverflowException; import std.exception : assertThrown; writeln(BigInt("0").to!int); // 0 writeln(BigInt("0").to!ubyte); // 0 writeln(BigInt("255").to!ubyte); // 255 assertThrown!ConvOverflowException(BigInt("256").to!ubyte); assertThrown!ConvOverflowException(BigInt("-1").to!ubyte);
- const nothrow @nogc @safe T
opCast
(T)()
if (isFloatingPoint!T); - 浮動小数点型へのキャストを実装する。Examples:
assert(cast(float) BigInt("35540592535949172786332045140593475584") == 35540592535949172786332045140593475584.0f); assert(cast(double) BigInt("35540601499647381470685035515422441472") == 35540601499647381470685035515422441472.0); assert(cast(real) BigInt("35540601499647381470685035515422441472") == 35540601499647381470685035515422441472.0L); writeln(cast(float)BigInt("-0x1345_6780_0000_0000_0000_0000_0000")); // -0x1.3456_78p+108f // -0x1.3456_78ab_cdefp+108 writeln(cast(double)BigInt("-0x1345_678a_bcde_f000_0000_0000_0000")); // -0x1.3456_78ab_cdefp+108L writeln(cast(real)BigInt("-0x1345_678a_bcde_f000_0000_0000_0000"));
Examples:浮動小数点へのキャスト時の丸め// BigIntの値がfloat/double/realで正確に表現できない場合、 // float/double/realにキャストする際に丸められる。floatまたは // double、あるいは64ビットのrealにキャストする場合は、丸め方は厳密に定義されている。 // 拡張精度のrealにキャストする場合は、丸め方のルールは環境によって異なる。 // 2つの無限大ではないfloat/doubleの間に位置するBigIntは、 // float/doubleにキャストされた際に、より近い値に丸められる。 writeln(cast(float)BigInt(0x1aaa_aae7)); // 0x1.aaa_aaep+28f writeln(cast(float)BigInt(0x1aaa_aaff)); // 0x1.aaa_ab0p+28f writeln(cast(float)BigInt(-0x1aaa_aae7)); // -0x1.aaaaaep+28f writeln(cast(float)BigInt(-0x1aaa_aaff)); // -0x1.aaaab0p+28f writeln(cast(double)BigInt(0x1aaa_aaaa_aaaa_aa77)); // 0x1.aaa_aaaa_aaaa_aa00p+60 writeln(cast(double)BigInt(0x1aaa_aaaa_aaaa_aaff)); // 0x1.aaa_aaaa_aaaa_ab00p+60 writeln(cast(double)BigInt(-0x1aaa_aaaa_aaaa_aa77)); // -0x1.aaa_aaaa_aaaa_aa00p+60 writeln(cast(double)BigInt(-0x1aaa_aaaa_aaaa_aaff)); // -0x1.aaa_aaaa_aaaa_ab00p+60 // 2つの無限大ではないfloat/doubleの間に正確に位置するBigIntは、 // float/doubleにキャストされる際にゼロから離れる方向に丸められる。(ほとんどの環境では、 // これはint/longをfloat/doubleにキャストする際に使用される // 丸めルールと同じではないことに注意。) writeln(cast(float)BigInt(0x1aaa_aaf0)); // 0x1.aaa_ab0p+28f writeln(cast(float)BigInt(-0x1aaa_aaf0)); // -0x1.aaaab0p+28f writeln(cast(double)BigInt(0x1aaa_aaaa_aaaa_aa80)); // 0x1.aaa_aaaa_aaaa_ab00p+60 writeln(cast(double)BigInt(-0x1aaa_aaaa_aaaa_aa80)); // -0x1.aaa_aaaa_aaaa_ab00p+60 // BigIntの一方の端が、最大の正の有限のfloat/doubleまたは // 最大の負の有限のfloat/doubleで制限され、もう一方の端が // 無限大または-無限大で制限されている場合、float/doubleにキャストした際に // 無限大の値が`2^^(T.max_exp)`であるかのように丸められる。 // float.infinity writeln(cast(float)BigInt("999_999_999_999_999_999_999_999_999_999_999_999_999")); // -float.infinity writeln(cast(float)BigInt("-999_999_999_999_999_999_999_999_999_999_999_999_999")); assert(cast(double) BigInt("999_999_999_999_999_999_999_999_999_999_999_999_999") < double.infinity); assert(cast(real) BigInt("999_999_999_999_999_999_999_999_999_999_999_999_999") < real.infinity);
- const pure nothrow @nogc T
opCast
(T)()
if (is(immutable(T) == immutable(BigInt))); - BigInt'へのキャストを実装する。
警告 const やimmutable からのキャストは、型システムの保証を破る可能性がある。 システムの保証を破る可能性がある。注意して使用すること。
Examples:const(BigInt) x = BigInt("123"); BigInt y = cast() x; // constを削除する writeln(y); // x
- const pure nothrow @nogc @safe int
opCmp
(const ref BigInty
);
const pure nothrow @nogc @safe intopCmp
(T)(const Ty
)
if (isIntegral!T);
const nothrow @nogc @safe intopCmp
(T)(const Ty
)
if (isFloatingPoint!T);
const pure nothrow @nogc @safe intopCmp
(T : BigInt)(const Ty
); - BigInt 、BigInt またはBigInt 、組み込み数値型との3者比較を実装する。 との3元比較を実装する。Examples:
auto x = BigInt("100"); auto y = BigInt("10"); int z = 50; const int w = 200; assert(y < x); assert(x > z); assert(z > y); assert(x < w);
Examples:auto x = BigInt("0x1abc_de80_0000_0000_0000_0000_0000_0000"); BigInt y = x - 1; BigInt z = x + 1; double d = 0x1.abcde8p124; assert(y < d); assert(z > d); assert(x >= d && x <= d); // BigIntをfloatまたはdoubleと比較する場合は、 // intとfloat、またはlongとdoubleを比較する場合とは異なり、 // BigIntの完全な精度が常に考慮されることに注意すること。 assert(BigInt(123456789) < cast(float) 123456789);
- const pure nothrow @nogc @safe long
toLong
(); - Returns:このBigInt の値をlong 、表せる範囲外の場合はlong.max/とする。long.min を表す。Examples:
auto b = BigInt("12345"); long l = b.toLong(); writeln(l); // 12345
- const pure nothrow @nogc @safe int
toInt
(); - Returns:このBigInt の値をint 、表現可能な範囲外の場合はint.max/int.min 。 を表す。Examples:
auto big = BigInt("5_000_000"); auto i = big.toInt(); writeln(i); // 5_000_000 // intに収まらないほど大きな数値は、int.maxにクランプされる。 auto tooBig = BigInt("5_000_000_000"); i = tooBig.toInt(); writeln(i); // int.max
- const pure nothrow @nogc @property @safe size_t
uintLength
(); - この数値を格納するために使用されるuintの有効数字の数。 このBigInt の絶対値は常に <232*uintLengthである。
- const pure nothrow @nogc @property @safe size_t
ulongLength
(); - この数値を格納する際に使用されるulongの有効数字の数。 このBigInt の絶対値は常に <264*ulongLengthである。
- const void
toString
(Writer)(ref scope Writersink
, stringformatString
);
const voidtoString
(Writer)(ref scope Writersink
, ref scope const FormatSpec!charf
);
const voidtoString
(scope void delegate(scope const(char)[])sink
, stringformatString
);
const voidtoString
(scope void delegate(scope const(char)[])sink
, ref scope const FormatSpec!charf
); - BigInt をstring に変換し、与えられたシンクに渡す。Parameters:
Writer sink
整形された文字列の分割されたセグメントを受け取るための OutputRange。 フォーマットされた文字列の断片的なセグメントを受け入れるための OutputRange。 string formatString
出力フォーマットを指定するフォーマット文字列。 利用可能な出力フォーマット: "d" 10進数 "o" 8進数 "x" 16進、小文字 "X" 16進数、大文字 "s" デフォルトの書式("d"と同じ) NULL デフォルトの書式("d"と同じ) Examples:toString
が直接呼び出されることはめったにない。 std.format.format:import std.format : format; auto x = BigInt("1_000_000"); x *= 12345; writeln(format("%d", x)); // "12345000000" writeln(format("%x", x)); // "2_dfd1c040" writeln(format("%X", x)); // "2_DFD1C040" writeln(format("%o", x)); // "133764340100"
- const pure nothrow @nogc @safe size_t
toHash
(); - Returns:ハッシュテーブルで使用するのに適した、BigInt'の値の一意なハッシュ。 テーブルで使用するのに適している。Examples:
toHash
が直接呼び出されることはほとんどない。 BigIntが連想配列のキーとして使われるときに暗黙的に使われる。string[BigInt] aa; aa[BigInt(123)] = "abc"; aa[BigInt(456)] = "def"; writeln(aa[BigInt(123)]); // "abc" writeln(aa[BigInt(456)]); // "def"
- const T
getDigit
(T = ulong)(size_tn
)
if (is(T == ulong) || is(T == uint)); - 全体を構成する基本表現におけるn番目の数を取得する。 BigInt.Parameters:
T 基礎となる表現を表示する "型 size_t n
取得するn番目の数。以下でなければならない。 ulongLengthまたは uintLengthT以下でなければならない。 Returns:このBigInt の表現におけるn番目のulong 。Examples:auto a = BigInt("1000"); writeln(a.ulongLength()); // 1 writeln(a.getDigit(0)); // 1000 writeln(a.uintLength()); // 1 writeln(a.getDigit!uint(0)); // 1000 auto b = BigInt("2_000_000_000_000_000_000_000_000_000"); writeln(b.ulongLength()); // 2 writeln(b.getDigit(0)); // 4584946418820579328 writeln(b.getDigit(1)); // 108420217 writeln(b.uintLength()); // 3 writeln(b.getDigit!uint(0)); // 3489660928 writeln(b.getDigit!uint(1)); // 1067516025 writeln(b.getDigit!uint(2)); // 108420217
- pure nothrow @safe string
toDecimalString
(const(BigInt)x
); - Parameters:
const(BigInt) x
BigInt を10進数に変換するstring 。 Returns:BigInt を10進数で表すstring 。Examples:auto x = BigInt("123"); x *= 1000; x += 456; auto xstr = x.toDecimalString(); writeln(xstr); // "123456"
- pure @safe string
toHex
(const(BigInt)x
); - Parameters:
const(BigInt) x
BigInt を16進数string に変換する。 Returns:BigInt を16進数(ベース16)で表すstring 。 を大文字で表す。Examples:auto x = BigInt("123"); x *= 1000; x += 456; auto xstr = x.toHex(); writeln(xstr); // "1E240"
- Unsigned!T
absUnsign
(T)(Tx
)
if (isIntegral!T); - xの絶対値を、対応する符号なし型に変換して返す。 型に変換して返す。Parameters:
T x
絶対値を返す積分値。 Returns:xの絶対値。Examples:writeln((-1).absUnsign); // 1 writeln(1.absUnsign); // 1
- pure nothrow @safe void
divMod
(const BigIntdividend
, const BigIntdivisor
, out BigIntquotient
, out BigIntremainder
); - 与えられた配当と除数の商と余りを1回の操作で求める。Parameters:Examples:
auto a = BigInt(123); auto b = BigInt(25); BigInt q, r; divMod(a, b, q, r); writeln(q); // 4 writeln(r); // 23 writeln(q * b + r); // a
- pure nothrow @safe BigInt
powmod
(BigIntbase
, BigIntexponent
, BigIntmodulus
); - オペランドに対する高速べき乗剰余計算 BigIntオペランドに対応する。Parameters:Returns:累乗モジュールの値は (base ^ exponent) % modulus である。Examples:for powmod
BigInt base = BigInt("123456789012345678901234567890"); BigInt exponent = BigInt("1234567890123456789012345678901234567"); BigInt modulus = BigInt("1234567"); BigInt result = powmod(base, exponent, modulus); writeln(result); // 359079
Copyright © 1999-2024 by the D Language Foundation
DEEPL APIにより翻訳、ところどころ修正。
このページの最新版(英語)
このページの原文(英語)
翻訳時のdmdのバージョン: 2.108.0
ドキュメントのdmdのバージョン: 2.109.1
翻訳日付 :
HTML生成日時:
編集者: dokutoku