Enforce precision in printf %F

master
Pierre Roux 2019-02-23 15:35:39 +01:00
parent 0c9b121661
commit ed74b5b237
11 changed files with 161 additions and 167 deletions

View File

@ -51,6 +51,12 @@ Working version
* GPR#2240: Constify "identifier" in struct custom_operations
(Cedric Cellier, review by Xavier Leroy)
### Standard library:
- GPR#2262: take precision (.<n>) and flags ('+' and ' ') into account
in printf %F
(Pierre Roux, review by Gabriel Scherer)
### Other libraries:
- GPR#2248: Unix alloc_sockaddr: Fix read of uninitialized memory for an

Binary file not shown.

Binary file not shown.

View File

@ -95,7 +95,8 @@ fun ign fmt -> match ign with
(Int64 (iconv, pad_of_pad_opt pad_opt, No_precision, fmt))
| Ignored_float (pad_opt, prec_opt) ->
Param_format_EBB
(Float (Float_f, pad_of_pad_opt pad_opt, prec_of_prec_opt prec_opt, fmt))
(Float ((Float_flag_, Float_f),
pad_of_pad_opt pad_opt, prec_of_prec_opt prec_opt, fmt))
| Ignored_bool pad_opt ->
Param_format_EBB (Bool (pad_of_pad_opt pad_opt, fmt))
| Ignored_format_arg (pad_opt, fmtty) ->
@ -216,10 +217,14 @@ type precision_ebb = Precision_EBB : ('a, 'b) precision -> precision_ebb
(* Constants *)
(* Default precision for float printing. *)
let default_float_precision = -6
let default_float_precision fconv =
match snd fconv with
| Float_f | Float_e | Float_E | Float_g | Float_G | Float_h | Float_H -> -6
(* For %h and %H formats, a negative precision means "as many digits as
necessary". For the other FP formats, we take the absolute value
of the precision, hence 6 digits by default. *)
| Float_F -> 12
(* Default precision for OCaml float printing (%F). *)
(******************************************************************************)
(* Externals *)
@ -286,11 +291,12 @@ let char_of_iconv iconv = match iconv with
| Int_Co -> 'o' | Int_u | Int_Cu -> 'u'
(* Convert a float conversion to char. *)
let char_of_fconv fconv = match fconv with
| Float_f | Float_pf | Float_sf -> 'f' | Float_e | Float_pe | Float_se -> 'e'
| Float_E | Float_pE | Float_sE -> 'E' | Float_g | Float_pg | Float_sg -> 'g'
| Float_G | Float_pG | Float_sG -> 'G' | Float_F -> 'F'
| Float_h | Float_ph | Float_sh -> 'h' | Float_H | Float_pH | Float_sH -> 'H'
(* `cF' will be 'F' for displaying format and 'g' to call libc printf *)
let char_of_fconv ?(cF='F') fconv = match snd fconv with
| Float_f -> 'f' | Float_e -> 'e'
| Float_E -> 'E' | Float_g -> 'g'
| Float_G -> 'G' | Float_F -> cF
| Float_h -> 'h' | Float_H -> 'H'
(* Convert a scanning counter to char. *)
@ -433,16 +439,10 @@ let bprint_altint_fmt buf ign_flag iconv pad prec c =
(***)
(* Print the optional '+' associated to a float conversion. *)
let bprint_fconv_flag buf fconv = match fconv with
| Float_pf | Float_pe | Float_pE
| Float_pg | Float_pG | Float_ph | Float_pH ->
buffer_add_char buf '+'
| Float_sf | Float_se | Float_sE
| Float_sg | Float_sG | Float_sh | Float_sH ->
buffer_add_char buf ' '
| Float_f | Float_e | Float_E
| Float_g | Float_G | Float_F | Float_h | Float_H ->
()
let bprint_fconv_flag buf fconv = match fst fconv with
| Float_flag_p -> buffer_add_char buf '+'
| Float_flag_s -> buffer_add_char buf ' '
| Float_flag_ -> ()
(* Print a complete float format in a buffer (ex: "%+*.3f"). *)
let bprint_float_fmt buf ign_flag fconv pad prec =
@ -1406,11 +1406,10 @@ let format_of_iconvn = function
| Int_o -> "%no" | Int_Co -> "%#no"
| Int_u | Int_Cu -> "%nu"
(* Generate the format_float first argument form a float_conv. *)
(* Generate the format_float first argument from a float_conv. *)
let format_of_fconv fconv prec =
if fconv = Float_F then "%.12g" else
let prec = abs prec in
let symb = char_of_fconv fconv in
let symb = char_of_fconv ~cF:'g' fconv in
let buf = buffer_create 16 in
buffer_add_char buf '%';
bprint_fconv_flag buf fconv;
@ -1457,21 +1456,21 @@ let convert_int64 iconv n =
(* Convert a float to string. *)
(* Fix special case of "OCaml float format". *)
let convert_float fconv prec x =
match fconv with
| Float_h | Float_ph | Float_sh | Float_H | Float_pH | Float_sH ->
match snd fconv with
| Float_h | Float_H ->
let sign =
match fconv with
| Float_ph | Float_pH -> '+'
| Float_sh | Float_sH -> ' '
match fst fconv with
| Float_flag_p -> '+'
| Float_flag_s -> ' '
| _ -> '-' in
let str = hexstring_of_float x prec sign in
begin match fconv with
| Float_H | Float_pH | Float_sH -> String.uppercase_ascii str
begin match snd fconv with
| Float_H -> String.uppercase_ascii str
| _ -> str
end
| _ ->
let str = format_float (format_of_fconv fconv prec) x in
if fconv <> Float_F then str else
if snd fconv <> Float_F then str else
let len = String.length str in
let rec is_valid i =
if i = len then false else
@ -1733,7 +1732,7 @@ and make_float_padding_precision : type x y a b c d e f .
fun k acc fmt pad prec fconv -> match pad, prec with
| No_padding, No_precision ->
fun x ->
let str = convert_float fconv default_float_precision x in
let str = convert_float fconv (default_float_precision fconv) x in
make_printf k (Acc_data_string (acc, str)) fmt
| No_padding, Lit_precision p ->
fun x ->
@ -1745,7 +1744,7 @@ and make_float_padding_precision : type x y a b c d e f .
make_printf k (Acc_data_string (acc, str)) fmt
| Lit_padding (padty, w), No_precision ->
fun x ->
let str = convert_float fconv default_float_precision x in
let str = convert_float fconv (default_float_precision fconv) x in
let str' = fix_padding padty w str in
make_printf k (Acc_data_string (acc, str')) fmt
| Lit_padding (padty, w), Lit_precision p ->
@ -1758,7 +1757,7 @@ and make_float_padding_precision : type x y a b c d e f .
make_printf k (Acc_data_string (acc, str)) fmt
| Arg_padding padty, No_precision ->
fun w x ->
let str = convert_float fconv default_float_precision x in
let str = convert_float fconv (default_float_precision fconv) x in
let str' = fix_padding padty w str in
make_printf k (Acc_data_string (acc, str')) fmt
| Arg_padding padty, Lit_precision p ->
@ -2940,39 +2939,27 @@ let fmt_ebb_of_string ?legacy_behavior str =
else incompatible_flag pct_ind str_ind symb "'+'"
| false, _, false, _ -> assert false
(* Convert (plus, symb) to its associated float_conv. *)
(* Convert (plus, space, symb) to its associated float_conv. *)
and compute_float_conv pct_ind str_ind plus space symb =
match plus, space, symb with
| false, false, 'f' -> Float_f | false, false, 'e' -> Float_e
| false, true, 'f' -> Float_sf | false, true, 'e' -> Float_se
| true, false, 'f' -> Float_pf | true, false, 'e' -> Float_pe
| false, false, 'E' -> Float_E | false, false, 'g' -> Float_g
| false, true, 'E' -> Float_sE | false, true, 'g' -> Float_sg
| true, false, 'E' -> Float_pE | true, false, 'g' -> Float_pg
| false, false, 'G' -> Float_G
| false, true, 'G' -> Float_sG
| true, false, 'G' -> Float_pG
| false, false, 'h' -> Float_h
| false, true, 'h' -> Float_sh
| true, false, 'h' -> Float_ph
| false, false, 'H' -> Float_H
| false, true, 'H' -> Float_sH
| true, false, 'H' -> Float_pH
| false, false, 'F' -> Float_F
| true, true, _ ->
if legacy_behavior then
(* plus and space: legacy implementation prefers plus *)
compute_float_conv pct_ind str_ind plus false symb
else incompatible_flag pct_ind str_ind ' ' "'+'"
| false, true, _ ->
if legacy_behavior then (* ignore *)
compute_float_conv pct_ind str_ind plus false symb
else incompatible_flag pct_ind str_ind symb "' '"
| true, false, _ ->
if legacy_behavior then (* ignore *)
compute_float_conv pct_ind str_ind false space symb
else incompatible_flag pct_ind str_ind symb "'+'"
| false, false, _ -> assert false
let flag = match plus, space with
| false, false -> Float_flag_
| false, true -> Float_flag_s
| true, false -> Float_flag_p
| true, true ->
(* plus and space: legacy implementation prefers plus *)
if legacy_behavior then Float_flag_p
else incompatible_flag pct_ind str_ind ' ' "'+'" in
let kind = match symb with
| 'f' -> Float_f
| 'e' -> Float_e
| 'E' -> Float_E
| 'g' -> Float_g
| 'G' -> Float_G
| 'h' -> Float_h
| 'H' -> Float_H
| 'F' -> Float_F
| _ -> assert false in
flag, kind
(* Raise [Failure] with a friendly error message about incompatible options.*)
and incompatible_flag : type a . int -> int -> char -> string -> a =

View File

@ -32,15 +32,20 @@ type int_conv =
| Int_Cd | Int_Ci | Int_Cu (* %#d | %#i | %#u *)
(* Float conversion. *)
type float_conv =
| Float_f | Float_pf | Float_sf (* %f | %+f | % f *)
| Float_e | Float_pe | Float_se (* %e | %+e | % e *)
| Float_E | Float_pE | Float_sE (* %E | %+E | % E *)
| Float_g | Float_pg | Float_sg (* %g | %+g | % g *)
| Float_G | Float_pG | Float_sG (* %G | %+G | % G *)
| Float_F (* %F *)
| Float_h | Float_ph | Float_sh (* %h | %+h | % h *)
| Float_H | Float_pH | Float_sH (* %H | %+H | % H *)
type float_flag_conv =
| Float_flag_ (* %[feEgGFhH] *)
| Float_flag_p (* %+[feEgGFhH] *)
| Float_flag_s (* % [feEgGFhH] *)
type float_kind_conv =
| Float_f (* %f | %+f | % f *)
| Float_e (* %e | %+e | % e *)
| Float_E (* %E | %+E | % E *)
| Float_g (* %g | %+g | % g *)
| Float_G (* %G | %+G | % G *)
| Float_F (* %F | %+F | % F *)
| Float_h (* %h | %+h | % h *)
| Float_H (* %H | %+H | % H *)
type float_conv = float_flag_conv * float_kind_conv
(***)
@ -386,7 +391,7 @@ and ('a, 'b, 'c, 'd, 'e, 'f) fmt =
int_conv * ('x, 'y) padding * ('y, int64 -> 'a) precision *
('a, 'b, 'c, 'd, 'e, 'f) fmt ->
('x, 'b, 'c, 'd, 'e, 'f) fmt
| Float : (* %[feEgGF] *)
| Float : (* %[feEgGFhH] *)
float_conv * ('x, 'y) padding * ('y, float -> 'a) precision *
('a, 'b, 'c, 'd, 'e, 'f) fmt ->
('x, 'b, 'c, 'd, 'e, 'f) fmt

View File

@ -22,11 +22,12 @@ type int_conv =
| Int_x | Int_Cx | Int_X | Int_CX | Int_o | Int_Co | Int_u
| Int_Cd | Int_Ci | Int_Cu
type float_conv =
| Float_f | Float_pf | Float_sf | Float_e | Float_pe | Float_se
| Float_E | Float_pE | Float_sE | Float_g | Float_pg | Float_sg
| Float_G | Float_pG | Float_sG | Float_F
| Float_h | Float_ph | Float_sh | Float_H | Float_pH | Float_sH
type float_flag_conv =
| Float_flag_ | Float_flag_p | Float_flag_s
type float_kind_conv =
| Float_f | Float_e | Float_E | Float_g | Float_G
| Float_F | Float_h | Float_H
type float_conv = float_flag_conv * float_kind_conv
type char_set = string
@ -197,7 +198,7 @@ and ('a, 'b, 'c, 'd, 'e, 'f) fmt =
int_conv * ('x, 'y) padding * ('y, int64 -> 'a) precision *
('a, 'b, 'c, 'd, 'e, 'f) fmt ->
('x, 'b, 'c, 'd, 'e, 'f) fmt
| Float : (* %[feEgGF] *)
| Float : (* %[feEgGFhH] *)
float_conv * ('x, 'y) padding * ('y, float -> 'a) precision *
('a, 'b, 'c, 'd, 'e, 'f) fmt ->
('x, 'b, 'c, 'd, 'e, 'f) fmt

View File

@ -61,7 +61,7 @@ val fprintf : out_channel -> ('a, out_channel, unit) format -> 'a
and the decimal-point character is removed if there is no fractional
part remaining.
- [h] or [H]: convert a floating-point argument to hexadecimal notation,
in the style [0xh.hhhh e+-dd] (hexadecimal mantissa, exponent in
in the style [0xh.hhhh p+-dd] (hexadecimal mantissa, exponent in
decimal and denotes a power of 2).
- [B]: convert a boolean argument to the string [true] or [false]
- [b]: convert a boolean argument (deprecated; do not use in new
@ -110,8 +110,9 @@ val fprintf : out_channel -> ('a, out_channel, unit) format -> 'a
The optional [precision] is a dot [.] followed by an integer
indicating how many digits follow the decimal point in the [%f],
[%e], and [%E] conversions. For instance, [%.4f] prints a [float] with
4 fractional digits.
[%e], [%E], [%h], and [%H] conversions or the maximum number of
significant digits to appear for the [%F], [%g] and [%G] conversions.
For instance, [%.4f] prints a [float] with 4 fractional digits.
The integer in a [width] or [precision] can also be specified as
[*], in which case an extra integer argument is taken to specify

View File

@ -1348,14 +1348,12 @@ fun ib fmt readers -> match fmt with
let c = integer_conversion_of_char (char_of_iconv iconv) in
let scan width _ ib = scan_int_conversion c width ib in
pad_prec_scanf ib rest readers pad prec scan (token_int64 c)
| Float (Float_F, pad, prec, rest) ->
| Float ((_, Float_F), pad, prec, rest) ->
pad_prec_scanf ib rest readers pad prec scan_caml_float token_float
| Float ((Float_f | Float_pf | Float_sf | Float_e | Float_pe | Float_se
| Float_E | Float_pE | Float_sE | Float_g | Float_pg | Float_sg
| Float_G | Float_pG | Float_sG), pad, prec, rest) ->
pad_prec_scanf ib rest readers pad prec scan_float token_float
| Float ((Float_h | Float_ph | Float_sh | Float_H | Float_pH | Float_sH),
| Float ((_, (Float_f | Float_e | Float_E | Float_g | Float_G)),
pad, prec, rest) ->
pad_prec_scanf ib rest readers pad prec scan_float token_float
| Float ((_, (Float_h | Float_H)), pad, prec, rest) ->
pad_prec_scanf ib rest readers pad prec scan_hex_float token_float
| Bool (pad, rest) ->
let scan _ _ ib = scan_bool ib in

View File

@ -285,15 +285,11 @@ try
test (sprintf "%4F" 3. = " 3.");
test (sprintf "%-4F" 3. = "3. ");
test (sprintf "%04F" 3. = "003.");
(* plus-padding unsupported
test (sprintf "%+4F" 3. = " +3.");
*)
(* no precision
test (sprintf "%.3F" 42.42 = "42.420");
test (sprintf "%12.3F" 42.42e42 = " 4.242e+43");
test (sprintf "%.3F" 42.00 = "42.000");
test (sprintf "%.3F" 0.0042 = "0.004");
*)
test (sprintf "%.3F" 42.42 = "42.4");
test (sprintf "%12.3F" 42.42e42 =* " 4.24e+43");
test (sprintf "%.3F" 42.00 = "42.");
test (sprintf "%.3F" 0.0042 = "0.0042");
printf "\nh\n%!";
test (sprintf "%+h" (+0.) = "+0x0p+0");
@ -373,23 +369,27 @@ try
(*test (sprintf "%-0+ #14.3E" 42.42 =* "+4.242E+01 ");*)
(* >> '-' is incompatible with '0', '#' is incompatible with 'E' *)
(* %g gives strange results that correspond to neither %f nor %e
printf "\ng\n%!";
test (sprintf "%g" (-42.42) = "-42.42000");
test (sprintf "%-15g" (-42.42) = "-42.42000 ");
test (sprintf "%015g" (-42.42) = "-00000042.42000");
test (sprintf "%+g" 42.42 = "+42.42000");
test (sprintf "% g" 42.42 = " 42.42000");
test (sprintf "%#g" 42.42 = "42.42000");
test (sprintf "%15g" 42.42 = " 42.42000");
test (sprintf "%*g" 14 42.42 = " 42.42000");
test (sprintf "%-0+ #14g" 42.42 = "+42.42000 ");
test (sprintf "%.3g" (-42.42) = "-42.420");
*)
test (sprintf "%g" (-42.42) = "-42.42");
test (sprintf "%.3g" (-4242.) =* "-4.24e+03");
test (sprintf "%-15g" (-42.42) = "-42.42 ");
test (sprintf "%015g" (-42.42) = "-00000000042.42");
test (sprintf "%+g" 42.42 = "+42.42");
test (sprintf "% g" 42.42 = " 42.42");
test (sprintf "%15g" 42.42 = " 42.42");
test (sprintf "%*g" 14 42.42 = " 42.42");
test (sprintf "%.3g" (-42.42) = "-42.4");
(* Same for %G
printf "\nG\n%!";
*)
test (sprintf "%G" (-42.42) = "-42.42");
test (sprintf "%.3G" (-4242.) =* "-4.24E+03");
test (sprintf "%-15G" (-42.42) = "-42.42 ");
test (sprintf "%015G" (-42.42) = "-00000000042.42");
test (sprintf "%+G" 42.42 = "+42.42");
test (sprintf "% G" 42.42 = " 42.42");
test (sprintf "%15G" 42.42 = " 42.42");
test (sprintf "%*G" 14 42.42 = " 42.42");
test (sprintf "%.3G" (-42.42) = "-42.4");
printf "\nB\n%!";
test (sprintf "%B" true = "true");

View File

@ -29,67 +29,71 @@ C
f
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
F
107 108 109 110 111 112 113
107 108 109 110 111 112 113 114 115 116 117 118
h
114 115 116 117 118 119 120 121 122 123 124 125 126
119 120 121 122 123 124 125 126 127 128 129 130 131
H
127 128 129 130 131 132 133 134 135 136 137 138 139
132 133 134 135 136 137 138 139 140 141 142 143 144
e
140 141 142 143 144 145 146 147 148 149 150 151 152 153
145 146 147 148 149 150 151 152 153 154 155 156 157 158
E
154 155 156 157 158 159 160 161 162 163 164 165 166 167
159 160 161 162 163 164 165 166 167 168 169 170 171 172
g
173 174 175 176 177 178 179 180 181
G
182 183 184 185 186 187 188 189 190
B
168 169 170 171
191 192 193 194
ld/li positive
172 173 174 175 176 177 178
195 196 197 198 199 200 201
ld/li negative
179 180 181 182 183 184 185
202 203 204 205 206 207 208
lu positive
186 187 188 189 190
209 210 211 212 213
lu negative
191
214
lx positive
192 193 194 195 196 197
215 216 217 218 219 220
lx negative
198
221
lX positive
199 200 201 202 203 204
222 223 224 225 226 227
lx negative
205
lo positive
206 207 208 209 210 211
lo negative
212
Ld/Li positive
213 214 215 216 217
Ld/Li negative
218 219 220 221 222
Lu positive
223 224 225 226 227
Lu negative
228
Lx positive
lo positive
229 230 231 232 233 234
Lx negative
lo negative
235
LX positive
236 237 238 239 240 241
Lx negative
242
Lo positive
243 244 245 246 247 248
Lo negative
249
a
250
t
Ld/Li positive
236 237 238 239 240
Ld/Li negative
241 242 243 244 245
Lu positive
246 247 248 249 250
Lu negative
251
Lx positive
252 253 254 255 256 257
Lx negative
258
LX positive
259 260 261 262 263 264
Lx negative
265
Lo positive
266 267 268 269 270 271
Lo negative
272
a
273
t
274
{...%}
252
275
(...%)
253
276
! % @ , and constants
254 255 256 257 258 259 260
277 278 279 280 281 282 283
end of tests
All tests succeeded.

View File

@ -3615,29 +3615,21 @@ and type_format loc str env =
| Int_o -> mk_constr "Int_o" [] | Int_Co -> mk_constr "Int_Co" []
| Int_u -> mk_constr "Int_u" [] | Int_Cd -> mk_constr "Int_Cd" []
| Int_Ci -> mk_constr "Int_Ci" [] | Int_Cu -> mk_constr "Int_Cu" []
and mk_fconv fconv = match fconv with
and mk_fconv fconv =
let flag = match fst fconv with
| Float_flag_ -> mk_constr "Float_flag_" []
| Float_flag_p -> mk_constr "Float_flag_p" []
| Float_flag_s -> mk_constr "Float_flag_s" [] in
let kind = match snd fconv with
| Float_f -> mk_constr "Float_f" []
| Float_pf -> mk_constr "Float_pf" []
| Float_sf -> mk_constr "Float_sf" []
| Float_e -> mk_constr "Float_e" []
| Float_pe -> mk_constr "Float_pe" []
| Float_se -> mk_constr "Float_se" []
| Float_E -> mk_constr "Float_E" []
| Float_pE -> mk_constr "Float_pE" []
| Float_sE -> mk_constr "Float_sE" []
| Float_g -> mk_constr "Float_g" []
| Float_pg -> mk_constr "Float_pg" []
| Float_sg -> mk_constr "Float_sg" []
| Float_G -> mk_constr "Float_G" []
| Float_pG -> mk_constr "Float_pG" []
| Float_sG -> mk_constr "Float_sG" []
| Float_h -> mk_constr "Float_h" []
| Float_ph -> mk_constr "Float_ph" []
| Float_sh -> mk_constr "Float_sh" []
| Float_H -> mk_constr "Float_H" []
| Float_pH -> mk_constr "Float_pH" []
| Float_sH -> mk_constr "Float_sH" []
| Float_F -> mk_constr "Float_F" []
| Float_F -> mk_constr "Float_F" [] in
mk_exp_loc (Pexp_tuple [flag; kind])
and mk_counter cnt = match cnt with
| Line_counter -> mk_constr "Line_counter" []
| Char_counter -> mk_constr "Char_counter" []