Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Printf: variable field width/precision
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <xavier.leroy@i...>
Subject: Re: [Caml-list] Printf: variable field width/precision
> 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