@@ -535,30 +535,39 @@ def sprintf_format(
535
535
536
536
elif placeholder .type in ("e" , "E" ):
537
537
try :
538
- if placeholder .precision :
538
+ # Get precision (default to 6 if not specified)
539
+ if placeholder .precision is not None :
539
540
try :
540
- precision = (
541
- int (placeholder .precision ) if placeholder .precision else 6
542
- )
541
+ precision = int (placeholder .precision ) if placeholder .precision else 6
543
542
except ValueError :
544
543
precision = 6
545
544
else :
546
545
precision = 6
547
-
546
+
548
547
# Format with scientific notation
549
548
float_val = float (arg )
550
-
551
- # Special case for whole numbers - produce simpler output
552
- if float_val == int (float_val ):
553
- arg = f"{ int (float_val )} e+0"
549
+
550
+ # Always use full precision format (like C++)
551
+ mantissa_format = f"{{:.{ precision } f}}"
552
+ mantissa = mantissa_format .format (float_val if abs (float_val ) < 1 else float_val / (10 ** int (f"{ float_val :e} " .split ('e' )[1 ])))
553
+
554
+ # Get exponent
555
+ if float_val == 0 :
556
+ exponent = "+00"
554
557
else :
555
- # Regular scientific notation with precision
556
- arg = f"{ float_val :.{precision }e} "
557
-
558
- arg = arg .upper () if placeholder .type == "E" else arg
559
-
558
+ exponent_val = int (f"{ float_val :e} " .split ('e' )[1 ])
559
+ exponent_sign = "+" if exponent_val >= 0 else "-"
560
+ exponent = f"{ exponent_sign } { abs (exponent_val ):02d} "
561
+
562
+ # Combine to match C++ format (always showing all decimal places)
563
+ arg = f"{ mantissa } e{ exponent } "
564
+
565
+ # Handle uppercase for E format
566
+ if placeholder .type == "E" :
567
+ arg = arg .upper ()
568
+
560
569
except (ValueError , TypeError ):
561
- arg = "0.000000e+00"
570
+ arg = "0.000000e+00" if placeholder . type == "e" else "0.000000E+00"
562
571
563
572
elif placeholder .type == "f" :
564
573
try :
0 commit comments