Topic: 4-3/2 == -3/2+4? compatibility with C99


Author: Charm.Quarker@gmail.com
Date: Sun, 6 Nov 2005 18:20:41 CST
Raw View
In C99, integer division truncates to zero, but in C++ it is
implementation-defined, thus the expression in the title may be false.
Unnatural, isn't it?

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ark@acm.org ("Andrew Koenig")
Date: Mon, 7 Nov 2005 06:43:02 GMT
Raw View
<Charm.Quarker@gmail.com> wrote in message
news:1131282820.669109.70040@o13g2000cwo.googlegroups.com...

> In C99, integer division truncates to zero, but in C++ it is
> implementation-defined, thus the expression in the title may be false.
> Unnatural, isn't it?

Why?  C++ arithmetic is compatible with C89, because C89 was the only C
standard that existed when the C++ standard was approved.

In practice, it's not much of an issue, because every C89 and every C++
implementation I've seen truncates toward zero anyway.  Moreover, it is very
rare for programs to depend on which way integer division truncates anyway.
It matters only when exactly one of the dividend and divisor is negative,
and I can't recall the last time I've seen a program that actually depends
on that case.


---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: yozara@terra.es (Zara)
Date: Mon, 7 Nov 2005 06:44:29 GMT
Raw View
On Sun,  6 Nov 2005 18:20:41 CST, Charm.Quarker@gmail.com wrote:

>In C99, integer division truncates to zero, but in C++ it is
>implementation-defined, thus the expression in the title may be false.
>Unnatural, isn't it?
>
>---
>[ 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.jamesd.demon.co.uk/csc/faq.html                       ]

In C89, the division could truncate upward or downward, implementation
defined. C++, in its effort to maintain compatibility with C, adopted
such behaviou. Later a footnote has been made aboiut the convenience
of adopting the rule of rounding towards zero, so I expect that this
behaviour will be adopted on C++0x.

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: M.Kronenburg@inter.nl.net ("Maarten Kronenburg")
Date: Tue, 8 Nov 2005 05:20:06 GMT
Raw View
In the POSIX standard, div_t div( int, int) provides
guaranteed division towards zero,
see POSIX Programmer's guide:
"Use div(a,b).quot instead of a/b if the quotient must be rounded
the same way on all systems. Use div(a,b).rem to obtain the remainder
of dividing a by b."
But I agree it should also be in the C/C++ standard, if only to
avoid the problem you mentioned.

<Charm.Quarker@gmail.com> wrote in message
news:1131282820.669109.70040@o13g2000cwo.googlegroups.com...
> In C99, integer division truncates to zero, but in C++ it is
> implementation-defined, thus the expression in the title may be false.
> Unnatural, isn't it?
>
> ---
> [ 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.jamesd.demon.co.uk/csc/faq.html                       ]
>


---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Tue, 8 Nov 2005 05:21:46 GMT
Raw View
Charm.Quarker@gmail.com wrote:
> In C99, integer division truncates to zero, but in C++ it is
> implementation-defined, thus the expression in the title may be false.
> Unnatural, isn't it?

Footnote 74 hints that ISO C had not specified the algorithm of integer
division at the time the C++ standard was written, but it shows clear
intent to prefer the ISO Fortran algorithm (truncation towards zero). As
C99 put this preference into standard, I believe C++ will follow that
line in the next revision. Remember that most (if not all) C++ compilers
on the market are also C compilers, so it would be silly for them to
implement division in two different ways ;-)

In case you are wondering, I guess the change wasn't introduced right in
TC1 because according to ISO rules a "technical corrigendum" can only
address "defects" and this issue does not qualify as such.

Ganesh

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Charm.Quarker@gmail.com
Date: Tue, 8 Nov 2005 22:32:33 CST
Raw View
I'm afraid that it is not so rare, e.g., in Euclid's algorithm for gcd
as a common example presented in many C/C++ textbooks, it does depend
on whether -a/b is equivalent to -(a/b) or not.

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "kanze" <kanze@gabi-soft.fr>
Date: Wed, 9 Nov 2005 09:46:48 CST
Raw View
"Maarten Kronenburg" wrote:
> In the POSIX standard, div_t div( int, int) provides
> guaranteed division towards zero,

In the C and the C++ standards as well.

> see POSIX Programmer's guide:
> "Use div(a,b).quot instead of a/b if the quotient must be
> rounded the same way on all systems. Use div(a,b).rem to
> obtain the remainder of dividing a by b."

> But I agree it should also be in the C/C++ standard, if only
> to avoid the problem you mentioned.

The function is there.  The problem is that 1) people don't use
it, and 2) it's often slower than strait division -- more than
one implementation doesn't inline it.

--
James Kanze                                           GABI Software
Conseils en informatique orient   e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S   mard, 78210 St.-Cyr-l'   cole, France, +33 (0)1 30 23 00 34


---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Andrew Koenig" <ark@acm.org>
Date: Wed, 9 Nov 2005 12:55:27 CST
Raw View
<Charm.Quarker@gmail.com> wrote in message
news:1131455192.993105.241910@o13g2000cwo.googlegroups.com...

> I'm afraid that it is not so rare, e.g., in Euclid's algorithm for gcd
> as a common example presented in many C/C++ textbooks, it does depend
> on whether -a/b is equivalent to -(a/b) or not.

If both operands are negative, then the result will be the same whichever
way division rounds.

If one operand is negative and the other is positive, it's not immediately
clear to me what the right result should be.  Is it clear to you?

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Charm.Quarker@gmail.com
Date: Thu, 10 Nov 2005 01:15:55 CST
Raw View
I found that the example of MCD is improper; it may be really rare for
a program to depend on this behavior. Nevertheless, only to be
compatible with C99 is a good enough reason to specify it in C++
standard, and having -3/2 == 0-3/2 == -(3/2) is of least surprise.

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]