Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   What shall return "0.0 ? 1 : 0" ? (http://www.velocityreviews.com/forums/t948310-what-shall-return-0-0-1-0-a.html)

Xavier Roche 07-20-2012 01:21 PM

What shall return "0.0 ? 1 : 0" ?
 
Hi folks!

Is there a C standard expert who can confirm what shall return
0.0 ? 1 : 0

With gcc 4.3/Linux, the result is 0. With Visual C++2010, the result is.. 1.

Is the result undefined by the standard, by chance ?


Regards,
Xavier Roche

Ben Bacarisse 07-20-2012 01:57 PM

Re: What shall return "0.0 ? 1 : 0" ?
 
Xavier Roche <xroche@free.fr.NOSPAM.invalid> writes:

> Hi folks!
>
> Is there a C standard expert who can confirm what shall return
> 0.0 ? 1 : 0
>
> With gcc 4.3/Linux, the result is 0. With Visual C++2010, the result
> is.. 1.


How odd.

> Is the result undefined by the standard, by chance ?


No, it's defined to be 0.

It does not violate any constraint that I can see, and the meaning
depends solely on whether 0.0 "compares equal to 0". I can't find any
justification for 0.0 not comparing equal to 0.

The phases "x compares equal to 0" and "x compares unequal to 0" are
used a lot in the standard. I've always taken them to mean x == 0 and x
!= 0, but I don't think that's ever stated explicitly.

--
Ben.

Noob 07-20-2012 01:58 PM

Re: What shall return "0.0 ? 1 : 0" ?
 
Xavier Roche wrote:

> Is there a C standard expert who can confirm what shall return
> 0.0 ? 1 : 0
>
> With gcc 4.3/Linux, the result is 0. With Visual C++2010, the result is.. 1.
>
> Is the result undefined by the standard, by chance ?


Lemme see...

3.3.15 Conditional operator

Syntax

conditional-expression:
logical-OR-expression
logical-OR-expression ? expression : conditional-expression

Constraints

The first operand shall have scalar type.
[...]

Semantics

The first operand is evaluated; there is a sequence point after
its evaluation. The second operand is evaluated only if the first
compares unequal to 0; the third operand is evaluated only if the
first compares equal to 0; the value of the second or third operand
(whichever is evaluated) is the result[41].

[41] A conditional expression does not yield an lvalue.

0.0 is a 'floating-constant' which is a kind of 'primary-expression'
which is a kind of 'logical-OR-expression'.

(Scalar type means integral or floating or pointer.)

Since 0.0 (the floating constant) compares equal to 0 (the integral constant)
(0.0 ? 1 : 0) has value 0, as far as I can tell.

Regards.

Xavier Roche 07-20-2012 02:02 PM

Re: What shall return "0.0 ? 1 : 0" ?
 
On 07/20/2012 03:58 PM, Noob wrote:
> Since 0.0 (the floating constant) compares equal to 0 (the integral constant)
> (0.0 ? 1 : 0) has value 0, as far as I can tell.


Thanks a lot to both of you. It appears to be a bug in Visual C++ 2010
(*) then.

This impression is confirmed when trying:
static double zero = 0.0;
...
zero ? 0 : 1
which returns the correct value (0).

(*) tested version: cl.exe 16.00.30319.01 for x64 (same issue using the
x86 release)


Noob 07-20-2012 02:22 PM

Re: What shall return "0.0 ? 1 : 0" ?
 
Xavier Roche wrote:
> On 07/20/2012 03:58 PM, Noob wrote:
>> Since 0.0 (the floating constant) compares equal to 0 (the integral constant)
>> (0.0 ? 1 : 0) has value 0, as far as I can tell.

>
> Thanks a lot to both of you. It appears to be a bug in Visual C++ 2010
> (*) then.
>
> This impression is confirmed when trying:
> static double zero = 0.0;
> ..
> zero ? 0 : 1
> which returns the correct value (0).


NB: You swapped 0 and 1 there.

( 0.0 ? 42 : 666) evaluates to 666
(zero ? 42 : 666) evaluates to 666

Xavier Roche 07-20-2012 02:26 PM

Re: What shall return "0.0 ? 1 : 0" ?
 
On 07/20/2012 04:22 PM, Noob wrote:
> NB: You swapped 0 and 1 there.


Yes, my mistake (but the tested code is the initial one and has the
wrong value)

I have reported the issue on the "Visual C++ Language" forums (I can't
understand why they ditched the microsoft.* groups for this piece of
webforum junk by the way)


Stefan Ram 07-20-2012 03:21 PM

Re: What shall return "0.0 ? 1 : 0" ?
 
Xavier Roche <xroche@free.fr.NOSPAM.invalid> writes:
>Is there a C standard expert who can confirm what shall return
>0.0 ? 1 : 0


An expression does not return. A function may return. After

int f(){ return 0.0 ? 1 : 0; {

, we can say that f »shall return 0.0 ? 1 : 0«.


jacob navia 07-20-2012 04:15 PM

Re: What shall return "0.0 ? 1 : 0" ?
 
Le 20/07/12 15:21, Xavier Roche a écrit :
> Hi folks!
>
> Is there a C standard expert who can confirm what shall return
> 0.0 ? 1 : 0
>
> With gcc 4.3/Linux, the result is 0. With Visual C++2010, the result
> is.. 1.
>
> Is the result undefined by the standard, by chance ?
>
>
> Regards,
> Xavier Roche


The bug is at the level of the compile-time interpretation since the
result is not calculated at run time (even without aptimizations!)

cl -FA bug.c yields this code:

; Listing generated by Microsoft (R) Optimizing Compiler Version
16.00.30319.01

include listing.inc

INCLUDELIB LIBCMT
INCLUDELIB OLDNAMES

_DATA SEGMENT
$SG2584 DB '???', 00H
ORG $+4
$SG2585 DB '0.0 ? ??? : OK = %s', 0aH, 00H
_DATA ENDS
PUBLIC main
EXTRN printf:PROC
pdata SEGMENT
$pdata$main DD imagerel $LN3
DD imagerel $LN3+30
DD imagerel $unwind$main
pdata ENDS
xdata SEGMENT
$unwind$main DD 010401H
DD 04204H
; Function compile flags: /Odtp
xdata ENDS
_TEXT SEGMENT
main PROC
; File d:\lcc\bug.c
; Line 3
$LN3:
sub rsp, 40 ; 00000028H
; Line 4
lea rdx, OFFSET FLAT:$SG2584
lea rcx, OFFSET FLAT:$SG2585
call printf
; Line 5
xor eax, eax
add rsp, 40 ; 00000028H
ret 0
main ENDP
_TEXT ENDS
END


Keith Thompson 07-20-2012 04:51 PM

Re: What shall return "0.0 ? 1 : 0" ?
 
ram@zedat.fu-berlin.de (Stefan Ram) writes:
> Xavier Roche <xroche@free.fr.NOSPAM.invalid> writes:
>>Is there a C standard expert who can confirm what shall return
>>0.0 ? 1 : 0

>
> An expression does not return. A function may return. After
>
> int f(){ return 0.0 ? 1 : 0; {
>
> , we can say that f »shall return 0.0 ? 1 : 0«.


An expression *yields* a value. (I think earlier versions of the
standard used the word "return" for the results of expressions in
some places; as far as I know it's been cleaned up.)

I agree that it's not strictly correct to say that an expression
returns a value, but the intended meaning is clear enough.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Stefan Ram 07-20-2012 05:07 PM

expressions, evaluations, and values (was: What shall return "0.0 ? 1 : 0" ?)
 
Keith Thompson <kst-u@mib.org> writes:
>An expression *yields* a value. (I think earlier versions of the
>standard used the word "return" for the results of expressions in
>some places; as far as I know it's been cleaned up.)


I'd say that an expression /had/ a value (and that an /evaluation/
yielded a value). Let me try to find quotations in N1570:

»Its lifetime begins when the expression is evaluated
and its initial value is the value of the expression.«
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
»An actual implementation need not evaluate part of an
expression if it can deduce that its value is not used
and that no needed side effects are produced«

»the value of CHAR_MIN«

»expressions with implementation-defined values«

»Its lifetime begins when the expression is evaluated
and its initial value is the value of the expression.«

»If an expression of any other type is evaluated as a
void expression, its value or designator is discarded.«

The relationship between an expression and its value in all
these quotes (selected without bias) is always /possessive/,
hence /has/.

However, I'd like to add that for run-time values, it would
be more correct never to say that /an expression had/ a value,
since the value is in a one-to-one correspondence only with
a single /evaluation/, because each evaluation of an
expression may yield a different value. But it might be too
cumbersome to always write »the value of an evaluation of x«
instead of »the value of x«.



All times are GMT. The time now is 11:50 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.