Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > parenthesis

Reply
Thread Tools

parenthesis

 
 
gc
Guest
Posts: n/a
 
      10-16-2003
If I have double variables, d,u,v,w,x. Soes the standard ensure that
while
assigning a value to d as,
d=(u+v)-(w+x);
u+v will be added first then w+x and then they will be added together
and that the compiler will not evaluate the above expression in
anyother order.

Just wondering, since addition of doubles is not associative.
 
Reply With Quote
 
 
 
 
Grumble
Guest
Posts: n/a
 
      10-16-2003
gc wrote:
> If I have double variables, d,u,v,w,x. Soes the standard ensure that
> while
> assigning a value to d as,
> d=(u+v)-(w+x);
> u+v will be added first then w+x and then they will be added together
> and that the compiler will not evaluate the above expression in
> anyother order.
>
> Just wondering, since addition of doubles is not associative.


The subtraction will be carried out last, but AFAIK the additions
could be done in either order. If you want to force a specific
evaluation order, you could use an extra variable.

 
Reply With Quote
 
 
 
 
Russell Hanneken
Guest
Posts: n/a
 
      10-16-2003
gc wrote:
> If I have double variables, d,u,v,w,x. Soes the standard ensure that
> while assigning a value to d as,
> d=(u+v)-(w+x);
> u+v will be added first then w+x


No, neither the C standard nor the C++ standard guarantee that. (w+x) might
be evaluated before (u+v).

--
Russell Hanneken
http://www.velocityreviews.com/forums/(E-Mail Removed)
Remove the 'g' from my address to send me mail.


 
Reply With Quote
 
Andreas Kahari
Guest
Posts: n/a
 
      10-16-2003
In article <bmljkn$86h$(E-Mail Removed)>, Grumble wrote:
> gc wrote:
>> If I have double variables, d,u,v,w,x. Soes the standard ensure that
>> while
>> assigning a value to d as,
>> d=(u+v)-(w+x);
>> u+v will be added first then w+x and then they will be added together
>> and that the compiler will not evaluate the above expression in
>> anyother order.
>>
>> Just wondering, since addition of doubles is not associative.

>
> The subtraction will be carried out last, but AFAIK the additions
> could be done in either order. If you want to force a specific
> evaluation order, you could use an extra variable.
>


double t1, t2;

t1 = u + v;
t2 = w + x;

d = t1 - t2;


AFAIK, there's nothing stopping the compiler from generating
code that calculates t2 before t1.

I'm not sure the finer details of floating point arithmetics is
an issue here at all.

--
Andreas Kähäri
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      10-16-2003
(E-Mail Removed) (gc) wrote:

> If I have double variables, d,u,v,w,x. Soes the standard ensure that
> while
> assigning a value to d as,
> d=(u+v)-(w+x);
> u+v will be added first then w+x and then they will be added together
> and that the compiler will not evaluate the above expression in
> anyother order.


No.

> Just wondering, since addition of doubles is not associative.


True, but it shouldn't matter at all in which order the two
sub-expressions are evaluated. This:

- add u and v; remember the result.
- add w and x; remember the result.
- subtract the second result from the first.

should give the same answer as this:

- add w and x; remember the result.
- add u and v; remember the result.
- subtract the first result from the second.


The problem only starts when you do things like

a=b+c+d;

because then, for floating point objects, (b+c)+d can give very
different results from b+(c+d). And indeed, the Standard prohibits
implementations from optimising that kind of expression too
enthusiastically. For example,

a=x+y+z;
b=w+y+z;

cannot be optimised into

t=y+z
a=x+t;
b=w+t;

because that might give different results. Contrariwise,

a=y+z+x;
b=y+z+w;

_can_ be optimised to

t=y+z
a=t+x;
b=t+w;


But your example does not have this problem.

Richard
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-16-2003
Grumble <(E-Mail Removed)> wrote:

>gc wrote:
>> If I have double variables, d,u,v,w,x. Soes the standard ensure that
>> while
>> assigning a value to d as,
>> d=(u+v)-(w+x);
>> u+v will be added first then w+x and then they will be added together
>> and that the compiler will not evaluate the above expression in
>> anyother order.
>>
>> Just wondering, since addition of doubles is not associative.

>
>The subtraction will be carried out last, but AFAIK the additions
>could be done in either order. If you want to force a specific
>evaluation order, you could use an extra variable.


Why use an extra variable?

d = u + v;
d -= w + x;

Regards
--
Irrwahn
((E-Mail Removed))
 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      10-16-2003

On Thu, 16 Oct 2003, Andreas Kahari wrote:
>
> In article <bmljkn$86h$(E-Mail Removed)>, Grumble wrote:
> > gc wrote:
> >>
> >> If I have double variables, d,u,v,w,x. Soes the standard ensure that
> >> while assigning a value to d as,
> >> d=(u+v)-(w+x);
> >> u+v will be added first then w+x and then they will be added together
> >> and that the compiler will not evaluate the above expression in
> >> anyother order.
> >>
> >> Just wondering, since addition of doubles is not associative.


The order of operations is not specified, except that (w+x) will be
subtracted from (u+v); the compiler can't decide to subtract w from
(u+v), and then subtract x from the result, *UNLESS* the answer would
be exactly the same (the "as if" rule) -- in which case who cares how
it was computed?

> > The subtraction will be carried out last, but AFAIK the additions
> > could be done in either order. If you want to force a specific
> > evaluation order, you could use an extra variable.

>
> double t1, t2;
>
> t1 = u + v;
> t2 = w + x;
>
> d = t1 - t2;
>
> AFAIK, there's nothing stopping the compiler from generating
> code that calculates t2 before t1.


Practically, that's true. However, the code *must* behave *AS IF*
t1 was calculated before t2, because the semicolons introduce
sequence points.

> I'm not sure the finer details of floating point arithmetics is
> an issue here at all.


On some systems (which may or may not be conforming, I don't know;
I don't follow floating-point stuff , we can have

double d = 3.14;
double e = 4.72;
double f = d/e;
printf("%g %g\n", f, d/e);

and get two different numbers, because the FPU registers use
slightly wider representations than the actual type 'double'
objects. That's why order-of-operations matters in general,
in practice.
And specifically about associativity, you know that

double d = 1e10, e = 5, f = 5;
double r1, r2;
r1 = (d+e)+f;
r2 = d+(e+f);

can produce (r1 != r2) (for suitable values of 5, of course).
It's plausible that something like that could be at stake
in the OP's code; I don't feel like running through all possible
values of (u,v,w,x) right now.

HTH,
-Arthur

 
Reply With Quote
 
Andreas Kahari
Guest
Posts: n/a
 
      10-16-2003
In article
<(E-Mail Removed)>,
Arthur J. O'Dwyer wrote:
>
> On Thu, 16 Oct 2003, Andreas Kahari wrote:
>>
>> In article <bmljkn$86h$(E-Mail Removed)>, Grumble wrote:

[cut]
>> > The subtraction will be carried out last, but AFAIK the additions
>> > could be done in either order. If you want to force a specific
>> > evaluation order, you could use an extra variable.

>> t1 = u + v;
>> t2 = w + x;
>>
>> d = t1 - t2;
>>
>> AFAIK, there's nothing stopping the compiler from generating
>> code that calculates t2 before t1.

>
> Practically, that's true. However, the code *must* behave *AS IF*
> t1 was calculated before t2, because the semicolons introduce
> sequence points.


In this example, there will not be any difference to the result
if u+v is evaluated before/after w+x. The OP's concern is
without foundation.

However, if the question was if there was a difference between
"u+v-w+x" and "(u+v)-(w+x)", then the answer is clearly yes,
depending on the magnitude and sign of the involved floating
point numbers.


--
Andreas Kähäri
 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      10-16-2003

On Thu, 16 Oct 2003, Andreas Kahari wrote:
>
> Arthur J. O'Dwyer wrote:
> > On Thu, 16 Oct 2003, Andreas Kahari wrote:
> >>
> >> AFAIK, there's nothing stopping the compiler from generating
> >> code that calculates t2 before t1.

> >
> > Practically, that's true. However, the code *must* behave *AS IF*
> > t1 was calculated before t2, because the semicolons introduce
> > sequence points.

>
> In this example, there will not be any difference to the result
> if u+v is evaluated before/after w+x. The OP's concern is
> without foundation.


But some compilers like to aggressively optimize arithmetic, turning
(u+v)-(w+x) into (u+v-w-x) or similar, and in *that* case there's
definitely a difference!

> However, if the question was if there was a difference between
> "u+v-w+x" and "(u+v)-(w+x)", then the answer is clearly yes,

^^^^^^^ ^^^^^^^^^^^
> depending on the magnitude and sign of the involved floating
> point numbers.


In particular, if the magnitude of 'x' is non-zero.

-Arthur
 
Reply With Quote
 
gc
Guest
Posts: n/a
 
      10-17-2003
Andreas Kahari <(E-Mail Removed)> wrote in message > In this example, there will not be any difference to the result
> if u+v is evaluated before/after w+x. The OP's concern is
> without foundation.
>


Sorry for not making myself clear,
I wanted to know whether the compiler is free to evaluate
(u+v)-(w+x);

as u+(v-w)-x; (i.e, add u to the result of v-w and then subtract x)
or in any other order it finds suitable.



Similarly, another question that arises is that whether is assured
that the compiler will not interpret the the test (1+x>1) as (x>0), if
x is double these two tests need not be the same
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expression to insert spaces around parenthesis puzzlecracker Java 0 01-25-2006 07:31 PM
")" expected error when there's supppose to be only 1 set of parenthesis .Net Sports ASP .Net 4 06-29-2005 06:33 PM
bracket or parenthesis number in Documents Larry Smith Computer Support 3 02-15-2005 01:19 AM
Free IDE with Parenthesis Matching? Rogue Noir C++ 6 08-02-2003 06:42 PM
Re: Automatic parenthesis for complex calculation Bob Day C Programming 1 07-11-2003 01:58 PM



Advertisments