Can A Macro Do This?

B

Barry Schwarz

You appear to have ignored my definition of transitivity.

If knowing that a == b && b == c means that you know for sure that a ==
c, then == is transitive. It has nothing to do with being able to use
the a == b == c syntax. As I said, I believe that == is transitive, but
I don't know the standard well enough to confirm this.

Well, if that were true this would produce three statements of
equality. It's a shame it won't compile. But, if you are not allowed
to compare them, how can you say they are equal?

#include <stdio.h>
int main(void){
int x[2];
int *a;
void *b;
int (*c)[2];
a = x;
b = x;
c = &x;
if (a == b) puts("a == b");
if (b == c) puts("b == c");
if (a == c)
puts("a == c");
else
puts("a != c");
getchar();
return 0;}


Remove del for email
 
P

Philip Potter

Barry said:
If knowing that a == b && b == c means that you know for sure that a ==
c, then == is transitive. It has nothing to do with being able to use
the a == b == c syntax. As I said, I believe that == is transitive, but
I don't know the standard well enough to confirm this.

Well, if that were true this would produce three statements of
equality. It's a shame it won't compile. But, if you are not allowed
to compare them, how can you say they are equal?

#include <stdio.h>
int main(void){
int x[2];
int *a;
void *b;
int (*c)[2];
a = x;
b = x;
c = &x;
if (a == b) puts("a == b");
if (b == c) puts("b == c");
if (a == c)
puts("a == c");
else
puts("a != c");
getchar();
return 0;}

It compiles on mine:

pgp@medusa-s2:~/tmp$ gcc -ansi -pedantic bs.c -obs
bs.c: In function `main':
bs.c:12: warning: comparison of distinct pointer types lacks a cast
pgp@medusa-s2:~/tmp$ ./bs
a == b
b == c
a == c
pgp@medusa-s2:~/tmp$

....although I accept that it's not guaranteed to compile everywhere.

which said:
> Hmm, for a = (int *) 0, b = 0, c = (float *) 0:
>
> a == b && b == c, but a == c is a constraint violation. I think. So ==
> is not transitive in the mathematical sense.


If we change the problem and limit ourselves to strictly-conforming
programs, is it possible to come up with expressions a, b, and c for
which a == b and b == c but a != c?
 
J

jameskuyper

Philip Potter wrote:
....
If we change the problem and limit ourselves to strictly-conforming
programs, is it possible to come up with expressions a, b, and c for
which a == b and b == c but a != c?

This isn't strictly conforming; if LDBL_MAX < UINT_MAX, which is
technically possible for a conforming implementation, it will
overflow. In order for this to work, I need a signed or floating point
type capable of representing UINT_MAX, and the standard does not
require that any such type exist. However, I expect most people will
be willing to accept that limitation:

#include <stdio.h>
#include <limits.h>
int main(void)
{
int a = -1;
unsigned b = UINT_MAX;
double c = b;

printf("a%c=b\n", a==b ? '=' : '!');
printf("b%c=c\n", b==c ? '=' : '!');
printf("a%c=c\n", a==c ? '=' : '!');
return 0;
}

Output:
a==b
b==c
a!=c
 
P

Philip Potter

Philip Potter wrote: [snip]
#include <stdio.h>
#include <limits.h>
int main(void)
{
int a = -1;
unsigned b = UINT_MAX;
double c = b;

printf("a%c=b\n", a==b ? '=' : '!');
printf("b%c=c\n", b==c ? '=' : '!');
printf("a%c=c\n", a==c ? '=' : '!');
return 0;
}

Output:
a==b
b==c
a!=c

Nice. Thanks a lot.
 
B

Barry Schwarz

This is the only point I was trying to make.
If we change the problem and limit ourselves to strictly-conforming
programs, is it possible to come up with expressions a, b, and c for
which a == b and b == c but a != c?

I expect there are floating point values, let's call one such value X,
for which
float a = X;
double b = X;
long double = X;
produces the situation in question due to the manner in which floating
point values are approximated. Unfortunately, I can't test it on my
system since long double and double have the same representation.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top