| 1485 | } |
| 1486 | |
| 1487 | fn format_hex_float(&self, writer: &mut String, value: f64) -> Result<()> { |
| 1488 | // Handle special cases first |
| 1489 | let (sign, raw_exponent, mantissa) = value.to_parts(); |
| 1490 | let is_subnormal = raw_exponent == 0; |
| 1491 | |
| 1492 | let precision = match self.precision { |
| 1493 | NumericParam::FromArgument => None, |
| 1494 | NumericParam::Literal(p) => Some(p), |
| 1495 | }; |
| 1496 | |
| 1497 | // Determine if we need to normalize subnormal numbers |
| 1498 | // Only normalize when precision is specified and less than full mantissa width |
| 1499 | let mantissa_hex_digits = f64::MANTISSA_BITS.div_ceil(4); // 13 for f64 |
| 1500 | let should_normalize = is_subnormal |
| 1501 | && precision.is_some() |
| 1502 | && precision.unwrap() < mantissa_hex_digits as i32; |
| 1503 | |
| 1504 | let (value, raw_exponent, mantissa) = if should_normalize { |
| 1505 | let value = value * f64::SCALEUP; |
| 1506 | let (_, raw_exponent, mantissa) = value.to_parts(); |
| 1507 | (value, raw_exponent, mantissa) |
| 1508 | } else { |
| 1509 | (value, raw_exponent, mantissa) |
| 1510 | }; |
| 1511 | |
| 1512 | let mut temp = String::new(); |
| 1513 | |
| 1514 | let sign_char = if sign { |
| 1515 | "-" |
| 1516 | } else if self.force_sign { |
| 1517 | "+" |
| 1518 | } else if self.space_sign { |
| 1519 | " " |
| 1520 | } else { |
| 1521 | "" |
| 1522 | }; |
| 1523 | match value.category() { |
| 1524 | FpCategory::Nan => { |
| 1525 | write!(&mut temp, "NaN")?; |
| 1526 | } |
| 1527 | FpCategory::Infinite => { |
| 1528 | write!(&mut temp, "{sign_char}Infinity")?; |
| 1529 | } |
| 1530 | FpCategory::Zero => { |
| 1531 | write!(&mut temp, "{sign_char}0x0.0p0")?; |
| 1532 | } |
| 1533 | _ => { |
| 1534 | let bias = i32::from(f64::EXPONENT_BIAS); |
| 1535 | // Calculate actual exponent |
| 1536 | // For subnormal numbers, the exponent is 1 - bias (not 0 - bias) |
| 1537 | let exponent = if is_subnormal && !should_normalize { |
| 1538 | 1 - bias |
| 1539 | } else { |
| 1540 | raw_exponent as i32 - bias |
| 1541 | }; |
| 1542 | |
| 1543 | // Handle precision for rounding |
| 1544 | let final_mantissa = if let Some(p) = precision { |