BigInt.opCast

Implements casting to floating point types.

  1. T opCast()
  2. T opCast()
  3. T opCast()
    struct BigInt
    @safe nothrow @nogc const
    T
    opCast
    (
    T
    )
    ()
  4. T opCast()

Examples

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

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

Rounding when casting to floating point

1 // BigInts whose values cannot be exactly represented as float/double/real
2 // are rounded when cast to float/double/real. When cast to float or
3 // double or 64-bit real the rounding is strictly defined. When cast
4 // to extended-precision real the rounding rules vary by environment.
5 
6 // BigInts that fall somewhere between two non-infinite floats/doubles
7 // are rounded to the closer value when cast to float/double.
8 assert(0x1.aaa_aaep+28f == cast(float) BigInt(0x1aaa_aae7));
9 assert(0x1.aaa_ab0p+28f == cast(float) BigInt(0x1aaa_aaff));
10 assert(-0x1.aaaaaep+28f == cast(float) BigInt(-0x1aaa_aae7));
11 assert(-0x1.aaaab0p+28f == cast(float) BigInt(-0x1aaa_aaff));
12 
13 assert(0x1.aaa_aaaa_aaaa_aa00p+60 == cast(double) BigInt(0x1aaa_aaaa_aaaa_aa77));
14 assert(0x1.aaa_aaaa_aaaa_ab00p+60 == cast(double) BigInt(0x1aaa_aaaa_aaaa_aaff));
15 assert(-0x1.aaa_aaaa_aaaa_aa00p+60 == cast(double) BigInt(-0x1aaa_aaaa_aaaa_aa77));
16 assert(-0x1.aaa_aaaa_aaaa_ab00p+60 == cast(double) BigInt(-0x1aaa_aaaa_aaaa_aaff));
17 
18 // BigInts that fall exactly between two non-infinite floats/doubles
19 // are rounded away from zero when cast to float/double. (Note that
20 // in most environments this is NOT the same rounding rule rule used
21 // when casting int/long to float/double.)
22 assert(0x1.aaa_ab0p+28f == cast(float) BigInt(0x1aaa_aaf0));
23 assert(-0x1.aaaab0p+28f == cast(float) BigInt(-0x1aaa_aaf0));
24 
25 assert(0x1.aaa_aaaa_aaaa_ab00p+60 == cast(double) BigInt(0x1aaa_aaaa_aaaa_aa80));
26 assert(-0x1.aaa_aaaa_aaaa_ab00p+60 == cast(double) BigInt(-0x1aaa_aaaa_aaaa_aa80));
27 
28 // BigInts that are bounded on one side by the largest positive or
29 // most negative finite float/double and on the other side by infinity
30 // or -infinity are rounded as if in place of infinity was the value
31 // `2^^(T.max_exp)` when cast to float/double.
32 assert(float.infinity == cast(float) BigInt("999_999_999_999_999_999_999_999_999_999_999_999_999"));
33 assert(-float.infinity == cast(float) BigInt("-999_999_999_999_999_999_999_999_999_999_999_999_999"));
34 
35 assert(double.infinity > cast(double) BigInt("999_999_999_999_999_999_999_999_999_999_999_999_999"));
36 assert(real.infinity > cast(real) BigInt("999_999_999_999_999_999_999_999_999_999_999_999_999"));

Meta

Suggestion Box / Bug Report