caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: ls-ocaml-developer-2006@m-e-leypold.de
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Multiplication of matrix in C and OCaml
Date: Sat, 10 Feb 2007 16:51:55 +0100	[thread overview]
Message-ID: <n98xf6j9ic.fsf@hod.lan.m-e-leypold.de> (raw)
In-Reply-To: <200702101452.02955.jon@ffconsultancy.com> (Jon Harrop's message of "Sat, 10 Feb 2007 14:52:02 +0000")


Jon Harrop <jon@ffconsultancy.com> writes:

> On Saturday 10 February 2007 14:41, ls-ocaml-developer-2006@m-e-leypold.de 
> wrote:
>> Just to be sure: Would the compiler be wrong to optimize
>>
>>    c * q > c * k
>>
>> to just
>>
>>        q > k
>>
>> (all floats). And why?
>
> Consider c=0, q=3 and k=2:
>
> 0 * 3 > 0 * 2  --> false
>     3 > 2      --> true
>
> It is just mathematically incorrect.

Damn. I should have taken '+'. I've not been paying attention. My
question should rather have been: Is the compiler allowed to make
optimizations according to known mathematical laws?


>> If not, why, in the case above?
>
> Floats are not reals, they are just an approximation that happens to be very 
> useful. Floats do not obey the same laws (e.g. associativity). However, 
> programmers may be relying upon this fact, e.g. when doing exact float 
> arithmetic.

Apart from my obvious error above, I wonder, how much the compiler is
required to keep to that expectations and when. Some examples to
illustrate: Recently someone complained to the Gcc team that the
compiler optimizes

   a + 100 < 100 

to false in case a is an unsigned int. Actually the lnaguage standard
allows it: If the addition flows over, anything is allowed (undefined
behaviour) and if not, a+100 < 100 is just like a < 0. The expectation
that the overflow must be visible in the language was not justfied in
this case.

I've been wondering about similar optimizations for floats, but didn't
get my examples right. The transformtion done by the compiler would of
course be forbidden to increase the error. 

>> I don't want  
>> letter and verse, but a general hand waving in the right direction
>> would be nice, since I have the impression, that is exactly what Gcc
>> 4.1. is currently doing (though for the integer case).

> Ints are completely different because they are exact (as modulo integers). So 
> they do obey the same laws and they do not have special constants (like 
> infinity, neg_infinity, nan, -0. and so on as floats do).

Yes, thanks. I hope I have justfied sufficiently my motiviation to
ask my question, about which I should have been thinking just for some
more minutes (I admit :-).

Still: With a certain Gcc version and flags combination the OP saw a
threefold improvement in performance. That in intself is suspicious (I
don't think that this much optimization potential was left in Gcc ...)
and I still would check for optimization errors in this case. Gcc is
not bug free either, so one should test the correctness of the
compiled program first and wether it really does the work it is
supposed to do.

Regards -- Markus


  reply	other threads:[~2007-02-10 15:46 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-07 23:42 Frédéric Gava
2007-02-08  2:14 ` [Caml-list] " Jacques Garrigue
2007-02-08  9:27   ` Frédéric Gava
2007-02-08  9:38     ` Frédéric Gava
2007-02-08 12:08     ` Jacques Garrigue
2007-02-08  9:56   ` Frédéric Gava
2007-02-08 10:45     ` Xavier Leroy
2007-02-08 15:16       ` Frédéric Gava
2007-02-09  2:58         ` Jacques Garrigue
2007-02-09  9:06           ` ls-ocaml-developer-2006
2007-02-09 10:32             ` ls-ocaml-developer-2006
2007-02-09 14:22               ` skaller
2007-02-09 21:47                 ` ls-ocaml-developer-2006
2007-02-09 21:55                   ` Andrej Bauer
2007-02-09 22:36                     ` ls-ocaml-developer-2006
2007-02-09 23:53                       ` Jon Harrop
2007-02-10  1:41                         ` ls-ocaml-developer-2006
2007-02-10  2:24                           ` Jon Harrop
2007-02-10 14:41                             ` ls-ocaml-developer-2006
2007-02-10 14:52                               ` Jon Harrop
2007-02-10 15:51                                 ` ls-ocaml-developer-2006 [this message]
2007-02-10 16:10                                   ` Xavier Leroy
2007-02-10 16:11                                   ` Jon Harrop
2007-02-10 14:55                               ` Mattias Engdegård
2007-02-11 13:13                             ` Christophe Raffalli
2007-02-10  1:10                     ` Brian Hurt
2007-02-10  1:16                       ` Robert Roessler
2007-02-09 23:56                 ` Jon Harrop
2007-02-09 12:05             ` Jon Harrop
2007-02-09 12:35               ` ls-ocaml-developer-2006
2007-02-09 13:50             ` Brian Hurt
2007-02-09 14:23               ` Gerd Stolpmann
2007-02-09 14:24 Frederic GAVA

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=n98xf6j9ic.fsf@hod.lan.m-e-leypold.de \
    --to=ls-ocaml-developer-2006@m-e-leypold.de \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).