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

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

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(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

// 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(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

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

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.