Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › math › big › int › Const › toFloat Public Type Function toFloat Doc Source Public Type Function std.math.big.int.Const.toFloat
pub fn toFloat (self : Const , comptime Float : type , round : Round ) struct File math/big/int.zig: 2380
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432 pub fn toFloat (self : Const , comptime Float : type , round : Round ) struct { Float , Exactness } {
if (Float == comptime_float ) return self .toFloat (f128 , round );
const normalized_abs : Const = .{
.limbs = self .limbs [0 ..llnormalize (self .limbs )],
.positive = true ,
};
if (normalized_abs .eqlZero ()) return .{ if (self .positive ) 0.0 else -0.0 , .exact };
const Repr = std .math .FloatRepr (Float );
var mantissa_limbs : [calcNonZeroTwosCompLimbCount (1 + @bitSizeOf (Repr .Mantissa ))]Limb = undefined ;
var mantissa : Mutable = .{
.limbs = &mantissa_limbs ,
.positive = undefined ,
.len = undefined ,
};
var exponent = normalized_abs .bitCountAbs () - 1 ;
const exactness : Exactness = exactness : {
if (exponent <= @bitSizeOf (Repr .Normalized .Fraction )) {
mantissa .shiftLeft (normalized_abs , @intCast (@bitSizeOf (Repr .Normalized .Fraction ) - exponent ));
break :exactness .exact ;
}
const shift : usize = @intCast (exponent - @bitSizeOf (Repr .Normalized .Fraction ));
mantissa .shiftRight (normalized_abs , shift );
const final_limb_index = (shift - 1 ) / limb_bits ;
const round_bits = normalized_abs .limbs [final_limb_index ] << @truncate (-%shift ) |
@intFromBool (!std .mem .allEqual (Limb , normalized_abs .limbs [0 ..final_limb_index ], 0 ));
if (round_bits == 0 ) break :exactness .exact ;
round : switch (round ) {
.nearest_even => {
const half : Limb = 1 << (limb_bits - 1 );
if (round_bits >= half ) mantissa .addScalar (mantissa .toConst (), 1 );
if (round_bits == half ) mantissa .limbs [0 ] &= ~@as (Limb , 1 );
},
.away => mantissa .addScalar (mantissa .toConst (), 1 ),
.trunc => {},
.floor => if (!self .positive ) continue :round .away ,
.ceil => if (self .positive ) continue :round .away ,
}
break :exactness .inexact ;
};
const normalized_res : Repr .Normalized = .{
.fraction = @truncate (mantissa .toInt (Repr .Mantissa ) catch |err | switch (err ) {
error .NegativeIntoUnsigned => unreachable ,
error .TargetTooSmall => fraction : {
assert (mantissa .toConst ().orderAgainstScalar (1 << @bitSizeOf (Repr .Mantissa )).compare (.eq ));
exponent += 1 ;
break :fraction 1 << (@bitSizeOf (Repr .Mantissa ) - 1 );
},
}),
.exponent = std .math .lossyCast (Repr .Normalized .Exponent , exponent ),
};
return .{ normalized_res .reconstruct (if (self .positive ) .positive else .negative ), exactness };
}
Convert self to
Float.