@@ -37,6 +37,33 @@ pub fn format_item_flo64(f: f64) -> String {
37
37
format ! ( " {}" , format_flo64( f) )
38
38
}
39
39
40
+ fn format_flo32_exp ( f : f32 , width : usize ) ->String {
41
+ if f. abs ( ) . log10 ( ) <0.0 {
42
+ return format ! ( "{f:width$e}" ) ;
43
+ }
44
+ // Leave room for the '+' sign
45
+ let formatted =format ! ( "{f:width$e}" , width = width -1 ) ;
46
+ formatted. replace ( 'e' , "e+" )
47
+ }
48
+
49
+ fn format_flo64_exp ( f : f64 , width : usize ) ->String {
50
+ if f. abs ( ) . log10 ( ) <0.0 {
51
+ return format ! ( "{f:width$e}" ) ;
52
+ }
53
+ // Leave room for the '+' sign
54
+ let formatted =format ! ( "{f:width$e}" , width = width -1 ) ;
55
+ formatted. replace ( 'e' , "e+" )
56
+ }
57
+
58
+ fn format_flo64_exp_precision ( f : f64 , width : usize , precision : usize ) ->String {
59
+ if f. abs ( ) . log10 ( ) <0.0 {
60
+ return format ! ( "{f:width$.precision$e}" ) ;
61
+ }
62
+ // Leave room for the '+' sign
63
+ let formatted =format ! ( "{f:width$.precision$e}" , width = width -1 ) ;
64
+ formatted. replace ( 'e' , "e+" )
65
+ }
66
+
40
67
fn format_flo16 ( f : f16 ) ->String {
41
68
format_float ( f64:: from ( f) , 9 , 4 )
42
69
}
@@ -49,7 +76,7 @@ fn format_flo32(f: f32) -> String {
49
76
50
77
if f. classify ( ) ==FpCategory :: Subnormal {
51
78
// subnormal numbers will be normal as f64, so will print with a wrong precision
52
- format ! ( "{f: width$e}" ) // subnormal numbers
79
+ format_flo32_exp ( f , width) // subnormal numbers
53
80
} else {
54
81
format_float ( f64:: from ( f) , width, precision)
55
82
}
@@ -67,7 +94,7 @@ fn format_float(f: f64, width: usize, precision: usize) -> String {
67
94
if f ==0.0 || !f. is_finite ( ) {
68
95
return format ! ( "{f:width$}" ) ;
69
96
}
70
- return format ! ( "{f: width$e}" ) ; // subnormal numbers
97
+ return format_flo64_exp ( f , width) ; // subnormal numbers
71
98
}
72
99
73
100
let mut l = f. abs ( ) . log10 ( ) . floor ( ) as i32 ;
@@ -83,7 +110,7 @@ fn format_float(f: f64, width: usize, precision: usize) -> String {
83
110
} else if l == -1 {
84
111
format ! ( "{f:width$.precision$}" )
85
112
} else {
86
- format ! ( "{f: width$.dec$e}" , dec = precision -1 )
113
+ return format_flo64_exp_precision ( f , width, precision -1 ) ; // subnormal numbers
87
114
}
88
115
}
89
116
@@ -108,11 +135,11 @@ fn test_format_flo32() {
108
135
assert_eq ! ( format_flo32( 9_999_999.0 ) , " 9999999.0" ) ;
109
136
assert_eq ! ( format_flo32( 10_000_000.0 ) , " 10000000" ) ;
110
137
assert_eq ! ( format_flo32( 99_999_992.0 ) , " 99999992" ) ;
111
- assert_eq ! ( format_flo32( 100_000_000.0 ) , " 1.0000000e8 " ) ;
112
- assert_eq ! ( format_flo32( 9.999_999_4e8 ) , " 9.9999994e8 " ) ;
113
- assert_eq ! ( format_flo32( 1.0e9 ) , " 1.0000000e9 " ) ;
114
- assert_eq ! ( format_flo32( 9.999_999_0e9 ) , " 9.9999990e9 " ) ;
115
- assert_eq ! ( format_flo32( 1.0e10 ) , " 1.0000000e10 " ) ;
138
+ assert_eq ! ( format_flo32( 100_000_000.0 ) , "1.0000000e+8 " ) ;
139
+ assert_eq ! ( format_flo32( 9.999_999_4e8 ) , "9.9999994e+8 " ) ;
140
+ assert_eq ! ( format_flo32( 1.0e9 ) , "1.0000000e+9 " ) ;
141
+ assert_eq ! ( format_flo32( 9.999_999_0e9 ) , "9.9999990e+9 " ) ;
142
+ assert_eq ! ( format_flo32( 1.0e10 ) , "1.0000000e+10 " ) ;
116
143
117
144
assert_eq ! ( format_flo32( 0.1 ) , " 0.10000000" ) ;
118
145
assert_eq ! ( format_flo32( 0.999_999_94 ) , " 0.99999994" ) ;
@@ -138,11 +165,11 @@ fn test_format_flo32() {
138
165
assert_eq ! ( format_flo32( -9_999_999.0 ) , " -9999999.0" ) ;
139
166
assert_eq ! ( format_flo32( -10_000_000.0 ) , " -10000000" ) ;
140
167
assert_eq ! ( format_flo32( -99_999_992.0 ) , " -99999992" ) ;
141
- assert_eq ! ( format_flo32( -100_000_000.0 ) , " -1.0000000e8 " ) ;
142
- assert_eq ! ( format_flo32( -9.999_999_4e8 ) , " -9.9999994e8 " ) ;
143
- assert_eq ! ( format_flo32( -1.0e9 ) , " -1.0000000e9 " ) ;
144
- assert_eq ! ( format_flo32( -9.999_999_0e9 ) , " -9.9999990e9 " ) ;
145
- assert_eq ! ( format_flo32( -1.0e10 ) , " -1.0000000e10 " ) ;
168
+ assert_eq ! ( format_flo32( -100_000_000.0 ) , " -1.0000000e+8 " ) ;
169
+ assert_eq ! ( format_flo32( -9.999_999_4e8 ) , " -9.9999994e+8 " ) ;
170
+ assert_eq ! ( format_flo32( -1.0e9 ) , " -1.0000000e+9 " ) ;
171
+ assert_eq ! ( format_flo32( -9.999_999_0e9 ) , " -9.9999990e+9 " ) ;
172
+ assert_eq ! ( format_flo32( -1.0e10 ) , "-1.0000000e+10 " ) ;
146
173
147
174
assert_eq ! ( format_flo32( -0.1 ) , " -0.10000000" ) ;
148
175
assert_eq ! ( format_flo32( -0.999_999_94 ) , " -0.99999994" ) ;
@@ -151,13 +178,13 @@ fn test_format_flo32() {
151
178
assert_eq ! ( format_flo32( -0.001 ) , " -1.0000000e-3" ) ;
152
179
assert_eq ! ( format_flo32( -0.009_999_999_8 ) , " -9.9999998e-3" ) ;
153
180
154
- assert_eq ! ( format_flo32( 3.402_823_3e38 ) , " 3.4028233e38 " ) ;
155
- assert_eq ! ( format_flo32( -3.402_823_3e38 ) , " -3.4028233e38 " ) ;
181
+ assert_eq ! ( format_flo32( 3.402_823_3e38 ) , "3.4028233e+38 " ) ;
182
+ assert_eq ! ( format_flo32( -3.402_823_3e38 ) , "-3.4028233e+38 " ) ;
156
183
assert_eq ! ( format_flo32( -1.166_310_8e-38 ) , "-1.1663108e-38" ) ;
157
184
assert_eq ! ( format_flo32( -4.701_977_1e-38 ) , "-4.7019771e-38" ) ;
158
185
assert_eq ! ( format_flo32( 1e-45 ) , " 1e-45" ) ;
159
186
160
- assert_eq ! ( format_flo32( -3.402_823_466e+38 ) , " -3.4028235e38 " ) ;
187
+ assert_eq ! ( format_flo32( -3.402_823_466e+38 ) , "-3.4028235e+38 " ) ;
161
188
assert_eq ! ( format_flo32( f32 :: NAN ) , " NaN" ) ;
162
189
assert_eq ! ( format_flo32( f32 :: INFINITY ) , " inf" ) ;
163
190
assert_eq ! ( format_flo32( f32 :: NEG_INFINITY ) , " -inf" ) ;
@@ -180,7 +207,7 @@ fn test_format_flo64() {
180
207
) ;
181
208
assert_eq ! (
182
209
format_flo64( 100_000_000_000_000_000.0 ) ,
183
- " 1.0000000000000000e17 "
210
+ "1.0000000000000000e+17 "
184
211
) ;
185
212
186
213
assert_eq ! ( format_flo64( -0.1 ) , " -0.10000000000000001" ) ;
@@ -210,13 +237,13 @@ fn test_format_flo16() {
210
237
assert_eq ! ( format_flo16( f16:: from_f32( 10.0 ) ) , " 10.00" ) ;
211
238
assert_eq ! ( format_flo16( f16:: from_f32( 100.0 ) ) , " 100.0" ) ;
212
239
assert_eq ! ( format_flo16( f16:: from_f32( 1000.0 ) ) , " 1000" ) ;
213
- assert_eq ! ( format_flo16( f16:: from_f32( 10000.0 ) ) , " 1.000e4 " ) ;
240
+ assert_eq ! ( format_flo16( f16:: from_f32( 10000.0 ) ) , "1.000e+4 " ) ;
214
241
215
242
assert_eq ! ( format_flo16( f16:: from_f32( -0.2 ) ) , " -0.2000" ) ;
216
243
assert_eq ! ( format_flo16( f16:: from_f32( -0.02 ) ) , "-2.000e-2" ) ;
217
244
218
245
assert_eq ! ( format_flo16( f16:: MIN_POSITIVE_SUBNORMAL ) , " 5.960e-8" ) ;
219
- assert_eq ! ( format_flo16( f16:: MIN ) , " -6.550e4 " ) ;
246
+ assert_eq ! ( format_flo16( f16:: MIN ) , "-6.550e+4 " ) ;
220
247
assert_eq ! ( format_flo16( f16:: NAN ) , " NaN" ) ;
221
248
assert_eq ! ( format_flo16( f16:: INFINITY ) , " inf" ) ;
222
249
assert_eq ! ( format_flo16( f16:: NEG_INFINITY ) , " -inf" ) ;