Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
OCaml and C math routines
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2000-10-14 (12:40)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: OCaml and C math routines
> OCaml'ers... I would be very interested to hear your response to this!

Here is a copy of the response I posted on

Zhong Shao <> writes:

> From what I know, for the floating-point intensive benchmarks, Ocaml
> is faster than SML/NJ mainly because Ocaml directly calls the native,
> C-based math library (which in turn is fine-tuned for the actual
> processor).  SML/NJ, on the other hand, implements all of the math
> library functions (e.g., cos, sin, tan) in ML itself (as part of the
> SML basis).

Thanks, Zhong, for this interesting piece of information.  I had no
idea that the SML Basis Library took such a "bare-bone" approach...

> Members of the SML basis committee and the Ocaml group probably can
> tell us the pros and cons of calling the native math library vs. having
> it written in ML itself.

Short answer: the idea of writing the math functions in ML itself
never even crossed my mind, and I can see absolutely no reason why one
would want to do so.

Longer answer: here are a bunch of reasons why calling the native math
library makes much more sense.

1- Availability: any Unix system, and any C compiler for other OS,
come with a math library conforming to ANSI (for the API) and IEEE
(for the semantics of the math operations).  So there are no
portability issues in using the native math library.

2- Correctness: floating-point arithmetic is surprisingly tricky.  These
native math libraries are widely used, in C and Fortran, by demanding
numerical users, so chances are that they are more correct than
anything we can write ourselves.  (And if they are not correct,
someone else gets to fix them, not us.)  Some of these libraries were
written by leading experts in the field (I'm thinking of Apple's SANE
library, which I believe was originally written by Prof. Kahan, one of
the biggest names in the area.)  So, I trust those libraries to be
more correct than anything I could write myself.

3- Speed: we can safely expect the native math library to be nearly
optimal in terms of performances, including processor-specific
optimizations, e.g. critical parts are written in assembly, exploiting
all resources of the target processor.  Sometimes, these libraries are
even written by the same people that designed the processor (I'm
thinking of Motorola's library for the Power PC).  So, I trust those
libraries to be faster than anything I could write myself.

4- Economy of means: implementors of ML have very limited human
resources, so it makes much more sense to interface with good existing
C libraries rather than redo everything from scratch.  In many areas,
it's not obvious to find good C libraries that do what we want, so
reimplementation in ML might be justified; but in the case of
floating-point math functions, I really don't see anything to hold
against the existing math libraries.  So, reimplementing exp() or sin()
myself would just be a waste of my time (and rather boring, in addition).

Now, I'd really love to hear from the SML Basis Library designers and
see what reason they can come up with to justify implementing the math
functions in ML.

- Xavier Leroy

Valid e-mail address (without the underscores): Xavier.Leroy@i_n_r_i_a.f_r
This is a protection against junk mail. Apologies for the inconvenience.
Home page: