View source code
Display the source code in std/bigint.d from which this page was generated on github.
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 local clone.

std.bigint.BigInt.opCast - multiple declarations

Function BigInt.opCast

Implements casting to bool.

T opCast(T)() const pure nothrow @nogc @safe;

Example

// Non-zero values are regarded as true
auto x = BigInt("1");
auto y = BigInt("10");
assert(x);
assert(y);

// Zero value is regarded as false
auto z = BigInt("0");
assert(!z);

Function BigInt.opCast

Implements casting to integer types.

T opCast(T)() const pure @safe;

Throws

ConvOverflowException if the number exceeds the target type's range.

Example

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);

Function BigInt.opCast

Implements casting to floating point types.

T opCast(T)() const nothrow @nogc @safe
if (isFloatingPoint!T);

Example

writeln(0x1.abcd_e8p+124f); // cast(float)BigInt("0x1abc_de80_0000_0000_0000_0000_0000_0000")
// cast(double)BigInt("0x1abc_def1_2345_6000_0000_0000_0000_0000")
writeln(0x1.abcd_ef12_3456p+124);
// cast(real)BigInt("0x1abc_def1_2345_6000_0000_0000_0000_0000")
writeln(0x1.abcd_ef12_3456p+124L);

writeln(-0x1.3456_78p+108f); // cast(float)BigInt("-0x1345_6780_0000_0000_0000_0000_0000")
// cast(double)BigInt("-0x1345_678a_bcde_f000_0000_0000_0000")
writeln(-0x1.3456_78ab_cdefp+108);
// cast(real)BigInt("-0x1345_678a_bcde_f000_0000_0000_0000")
writeln(-0x1.3456_78ab_cdefp+108L);

Example

Rounding when casting to floating point

// BigInts whose values cannot be exactly represented as float/double/real
// are rounded when cast to float/double/real. When cast to float or
// double or 64-bit real the rounding is strictly defined. When cast
// to extended-precision real the rounding rules vary by environment.

// BigInts that fall somewhere between two non-infinite floats/doubles
// are rounded to the closer value when cast to float/double.
writeln(0x1.aaa_aaep+28f); // cast(float)BigInt(0x1aaa_aae7)
writeln(0x1.aaa_ab0p+28f); // cast(float)BigInt(0x1aaa_aaff)
writeln(-0x1.aaaaaep+28f); // cast(float)BigInt(-0x1aaa_aae7)
writeln(-0x1.aaaab0p+28f); // cast(float)BigInt(-0x1aaa_aaff)

writeln(0x1.aaa_aaaa_aaaa_aa00p+60); // cast(double)BigInt(0x1aaa_aaaa_aaaa_aa77)
writeln(0x1.aaa_aaaa_aaaa_ab00p+60); // cast(double)BigInt(0x1aaa_aaaa_aaaa_aaff)
writeln(-0x1.aaa_aaaa_aaaa_aa00p+60); // cast(double)BigInt(-0x1aaa_aaaa_aaaa_aa77)
writeln(-0x1.aaa_aaaa_aaaa_ab00p+60); // cast(double)BigInt(-0x1aaa_aaaa_aaaa_aaff)

// BigInts that fall exactly between two non-infinite floats/doubles
// are rounded away from zero when cast to float/double. (Note that
// in most environments this is NOT the same rounding rule rule used
// when casting int/long to float/double.)
writeln(0x1.aaa_ab0p+28f); // cast(float)BigInt(0x1aaa_aaf0)
writeln(-0x1.aaaab0p+28f); // cast(float)BigInt(-0x1aaa_aaf0)

writeln(0x1.aaa_aaaa_aaaa_ab00p+60); // cast(double)BigInt(0x1aaa_aaaa_aaaa_aa80)
writeln(-0x1.aaa_aaaa_aaaa_ab00p+60); // cast(double)BigInt(-0x1aaa_aaaa_aaaa_aa80)

// BigInts that are bounded on one side by the largest positive or
// most negative finite float/double and on the other side by infinity
// or -infinity are rounded as if in place of infinity was the value
// `2^^(T.max_exp)` when cast to float/double.
// cast(float)BigInt("999_999_999_999_999_999_999_999_999_999_999_999_999")
writeln(float.infinity);
// cast(float)BigInt("-999_999_999_999_999_999_999_999_999_999_999_999_999")
writeln(-float.infinity);

assert(double.infinity > cast(double) BigInt("999_999_999_999_999_999_999_999_999_999_999_999_999"));
assert(real.infinity > cast(real) BigInt("999_999_999_999_999_999_999_999_999_999_999_999_999"));

Function BigInt.opCast

Implements casting to/from qualified BigInt's.

T opCast(T)() const pure nothrow @nogc
if (is(immutable(T) == immutable(BigInt)));

Warning

Casting to/from const or immutable may break type system guarantees. Use with care.

Example

const(BigInt) x = BigInt("123");
BigInt y = cast() x;    // cast away const
writeln(y); // x

Authors

Don Clugston

License

Boost License 1.0.