Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #23436 > unrolled thread
| Started by | Xavier Roche <xroche@free.fr.NOSPAM.invalid> |
|---|---|
| First post | 2012-07-20 15:21 +0200 |
| Last post | 2012-07-20 10:08 -0700 |
| Articles | 17 on this page of 37 — 15 participants |
Back to article view | Back to comp.lang.c
What shall return "0.0 ? 1 : 0" ? Xavier Roche <xroche@free.fr.NOSPAM.invalid> - 2012-07-20 15:21 +0200
Re: What shall return "0.0 ? 1 : 0" ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-20 14:57 +0100
Re: What shall return "0.0 ? 1 : 0" ? Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-21 14:39 -0700
Re: What shall return "0.0 ? 1 : 0" ? Ralf Damaschke <rwspam@gmx.de> - 2012-07-21 23:31 +0000
Re: What shall return "0.0 ? 1 : 0" ? Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-21 21:57 -0700
Re: What shall return "0.0 ? 1 : 0" ? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-07-22 17:27 +0300
Re: What shall return "0.0 ? 1 : 0" ? Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 11:15 -0700
Re: What shall return "0.0 ? 1 : 0" ? Martin Shobe <martin.shobe@yahoo.com> - 2012-07-22 18:43 +0000
Re: What shall return "0.0 ? 1 : 0" ? Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 10:14 -0700
Re: What shall return "0.0 ? 1 : 0" ? Ralf Damaschke <rwspam@gmx.de> - 2012-07-22 22:43 +0000
Re: What shall return "0.0 ? 1 : 0" ? Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 10:59 -0700
Re: What shall return "0.0 ? 1 : 0" ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-22 02:43 +0100
Re: What shall return "0.0 ? 1 : 0" ? Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-21 22:05 -0700
Re: What shall return "0.0 ? 1 : 0" ? Seungbeom Kim <musiphil@bawi.org> - 2012-07-22 03:20 -0700
Re: What shall return "0.0 ? 1 : 0" ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-22 14:25 +0100
Re: What shall return "0.0 ? 1 : 0" ? "Bill Leary" <Bill_Leary@msn.com> - 2012-07-22 18:56 -0400
Re: What shall return "0.0 ? 1 : 0" ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-23 01:37 +0100
Re: What shall return "0.0 ? 1 : 0" ? "Bill Leary" <Bill_Leary@msn.com> - 2012-07-23 07:27 -0400
Re: What shall return "0.0 ? 1 : 0" ? Ike Naar <ike@sverige.freeshell.org> - 2012-07-23 06:45 +0000
Re: What shall return "0.0 ? 1 : 0" ? "Bill Leary" <Bill_Leary@msn.com> - 2012-07-23 07:37 -0400
Re: What shall return "0.0 ? 1 : 0" ? Ike Naar <ike@sverige.freeshell.org> - 2012-07-23 06:49 +0000
Re: What shall return "0.0 ? 1 : 0" ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-23 12:42 +0100
Re: What shall return "0.0 ? 1 : 0" ? Noob <root@127.0.0.1> - 2012-07-20 15:58 +0200
Re: What shall return "0.0 ? 1 : 0" ? Xavier Roche <xroche@free.fr.NOSPAM.invalid> - 2012-07-20 16:02 +0200
Re: What shall return "0.0 ? 1 : 0" ? Noob <root@127.0.0.1> - 2012-07-20 16:22 +0200
Re: What shall return "0.0 ? 1 : 0" ? Xavier Roche <xroche@free.fr.NOSPAM.invalid> - 2012-07-20 16:26 +0200
Re: What shall return "0.0 ? 1 : 0" ? Kenneth Brody <kenbrody@spamcop.net> - 2012-07-20 15:11 -0400
Re: What shall return "0.0 ? 1 : 0" ? Robert Miles <milesrf@Usenet-News.net> - 2012-08-25 00:16 -0500
Re: What shall return "0.0 ? 1 : 0" ? Kenneth Brody <kenbrody@spamcop.net> - 2012-07-20 15:10 -0400
Re: What shall return "0.0 ? 1 : 0" ? jacob navia <jacob@spamsink.net> - 2012-07-20 18:15 +0200
Re: What shall return "0.0 ? 1 : 0" ? Keith Thompson <kst-u@mib.org> - 2012-07-20 09:51 -0700
Re: What shall return "0.0 ? 1 : 0" ? pete <pfiland@mindspring.com> - 2012-07-20 20:29 -0400
Re: What shall return "0.0 ? 1 : 0" ? Keith Thompson <kst-u@mib.org> - 2012-07-20 20:25 -0700
Re: What shall return "0.0 ? 1 : 0" ? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-07-21 22:22 +0300
Re: What shall return "0.0 ? 1 : 0" ? Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-21 14:54 -0700
Re: expressions, evaluations, and values (was: What shall return "0.0 ? 1 : 0" ?) Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-21 14:52 -0700
Re: What shall return "0.0 ? 1 : 0" ? Keith Thompson <kst-u@mib.org> - 2012-07-20 10:08 -0700
Page 2 of 2 — ← Prev page 1 [2]
| From | Ike Naar <ike@sverige.freeshell.org> |
|---|---|
| Date | 2012-07-23 06:49 +0000 |
| Message-ID | <slrn3vfsk0pt00.lad.ike@sverige.freeshell.org> |
| In reply to | #23570 |
On 2012-07-22, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote: > That aside, 0.0 == 0 is interesting. On a machine that has an exact > floating zero, 0 must convert to it, but 0.0 need not (surely this is > unintended?). On one that does not have an exact floating zero, very > similar wording is used in 6.3.1.4 p2 (for the conversion) as is used in > 6.4.4.2 p3 (for the representation of the constant): > > "the result is either the nearest representable value, or the larger > or smaller representable value immediately adjacent to the nearest > representable value, chosen in an implementation-defined manner." > > "the result is either the nearest higher or nearest lower > representable value, chosen in an implementation-defined manner" > > but there's nothing to say that the implementation must choose in the > same way in both cases. A perverse implementation could "round" 0.0 up > and convert 0 by going the other way! That also raises the question: in static double d; what would be the initial value of d? 0 or 0.0 ?
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2012-07-23 12:42 +0100 |
| Message-ID | <0.31808ce4fe8b9f7fc21d.20120723124235BST.87obn64sb8.fsf@bsb.me.uk> |
| In reply to | #23600 |
Ike Naar <ike@sverige.freeshell.org> writes: > On 2012-07-22, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote: >> That aside, 0.0 == 0 is interesting. On a machine that has an exact >> floating zero, 0 must convert to it, but 0.0 need not (surely this is >> unintended?). On one that does not have an exact floating zero, very >> similar wording is used in 6.3.1.4 p2 (for the conversion) as is used in >> 6.4.4.2 p3 (for the representation of the constant): >> >> "the result is either the nearest representable value, or the larger >> or smaller representable value immediately adjacent to the nearest >> representable value, chosen in an implementation-defined manner." >> >> "the result is either the nearest higher or nearest lower >> representable value, chosen in an implementation-defined manner" >> >> but there's nothing to say that the implementation must choose in the >> same way in both cases. A perverse implementation could "round" 0.0 up >> and convert 0 by going the other way! > > That also raises the question: in > > static double d; > > what would be the initial value of d? > 0 or 0.0 ? I think it must be floating zero. My remarks about machine with no exact zero are somewhat hypothetical. I am not yet convinced that such a machine can support a conforming C implementation, but people whose opinions I respect currently disagree. As someone else has already point out (sorry, I don't recall who right now), the rules for default initialisation of objects with static storage duration state that arithmetic types are initialised to "(positive or unsigned) zero;". I don't think that can mean anything but exact zero (whatever that really means). -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Noob <root@127.0.0.1> |
|---|---|
| Date | 2012-07-20 15:58 +0200 |
| Message-ID | <jubo6i$s65$1@dont-email.me> |
| In reply to | #23436 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Xavier Roche <xroche@free.fr.NOSPAM.invalid> |
|---|---|
| Date | 2012-07-20 16:02 +0200 |
| Message-ID | <juboe7$k38$2@news.httrack.net> |
| In reply to | #23439 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Noob <root@127.0.0.1> |
|---|---|
| Date | 2012-07-20 16:22 +0200 |
| Message-ID | <jubpja$429$1@dont-email.me> |
| In reply to | #23440 |
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
[toc] | [prev] | [next] | [standalone]
| From | Xavier Roche <xroche@free.fr.NOSPAM.invalid> |
|---|---|
| Date | 2012-07-20 16:26 +0200 |
| Message-ID | <jubpqr$k38$3@news.httrack.net> |
| In reply to | #23441 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Kenneth Brody <kenbrody@spamcop.net> |
|---|---|
| Date | 2012-07-20 15:11 -0400 |
| Message-ID | <1pednfqqEfDDMJTNnZ2dnUVZ_tqdnZ2d@bestweb.net> |
| In reply to | #23443 |
On 7/20/2012 10:26 AM, Xavier Roche wrote: > 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) Haven't you heard? Usenet is dead. :-) -- Kenneth Brody
[toc] | [prev] | [next] | [standalone]
| From | Robert Miles <milesrf@Usenet-News.net> |
|---|---|
| Date | 2012-08-25 00:16 -0500 |
| Message-ID | <50385fc7$0$8711$882e7ee2@usenet-news.net> |
| In reply to | #23443 |
On 7/20/2012 9:26 AM, Xavier Roche wrote: > 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) Partly because their connection to Google Groups allowed a large inflow of spam and posts saying that other operating systems are better. Partly because they are moving away from supporting newsgroups at all.
[toc] | [prev] | [next] | [standalone]
| From | Kenneth Brody <kenbrody@spamcop.net> |
|---|---|
| Date | 2012-07-20 15:10 -0400 |
| Message-ID | <1pednfuqEfC-MJTNnZ2dnUVZ_tqdnZ2d@bestweb.net> |
| In reply to | #23440 |
On 7/20/2012 10:02 AM, 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).
>
> (*) tested version: cl.exe 16.00.30319.01 for x64 (same issue using the x86
> release)
Confirmed as well with cl.exe 16.00.40219.01 for 80x86.
=====
#include <stdio.h>
int main(void)
{
double zero = 0.0;
printf("%d\n", ( 0.0 ? 1 : 0 ) );
printf("%d\n", ( zero ? 1 : 0 ) );
return(0);
}
=====
Displays:
1
0
=====
Interestingly enough, if I turn optimization all the way on, it eliminates
the conditionals altogether, as well as the variable "zero", and compiles to
the equivalent of:
printf("%d\n",1);
printf("%d\n",0);
Ditto for a definition of "float zero".
Apparently, it thinks that a float/double with 0.0 in it is equivalent to
zero, but a constant "0.0" is not. ???
--
Kenneth Brody
[toc] | [prev] | [next] | [standalone]
| From | jacob navia <jacob@spamsink.net> |
|---|---|
| Date | 2012-07-20 18:15 +0200 |
| Message-ID | <juc075$sku$1@speranza.aioe.org> |
| In reply to | #23436 |
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
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-07-20 09:51 -0700 |
| Message-ID | <lnobna9y00.fsf@nuthaus.mib.org> |
| In reply to | #23436 |
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"
[toc] | [prev] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2012-07-20 20:29 -0400 |
| Message-ID | <5009F7F6.C47@mindspring.com> |
| In reply to | #23448 |
Keith Thompson wrote:
>
> 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.)
Section "6.5 Expressions" in both C89 and in C99,
described the bitwise operators as returning values.
--
pete
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-07-20 20:25 -0700 |
| Message-ID | <ln7gtxaj7r.fsf@nuthaus.mib.org> |
| In reply to | #23469 |
pete <pfiland@mindspring.com> writes:
> Keith Thompson wrote:
>> 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.)
>
> Section "6.5 Expressions" in both C89 and in C99,
> described the bitwise operators as returning values.
C99 says they "return" values; N1256 says they "yield" values,
as does N1570 (the C11 pre-release draft).
None of the three Technical Corrigenda (which in theory constitute
the difference between C99 and N1256) contain the word "yield".
I think the editor, Larry Jones, just made the change himself.
--
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"
[toc] | [prev] | [next] | [standalone]
| From | Phil Carmody <thefatphil_demunged@yahoo.co.uk> |
|---|---|
| Date | 2012-07-21 22:22 +0300 |
| Message-ID | <87ipdgx6l6.fsf@bazspaz.fatphil.org> |
| In reply to | #23473 |
Keith Thompson <kst-u@mib.org> writes:
> pete <pfiland@mindspring.com> writes:
> > Keith Thompson wrote:
> >> 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.)
> >
> > Section "6.5 Expressions" in both C89 and in C99,
> > described the bitwise operators as returning values.
>
> C99 says they "return" values; N1256 says they "yield" values,
> as does N1570 (the C11 pre-release draft).
>
> None of the three Technical Corrigenda (which in theory constitute
> the difference between C99 and N1256) contain the word "yield".
> I think the editor, Larry Jones, just made the change himself.
Not just that, but he disclosed that here after a discussion
almost identical to this one. IIRC.
Whilst I think it's good to wean people off inappropriate use of
"return", I don't think it's worth making too big a thing of, at
least if the question has been understood by everyone who didn't
deliberately want to be adversarial.
Phil
--
> I'd argue that there is much evidence for the existence of a God.
Pics or it didn't happen.
-- Tom (/. uid 822)
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Date | 2012-07-21 14:54 -0700 |
| Message-ID | <kfnzk6s3hme.fsf@x-alumni2.alumni.caltech.edu> |
| In reply to | #23473 |
Keith Thompson <kst-u@mib.org> writes:
> pete <pfiland@mindspring.com> writes:
>> Keith Thompson wrote:
>>> 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.)
>>
>> Section "6.5 Expressions" in both C89 and in C99,
>> described the bitwise operators as returning values.
>
> C99 says they "return" values; N1256 says they "yield" values,
> as does N1570 (the C11 pre-release draft). [snip]
Yes, and N1124 is like N1256 in this regard.
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Date | 2012-07-21 14:52 -0700 |
| Subject | Re: expressions, evaluations, and values (was: What shall return "0.0 ? 1 : 0" ?) |
| Message-ID | <kfn4np04wa0.fsf@x-alumni2.alumni.caltech.edu> |
| In reply to | #23448 |
ram@zedat.fu-berlin.de (Stefan Ram) writes: > 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/. [snip] There are also lots of places where the Standard uses phrasing like 'an expression yields the value ...' or 'x[3][5] yields ...'. Generally speaking the Standard often doesn't do a good job of observing the distinction between translation-time properties and execution-time properties. Strictly speaking, an expression is a translation-time entity; it is only an evaulation of an expression that has (or yields) a value, or evaluating an expression that yields a value. Some expressions have the property that every time they are evaluated the evaluations always yield the same value, and there it might be more defensible to say that the expression /has/ that value. But I don't think the Standard itself is careful about these distinctions one way or the other.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-07-20 10:08 -0700 |
| Message-ID | <lnk3xy9x8m.fsf@nuthaus.mib.org> |
| In reply to | #23436 |
Xavier Roche <xroche@free.fr.NOSPAM.invalid> writes:
> 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 ?
As other responses have said, the result is well defined, and this
appears to be a bug in Visual C++ 2010.
I just tried this program:
#include <stdio.h>
int main(void) {
printf("%d\n", 0.0 ? 1 : 0);
if (0.0) {
puts("0.0 is true");
}
else {
puts("0.0 is false");
}
if (0.0 == 0) {
puts("0.0 compares equal to 0");
}
else {
puts("0.0 does not compare equal to 0");
}
return 0;
}
With gcc, the output is:
0
0.0 is false
0.0 compares equal to 0
With Visual Studio, the output is:
1
0.0 is false
0.0 compares equal to 0
with the language set to C and extensions disabled.
--
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"
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.c
csiph-web