* [Caml-list] Printf: variable field width/precision
@ 2001-10-08 16:09 Thorsten Ohl
2001-10-12 14:23 ` Xavier Leroy
0 siblings, 1 reply; 3+ messages in thread
From: Thorsten Ohl @ 2001-10-08 16:09 UTC (permalink / raw)
To: caml-list
In the C library, printf(3) supports variable field widths and
precisions with the `*' specifier.
The field width
An optional decimal digit string (with nonzero first digit)
specifying a minimum field width. [...] Instead of a decimal
digit string one may write `*' or `*m$' (for some decimal
integer m) to specify that the field width is given in the next
argument, or in the m-th argument, respectively, which must be
of type int. [...]
Unfortunately, neither the O'Caml library nor the compiler accept `*'.
If the `*' form is available, the `*m$' form is hardly needed, but I
couldn't find a way to hack around the missing `*' (handling every
conceivable width/precision pair as a special case doesn't count ...).
Is there are chance that variable field widths for the benefit of
numerical programs will be supported in the future? [It would be easy
to hack the library, but since the compiler must perform its own magic
for type safety, I couldn't distribute the code without asking users
to patch the compiler.]
Or does anybody know a nice workaround?
Merci,
-Thorsten
--
Thorsten Ohl, Physics Department, TU Darmstadt -- ohl@hep.tu-darmstadt.de
http://heplix.ikp.physik.tu-darmstadt.de/~ohl/ [<=== PGP public key here]
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] Printf: variable field width/precision
2001-10-08 16:09 [Caml-list] Printf: variable field width/precision Thorsten Ohl
@ 2001-10-12 14:23 ` Xavier Leroy
2001-10-12 17:31 ` Alan Schmitt
0 siblings, 1 reply; 3+ messages in thread
From: Xavier Leroy @ 2001-10-12 14:23 UTC (permalink / raw)
To: Thorsten Ohl; +Cc: caml-list
> In the C library, printf(3) supports variable field widths and
> precisions with the `*' specifier.
> Unfortunately, neither the O'Caml library nor the compiler accept `*'.
> If the `*' form is available, the `*m$' form is hardly needed, but I
> couldn't find a way to hack around the missing `*' (handling every
> conceivable width/precision pair as a special case doesn't count ...).
>
> Is there are chance that variable field widths for the benefit of
> numerical programs will be supported in the future?
I've never needed the '*' specifier so far, but I agree this should be
supported at some point. As far as I can say, this requires a bit of
work, though.
> [It would be easy
> to hack the library, but since the compiler must perform its own magic
> for type safety, I couldn't distribute the code without asking users
> to patch the compiler.]
Well, you could always donate us the code, which would then end up in
the standard distribution, making life really easy for users :-)
> Or does anybody know a nice workaround?
You could write a wrapper around the low-level "format_float"
primitive, i.e.
external format_float: string -> float -> string = "format_float"
let format_float_variable width prec f =
format_float (Printf.sprintf "%%%d.%de" width prec) f
Not terribly nice, I agree.
Concerning your other request:
> The C library header file <float.h> contains (among others) the
> following useful macros:
> /* Number of decimal digits of precision in a double */
> #define DBL_DIG 15
> /* Difference between 1.0 and the minimum double greater than 1.0 */
> #define DBL_EPSILON 2.2204460492503131e-16
> /* Minimum normalised double */
> #define DBL_MIN 2.2250738585072014e-308
> /* Minimum int x such that 10**x is a normalised double */
> #define DBL_MIN_10_EXP (-307)
> /* Maximum double */
> #define DBL_MAX 1.7976931348623157e+308
> /* Maximum int x such that 10**x is a representable double */
> #define DBL_MAX_10_EXP 308
> It would be great if these values were accessible from O'Caml in a
> future version so that numerical code could be made to work on
> different machines.
This is very reasonable indeed. Thanks for the suggestion.
- Xavier Leroy
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] Printf: variable field width/precision
2001-10-12 14:23 ` Xavier Leroy
@ 2001-10-12 17:31 ` Alan Schmitt
0 siblings, 0 replies; 3+ messages in thread
From: Alan Schmitt @ 2001-10-12 17:31 UTC (permalink / raw)
To: caml-list
* Xavier Leroy (xavier.leroy@inria.fr) wrote:
> > In the C library, printf(3) supports variable field widths and
> > precisions with the `*' specifier.
> > Unfortunately, neither the O'Caml library nor the compiler accept `*'.
> > If the `*' form is available, the `*m$' form is hardly needed, but I
> > couldn't find a way to hack around the missing `*' (handling every
> > conceivable width/precision pair as a special case doesn't count ...).
> >
> > Is there are chance that variable field widths for the benefit of
> > numerical programs will be supported in the future?
>
> I've never needed the '*' specifier so far, but I agree this should be
> supported at some point. As far as I can say, this requires a bit of
> work, though.
By the way, it also seems that the + format is not supported either:
# Printf.printf "%+f" 2. ;;
Bad format `%+'
Alan
--
The hacker: someone who figured things out and made something cool happen.
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2001-10-12 17:36 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-08 16:09 [Caml-list] Printf: variable field width/precision Thorsten Ohl
2001-10-12 14:23 ` Xavier Leroy
2001-10-12 17:31 ` Alan Schmitt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox