Perl `pack` Template Comparison: "d" vs "F" =========================================== Summary Table ------------- | Template | Type | Precision | Bits | Description | |----------|--------|--------------|------|------------------------------------| | "d" | double | 64-bit float | 64 | IEEE 754 double-precision float | | "F" | float | 32-bit float | 32 | IEEE 754 single-precision float | Example Code ------------ use feature 'say'; my $x = 1/3; say "Double:"; say unpack("B64", pack("d>", $x)); # Big-endian double say "Float:"; say unpack("B32", pack("F>", $x)); # Big-endian float Difference Illustration ----------------------- my $x = 1/3; my $float_bin = unpack "B32", pack "F>", $x; my $double_bin = unpack "B64", pack "d>", $x; printf " Float (32-bit): %s\n", $float_bin; printf "Double (64-bit): %s\n", $double_bin; Expected Output: Float (32-bit): 00111110101010101010101010101011 Double (64-bit): 0011111111010101010101010101010101010101010101010101010101010101 IEEE 754 Format Summary ----------------------- | Precision | Sign | Exponent | Mantissa (Significand) | Bias | |-----------|------|----------|-------------------------|------| | Float | 1 bit | 8 bits | 23 bits | 127 | | Double | 1 bit | 11 bits | 52 bits | 1023 | Usage Notes ----------- - Use "d" / "d>" if you want: - Higher precision - Full 64-bit IEEE 754 compliance - Direct compatibility with most CPUs' `double` type - Use "F" / "F>" if you want: - Reduced memory size - Exact compatibility with 32-bit float structures (e.g., in network protocols or embedded devices) Endianness Reminder ------------------- - d> and F> -> big-endian - d< and F< -> little-endian - d and F (no angle) -> native-endian (machine dependent)