Topic: Is ( -10 > -10 + unsigned int (20) ) true?


Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Sat, 17 Nov 2001 00:26:19 GMT
Raw View
Marco Dalla Gasperina wrote:
>
> "Barry Margolin" <barmar@genuity.net> wrote in message
> news:WgXI7.24$I25.2565@burlma1-snr2...
> > The properties of modular arithmetic are such that it doesn't make a
> > difference whether you convert -10 before or after the addition.  I.e.
> >
> > (x + y) mod n == (x mod n) + (y mod n)
>
> ???
> (2+3) mod 4 = 1
> (2 mod 4) + (3 mod 4) == 5
>
> do you mean
> (x + y) mod n == ((x mod n) + (y mod n) mod n)
> ?

Almost; as you've re-written it, the final "mod n" is redundant. It's
actually:

(x + y) mod n == (x mod n + y mod n) mod n

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Patricio" <nospam@nospam.invalid>
Date: Thu, 15 Nov 2001 17:21:31 GMT
Raw View
Hello all.  I have the following problem/question and I wanted to know if my
compiler is right (Microsoft Visual C++ 6.0).

I have an expression with integers and unsigned integers and the compiler is
converting the integers to unsigned integers.

If I have an unsigned int b = 20, the expression (-10 > -10 + b) is
evaluated to true.  If b is smaller than 10 then the expression is false.

If I use constants the expression (-10 > -10 + unsigned int(20)) is also
evaluated to true but I get compiler warnings (negative integral constant
converted to unsigned type, and '+' : integral constant overflow) that
indicates that the -10 is being converted to unsigned int.

If I assigned the result to an integer (int a = -10 + unsigned int(20)) I
get the same compiler warnings but the result is right.

So the question is: Is my compiler right?  If I have mixed types in an
algebraic equation, how the elements are converted to a common type (right
to left, left to right)?

Thank you for your time.

Patricio.


Here is the test program:
#include<iostream>
int main()
{
  unsigned int b = 20;
  // This generates no warning and we get unespected results!
  if (-10 > -10 + b) {
    std::cout << "What? -10 > -10 + b" << std::endl;
  }
  // But this generates the right warning...
  // UnsignedSignedWarning.cpp(16) : warning C4308: negative integral
constant converted to unsigned type
  // UnsignedSignedWarning.cpp(16) : warning C4307: '+' : integral constant
overflow
  if (-10 > -10 + unsigned int(20)) {
    std::cout << "What? -10 > -10 + unsigned int(20)" << std::endl;
  }
  // This generates the same warning but the espected result....
  // UnsignedSignedWarning.cpp(24) : warning C4308: negative integral
constant converted to unsigned type
  // UnsignedSignedWarning.cpp(24) : warning C4307: '+' : integral constant
overflow
  int a = -10 + unsigned int(20);
  if (a != 10)  {
    std::cout << "What? a != 10" << std::endl;
  }
  return 0;
}



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Ron Natalie <ron@sensor.com>
Date: Thu, 15 Nov 2001 17:47:04 GMT
Raw View

Patricio wrote:
>=20
> Hello all.  I have the following problem/question and I wanted to know =
if my
> compiler is right (Microsoft Visual C++ 6.0).
>=20
> I have an expression with integers and unsigned integers and the compil=
er is
> converting the integers to unsigned integers.
>=20
> If I have an unsigned int b =3D 20, the expression (-10 > -10 + b) is
> evaluated to true.  If b is smaller than 10 then the expression is fals=
e.
>=20

When addition is preformed with an unsigned int and a signed int, the res=
ult
is always unsigned:

Many binary operators that expect operands of arithmetic or enumeration t=
ype cause conversions and yield
result types in a similar way. The purpose is to yield a common type, whi=
ch is also the type of the result.
This pattern is called the usual arithmetic conversions, which are define=
d as follows:
=97 If either operand is of type long double, the other shall be converte=
d to long double.
=97 Otherwise, if either operand is double, the other shall be converted =
to double.
=97 Otherwise, if either operand is float, the other shall be converted t=
o float.
=97 Otherwise, the integral promotions (4.5) shall be performed on both o=
perands.54)
=97 Then, if either operand is unsigned long the other shall be converted=
 to unsigned long.
=97 Otherwise, if one operand is a long int and the other unsigned int, t=
hen if a long int can rep-resent
all the values of an unsigned int, the unsigned int shall be converted to=
 a long int;
otherwise both operands shall be converted to unsigned long int.
=97 Otherwise, if either operand is long, the other shall be converted to=
 long.
=97 Otherwise, if either operand is unsigned, the other shall be converte=
d to unsigned.
[Note: otherwise, the only remaining case is that both operands are int ]


=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D MODERATOR'S COMMENT:=20
 Line wrap, line wrap....

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Andrew Koenig <ark@research.att.com>
Date: Thu, 15 Nov 2001 21:26:29 GMT
Raw View
Patricio> Hello all.  I have the following problem/question and I
Patricio> wanted to know if my compiler is right (Microsoft Visual C++
Patricio> 6.0).

Patricio> I have an expression with integers and unsigned integers and
Patricio> the compiler is converting the integers to unsigned
Patricio> integers.

Patricio> If I have an unsigned int b = 20, the expression (-10 > -10
Patricio> + b) is evaluated to true.  If b is smaller than 10 then the
Patricio> expression is false.

The expression

        -10 > -10 + unsigned int(20)

has the same meaning as

        -10 > (-10 + unsigned int(20))

so we must first figure out the meaning of the sub expression

        (-10 + unsigned int(20))

When combining signed and unsigned quantities, the result is unsigned,
so the subexpression has the same type and value as

        unsigned int(10)

and the whole expression is therefore equivalent to

        -10 > unsigned int(10)

Comparing a signed and unsigned quantity converts the signed value to
unsigned, so the result is equivalent to

        unsigned int(-10) > unsigned int(10)

The value of unsigned int(-10) is equivalent to -10 mod 2^n, where n
is the number of bits in an unsigned int.  In all implementations, n
is required to be at least 16, so unsigned int(-10) >= 2^16-10 in all
implementations.  Therefore, unsigned int(-10) will never be less than
65526, which is greater than 10, so this expression should yield true.


--
Andrew Koenig, ark@research.att.com, http://www.research.att.com/info/ark

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Ron Natalie <ron@sensor.com>
Date: Thu, 15 Nov 2001 21:55:13 GMT
Raw View
>         (-10 + unsigned int(20))
>
> When combining signed and unsigned quantities, the result is unsigned,
> so the subexpression has the same type and value as
>
>         unsigned int(10)
>
Lets not skip any steps here lest he gets confused:

 (-10 + unsigned int(20)

is converted to

 (unsigned int(-10) + unsigned int (20))


first.  Negative values converted to unsigned are subtracted from 2**num_bits_in_unsigned
to yield the corresponding unsigned value and unsigned addition wraps module that number.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Barry Margolin <barmar@genuity.net>
Date: Thu, 15 Nov 2001 16:29:12 CST
Raw View
In article <3BF439AE.5728FA67@sensor.com>, Ron Natalie  <ron@sensor.com> wrote:
>
>>         (-10 + unsigned int(20))
>>
>> When combining signed and unsigned quantities, the result is unsigned,
>> so the subexpression has the same type and value as
>>
>>         unsigned int(10)
>>
>Lets not skip any steps here lest he gets confused:
>
> (-10 + unsigned int(20)
>
>is converted to
>
> (unsigned int(-10) + unsigned int (20))
>
>
>first.  Negative values converted to unsigned are subtracted from
>2**num_bits_in_unsigned
>to yield the corresponding unsigned value and unsigned addition wraps
>module that number.

The properties of modular arithmetic are such that it doesn't make a
difference whether you convert -10 before or after the addition.  I.e.

(x + y) mod n == (x mod n) + (y mod n)

This is one of the few mathematical identities that actually maps directly
into a requirement of the C and C++ languages.

--
Barry Margolin, barmar@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Marco Dalla Gasperina" <marcodg@home.com>
Date: Fri, 16 Nov 2001 13:33:28 GMT
Raw View
"Barry Margolin" <barmar@genuity.net> wrote in message
news:WgXI7.24$I25.2565@burlma1-snr2...
> The properties of modular arithmetic are such that it doesn't make a
> difference whether you convert -10 before or after the addition.  I.e.
>
> (x + y) mod n == (x mod n) + (y mod n)

???
(2+3) mod 4 = 1
(2 mod 4) + (3 mod 4) == 5

do you mean
(x + y) mod n == ((x mod n) + (y mod n) mod n)
?

> This is one of the few mathematical identities that actually maps directly
> into a requirement of the C and C++ languages.

marco



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]