how long is double

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Francis Glassborow

    #31
    Re: how long is double

    In message <3ffa369d$0$323 29$9a6e19ea@new s.newshosting.c om>, Ron Natalie
    <ron@sensor.com > writes[color=blue][color=green]
    > > Suppose I have expression, like
    > >
    > > double d = 1.1 + x + 2.2 + 3.3; // we have double x in scope
    > >
    > > here the compiler must generate code that will do 3 separate additions an
    > > that order? And emitting code equivalent to expression
    > >
    > > double d = x + 6.6;[/color]
    >
    >The compiler is free to reorder the expression. If you want to enforce ordering
    >you have to introduce sequence points in the calculation.[/color]

    There are two places in the Standard that might be considered relevant:

    1) 1.9 para 15 (which is a note and so non-normative but can give a clue
    as to intent)

    There it requires that the operators are really associative and
    commutative -- and a non-normative footnote to the non-normative note
    adds that this is never considered to be the case for overloaded
    operators)

    It then proceeds to give source code examples re int values and fails to
    give any guidance in the case of floating point.

    When I combine the above with C's rules (which explicitly forbid
    re-ordering) I come to the conclusion that fp arithmetic operators are
    not 'really' associative and commutative and so the limited licence to
    regroup (note not re-order) does not apply. But even if it did the best
    that could be achieved with the above is:

    double d = 1.1 + x + (2.2 + 3.3);
    which the compiler could transform to :

    double d = 1.1 + x + 5.5;

    Had the writers of that section meant re-order they could have said so.

    2) Section 5 para 4 is actually no more helpful. Historically this
    formulation has not been taken as a licence for re-ordering successive
    applications of the same operator other than where explicit licence is
    granted (or can be deduced fro the as-if rule) if op is a left-to-right
    associative operator:

    a op b op c;

    must be evaluated as:
    (a op b) op c;
    and not as:
    a op (b op c);

    However:

    a op1 b op2 c op1 d;

    with op1 having a higher precedence than op2 allows for (a op1 b) and (c
    op1 d) to be evaluated in either order. That has been the normal
    interpretation of the rule with regard to order of evaluation of
    sub-expressions.

    It is probably safer with modern high optimisation implementations to
    write code with sequence points enforcing intent but I can see nothing
    in the C++ Standard that allows C++ to treat floating point expressions
    differently to the way that a C compiler is required to.


    --
    Francis Glassborow ACCU
    Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
    or http://www.robinton.demon.co.uk
    Happy Xmas, Hanukkah, Yuletide, Winter/Summer Solstice to all.


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.m oderated. First time posters: Do this! ]

    Comment

    • Francis Glassborow

      #32
      Re: how long is double

      In message <pan.2004.01.06 .12.13.21.93557 8@remove.this.p art.rtij.nl>,
      Martijn Lievaart <m@remove.this. part.rtij.nl> writes[color=blue]
      >On Tue, 06 Jan 2004 06:35:26 -0500, kanze wrote:
      >[color=green]
      > > I think that this was Francis' point. According to the standard, a+b+c
      > > is (a+b)+c. The compiler is free to rearrange this any way it pleases,
      > > as long as the results are the same as if it had done (a+b)+c. On most
      > > machines, with integer arithmetic, there is no problem. On no machine
      > > that I know of, however, can the compiler legally rearrange floating
      > > point, unless it absolutely knows the values involved.
      > >
      > > There was quite a lot of discussion about this when the C standard was
      > > first being written. K&R explicitly allowed rearrangement, even when it
      > > would result in different results. In the end, the C standard decided
      > > not to allow this.[/color]
      >
      >Then this seems a place where C and C++ differ, see the answer and quote
      >from the C++ standard from Ron Natalie.
      >
      >Anyone who can confirm this?[/color]

      I do not think so, I think C++ in the non-normative note [1.9 para 15]
      was attempting to make current practice explicit -- i.e. regrouping.
      Clause 5 para 4 largely para-phrases 6.5 paras 1-3 of the current C
      Standard (or 6.3 in the old C Standard)

      I do not think we intended any extra licence in C++ other than that
      granted in C.

      --
      Francis Glassborow ACCU
      Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
      or http://www.robinton.demon.co.uk
      Happy Xmas, Hanukkah, Yuletide, Winter/Summer Solstice to all.


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.m oderated. First time posters: Do this! ]

      Comment

      • John Potter

        #33
        Re: how long is double

        On 7 Jan 2004 03:50:05 -0500, Martijn Lievaart
        <m@remove.this. part.rtij.nl> wrote:
        [color=blue]
        > On Tue, 06 Jan 2004 06:35:26 -0500, kanze wrote:[/color]
        [color=blue][color=green]
        > > I think that this was Francis' point. According to the standard, a+b+c
        > > is (a+b)+c. The compiler is free to rearrange this any way it pleases,
        > > as long as the results are the same as if it had done (a+b)+c. On most
        > > machines, with integer arithmetic, there is no problem. On no machine
        > > that I know of, however, can the compiler legally rearrange floating
        > > point, unless it absolutely knows the values involved.[/color][/color]
        [color=blue][color=green]
        > > There was quite a lot of discussion about this when the C standard was
        > > first being written. K&R explicitly allowed rearrangement, even when it
        > > would result in different results. In the end, the C standard decided
        > > not to allow this.[/color][/color]
        [color=blue]
        > Then this seems a place where C and C++ differ, see the answer and quote
        > from the C++ standard from Ron Natalie.[/color]
        [color=blue]
        > Anyone who can confirm this?[/color]

        His quote is about order of evaluation. It relates to a*b+c*d where
        either of a*b or c*d may be evaluated first. In the code under
        discussion, a+b+c, it is (a+b)+c. C may be evaluated before a+b but
        changing it to a+(b+c) or (a+c)+b is only allowed by the as-if rule.
        The non-normative note in 1.9/15 amplifies this. It seems that the
        C++ rules are the same as the C rules.

        I have no idea how Ron based his other post that claimed an expression
        could be reordered giving different results. There is no support in
        either of the sited verses.

        John

        [ See http://www.gotw.ca/resources/clcm.htm for info about ]
        [ comp.lang.c++.m oderated. First time posters: Do this! ]

        Comment

        Working...