Re: C values that are not known at compile time
- From: Richard Dobson <richarddobson@xxxxxxxxxxxxxxxx>
- Date: Thu, 05 Jan 2006 16:09:15 GMT
Chris Bore wrote:
It is compiler-dependent if an expression like float x,a,b,c; x = a+b-c; is implemented as x1 = a + (b-c); or x2 = (a+b)-c;
I think the order of evaluation of floats is well-defined. Some compilers choose to ignore the rules for reasons of speed, which means their results differ even with the same code and same compiler depending on optimization level (first rule of optimization - code should always give same result..). So I think one of your compilers was flaky - I think the Texas compiler for their 'C40 processors let itself change float evaluation order, to increase speed, but that is not correct. The compiler might allow you to break the float ordering rule, but that should be an explicit option.
In fact the required order of evaluation of floats (wihin a statement) is one thing that hampers parallel compilers - because they are not allowed to change the order and so cannot exploit potential paralellism. Float arithmtic is not associative, so a+(b-c) is not equal to (a+b) -c you can see this if you make a and b very large and very small respetcively.
Chris
It all depends on whether the compiler aims to be ANSI compliant. that standard defines both the precedence ~and~ the associativity of operators; e.g unary operators are right-associative, binary ones are left-associative, which means that in an expression "a + b - c - d" where the operators are at the same precedecne level, operations will be calculatew from left to right:
((a+b)-c)-d
And the indirection unary operators & and * have a higher precedence than the binary arithmetic operators * and /, so
&a[2] always means &(a[2])
If the compilers are not ANSI-compliant, all bets are off, and all one can do is RTFM, if there is one, and if not, hope+pray.
In ANSI C float types are not treated in any special way, so expressions involving floats obey the same precedence and associativity rules as any other numeric type. It is up to the programmer to worry about rounding errors, etc, and thus to arrange calculation order effectively. Again, if the compiler elects to disregard ANSI rules, anything is possible, except generalisations and portability.
Richard Dobson
Richard Dobson .
- Follow-Ups:
- Re: C values that are not known at compile time
- From: Chris Bore
- Re: C values that are not known at compile time
- References:
- Re: C values that are not known at compile time
- From: Chris Bore
- Re: C values that are not known at compile time
- From: Jon Harris
- Re: C values that are not known at compile time
- From: Chris Bore
- Re: C values that are not known at compile time
- From: Rune Allnor
- Re: C values that are not known at compile time
- From: Chris Bore
- Re: C values that are not known at compile time
- Prev by Date: Re: C values that are not known at compile time
- Next by Date: OT Zero Padding in radix 2 FFT
- Previous by thread: Re: C values that are not known at compile time
- Next by thread: Re: C values that are not known at compile time
- Index(es):
Relevant Pages
|