Topic: Integer division specification


Author: "Satish" <satishsanthanam@gmail.com>
Date: Wed, 21 Feb 2007 20:45:51 CST
Raw View
On Feb 19, 9:49 am, "James Kanze" <james.ka...@gmail.com> wrote:
> Maarten Kronenburg wrote:
> > "Gennaro Prota"  wrote
> > > >I was wondering if, as part of the synchronization with C99, the C++
> > > >standard is going to require truncation towards zero for integer
> > > >division or continue to allow the same choice as C90.
> > > Well, let me please do another attempt at getting a reply. I have a
> > > defect report concerning the specification of operator / and the
> > > proposed resolution would depend on whether truncation "towards -inf"
> > > is allowed or not.
> > > It would help everyone, not least the committee which would have to
> > > examine the DR, to know the answer to this (if it is already known
> > > :-)).
> > Of course also I don't know the answer of this, because somewhere in the
> > world there may be hardware with a compiler where truncation is not towards
> > zero.
>
> I doubt it.  Fortran requires truncation toward zero, and when
> the C committee considered the issue, they were unable to find
> such hardware.
>
> > All I know is that at least on x86 the truncation is toward zero. When
> > the truncation is toward -inf, then technically the remainder becomes the
> > modulo.
> > In document N2134, 5.6 [expr.mul], it says "the sign of remainder is
> > implementation defined" with footnote 78: "According to work underway ...
> > the quotient is always rounded toward zero."
> > The problem of course is how to know if there are compilers with hardware
> > not truncating toward zero. So the solution may then be to have a list of
> > e-mail addresses of compiler vendors who can answer that question for you,
> > then you could check all the compiler vendors and ask them personally. When
> > they wouldn't respond then at least they had the opportunity to object.
>
> You can probably suppose that any vendor supporting both C and
> C++ will handle division the same way in both languages, and C
> requires truncation to zero, so the only vendors you'd really
> have ask are those who only support C++, and not C.  And I don't
> think that there are too many of those.
>
> At the hardware level, of course, hardware tends to be designed
> to make programs run faster.  Since most (all?) major languages
> except C++ require truncation to zero, it seems a safe bet that
> all hardware truncates to zero.
>
> I suspect that the original decision to truncate to zero in
> Fortran was based in fact on what the hardware at the time did.
> And that the hardware at that time did whatever was simplest and
> fastest.  Which makes me think that there probably isn't any
> reason for hardware ever to do anything else.
>
> --
> James Kanze (GABI Software)             email:james.ka...@gmail.com
> 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-...@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ:http://www.comeaucomputing.com/csc/faq.html                     ]

If using Intel, check if this document will be helpful.

ftp://download.intel.com/design/pentium/manuals/241430.htm



---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: "James Kanze" <james.kanze@gmail.com>
Date: Mon, 19 Feb 2007 08:49:15 CST
Raw View
Maarten Kronenburg wrote:
> "Gennaro Prota"  wrote
> > >I was wondering if, as part of the synchronization with C99, the C++
> > >standard is going to require truncation towards zero for integer
> > >division or continue to allow the same choice as C90.

> > Well, let me please do another attempt at getting a reply. I have a
> > defect report concerning the specification of operator / and the
> > proposed resolution would depend on whether truncation "towards -inf"
> > is allowed or not.

> > It would help everyone, not least the committee which would have to
> > examine the DR, to know the answer to this (if it is already known
> > :-)).

> Of course also I don't know the answer of this, because somewhere in the
> world there may be hardware with a compiler where truncation is not towards
> zero.

I doubt it.  Fortran requires truncation toward zero, and when
the C committee considered the issue, they were unable to find
such hardware.

> All I know is that at least on x86 the truncation is toward zero. When
> the truncation is toward -inf, then technically the remainder becomes the
> modulo.
> In document N2134, 5.6 [expr.mul], it says "the sign of remainder is
> implementation defined" with footnote 78: "According to work underway ...
> the quotient is always rounded toward zero."
> The problem of course is how to know if there are compilers with hardware
> not truncating toward zero. So the solution may then be to have a list of
> e-mail addresses of compiler vendors who can answer that question for you,
> then you could check all the compiler vendors and ask them personally. When
> they wouldn't respond then at least they had the opportunity to object.

You can probably suppose that any vendor supporting both C and
C++ will handle division the same way in both languages, and C
requires truncation to zero, so the only vendors you'd really
have ask are those who only support C++, and not C.  And I don't
think that there are too many of those.

At the hardware level, of course, hardware tends to be designed
to make programs run faster.  Since most (all?) major languages
except C++ require truncation to zero, it seems a safe bet that
all hardware truncates to zero.

I suspect that the original decision to truncate to zero in
Fortran was based in fact on what the hardware at the time did.
And that the hardware at that time did whatever was simplest and
fastest.  Which makes me think that there probably isn't any
reason for hardware ever to do anything else.

--
James Kanze (GABI Software)             email:james.kanze@gmail.com
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.comeaucomputing.com/csc/faq.html                      ]





Author: gennaro@this.is.invalid (Gennaro Prota)
Date: Tue, 20 Feb 2007 17:42:39 GMT
Raw View
On Sun, 18 Feb 2007 21:35:35 CST, Maarten Kronenburg wrote:

>> Well, let me please do another attempt at getting a reply. I have a
>> defect report concerning the specification of operator / and the
>> proposed resolution would depend on whether truncation "towards -inf"
>> is allowed or not.
>>
>> It would help everyone, not least the committee which would have to
>> examine the DR, to know the answer to this (if it is already known
>> :-)).
>>
>Genny,
>Of course also I don't know the answer of this, because somewhere in the
>world there may be hardware with a compiler where truncation is not towards
>zero. All I know is that at least on x86 the truncation is toward zero.

Thanks Maarten, I was wondering if they will synchronize with C99,
which now imposes truncation towards zero. Since there was no reply
from committee members I think I'll submit the DR with resolutions for
both cases (though, at this point, synch with C99 looks a bit
unlikely).

Genny.

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: "=?iso-8859-1?q?Daniel_Kr=FCgler?=" <daniel.kruegler@googlemail.com>
Date: Tue, 20 Feb 2007 12:05:32 CST
Raw View
On 17 Feb., 20:37, Gennaro Prota <gennaro.pr...@yahoo.com> wrote:
> On Wed, 14 Feb 2007 09:25:30 CST, Gennaro Prota wrote:
> >Hi,
>
> >I was wondering if, as part of the synchronization with C99, the C++
> >standard is going to require truncation towards zero for integer
> >division or continue to allow the same choice as C90.
>
> Well, let me please do another attempt at getting a reply. I have a
> defect report concerning the specification of operator / and the
> proposed resolution would depend on whether truncation "towards -inf"
> is allowed or not.
>
> It would help everyone, not least the committee which would have to
> examine the DR, to know the answer to this (if it is already known
> :-)).

I'm sorry, that I can be only of limited help here, but even some
little help is better than nothing, I guess ;-)

I just read the current wording of C99 and the most recent draft for
C++. From what I read, I would deduce the following:

1) C99 (More exactly: I have studied 9899:1999 (E) for this), says
in 6.5.5/6:

"When integers are divided, the result of the / operator is the
algebraic
quotient with any fractional part discarded.87)"

with footnote 87:

"This is often called ''truncation toward zero''."

so indeed C99 demands truncation toward zero.

2) Opposed to this, N2134 says in 5.6/4:

"If both operands are nonnegative then the remainder is nonnegative;
if not, the sign of the remainder is implementation-defined78)"

with footnote 78:

"According to work underway toward the revision of ISO C, the
preferred
algorithm for integer division follows the rules defined in the ISO
Fortran
standard, ISO/IEC 1539:1991, in which the quotient is always rounded
toward zero."

So viewing from the Status Quo I interpret that our C++ Standard does
currently would allow any rounding strategy (including truncation
"towards -inf") if one of the operands is negative.

I cannot say, whether the current state of affairs are by design or
not,
but the synchronization seems to be useful for most programmers.

Greetings from Bremen,

Daniel



---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: M.Kronenburg@inter.nl.net ("Maarten Kronenburg")
Date: Tue, 20 Feb 2007 20:50:00 GMT
Raw View
"Gennaro Prota"  wrote
> Thanks Maarten, I was wondering if they will synchronize with C99,
> which now imposes truncation towards zero. Since there was no reply
> from committee members I think I'll submit the DR with resolutions for
> both cases (though, at this point, synch with C99 looks a bit
> unlikely).
>
Genny,
When I read the other replies personally I would propose in the DR to adopt
the C99 standard (truncation toward zero) and see what opposition exists.
Most programmers want the standard to be as explicit and unambiguous as
possible, and personally I think C++ cannot afford to be ambiguous about
this, and that C++ should adopt what Fortran and C already have, that is
truncation toward zero. A compiler with hardware that has truncation other
than toward zero should then mention that in their documentation.
After all, when -4 / 3 == -2 (instead of -1), while 4 / 3 == 1, then
obviously something strange is going on.
Regards, Maarten.


---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: jm@bourguet.org (Jean-Marc Bourguet)
Date: Tue, 20 Feb 2007 21:55:23 GMT
Raw View
M.Kronenburg@inter.nl.net ("Maarten Kronenburg") writes:

> After all, when -4 / 3 == -2 (instead of -1), while 4 / 3 == 1, then
> obviously something strange is going on.

You can wonder why Knuth defined MMIX so that its division operates this
way.  See also http://mathworld.wolfram.com/IntegerDivision.html which
define integer division in exactly that way.

Practically, I remember few use of integer division or remainder with
negative operands.  All divisions needed the version C99 mandates.  Things
change when you consider the remainder: the two versions seems to me about
as usefull.

For what it worth, Ada defines / as Fortran, but has two remainder
operators: mod (which is periodic) and rem (which is odd).

Yours,

--
Jean-Marc

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: "=?iso-8859-1?q?Daniel_Kr=FCgler?=" <daniel.kruegler@googlemail.com>
Date: Tue, 20 Feb 2007 16:21:02 CST
Raw View
On 20 Feb., 18:42, genn...@this.is.invalid (Gennaro Prota) wrote:
> [..] Since there was no reply
> from committee members I think I'll submit the DR with resolutions for
> both cases (though, at this point, synch with C99 looks a bit
> unlikely).

Genny, why do you consider a sync with C99 as unlikely?
AFAIK C99 behaviour seems already a valid implementation now,
or have I overseen something?
If this sync would be required, current compiler writers will
probably add a flag (if needed) to use the "old" style.

Greetings,

Daniel

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: gennaro.prota@yahoo.com (Gennaro Prota)
Date: Tue, 20 Feb 2007 22:52:15 GMT
Raw View
On Tue, 20 Feb 2007 16:21:02 CST, Daniel Kr=FCgler wrote:

>Genny, why do you consider a sync with C99 as unlikely?

Just a guess, based on the fact that there's one meeting left before
C++09 (two months from now) and the publicly available draft has no
change concerning this.

Genny.

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: Pete Becker <pete@versatilecoding.com>
Date: Tue, 20 Feb 2007 16:56:23 CST
Raw View
Maarten Kronenburg wrote:
> A compiler with hardware that has truncation other
> than toward zero should then mention that in their documentation.

In C++, the direction of rounding is implemenation defined. That means
the implementation is required to document it in all cases.

> After all, when -4 / 3 == -2 (instead of -1), while 4 / 3 == 1, then
> obviously something strange is going on.

Not necessarily. Consider the remainders, as well. When you don't have
rounding toward negative infinity, you often end up having to test
remainders to see whether they're negative. Calendar calculations, in
particular, end up with lots of code that looks like this:

int dow = date % 7;
if (dow < 0)
 dow += 7;

--

 -- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: "Maarten Kronenburg" <M.Kronenburg@inter.nl.net>
Date: Tue, 20 Feb 2007 17:17:01 CST
Raw View
"Jean-Marc Bourguet"  wrote
> > After all, when -4 / 3 == -2 (instead of -1), while 4 / 3 == 1, then
> > obviously something strange is going on.
>
> You can wonder why Knuth defined MMIX so that its division operates this
> way.  See also http://mathworld.wolfram.com/IntegerDivision.html which
> define integer division in exactly that way.
>
> Practically, I remember few use of integer division or remainder with
> negative operands.  All divisions needed the version C99 mandates.  Things
> change when you consider the remainder: the two versions seems to me about
> as usefull.
>
> For what it worth, Ada defines / as Fortran, but has two remainder
> operators: mod (which is periodic) and rem (which is odd).
>
When / truncates toward zero, then rem (calling it %) is truly the
remainder:
a = ( a / b ) * b + a % b
When / truncates toward -inf, then mod is the remainder.
Now when in Fortran and C99 / truncates toward zero, as most hardware does,
then it seems to me that in C++ / should also truncate toward zero, simply
following Fortran and C99. Then it follows that rem is the remainder,
calling it % as above.
Luckily rem is more like the word remainder than mod is, because mod is an
abbreviation of modulo. Most mathematical texts however mix remainder and
modulo together, which causes all the confusion.
Regards, Maarten.


---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: Gennaro Prota <gennaro.prota@yahoo.com>
Date: Wed, 14 Feb 2007 09:25:30 CST
Raw View
Hi,

I was wondering if, as part of the synchronization with C99, the C++
standard is going to require truncation towards zero for integer
division or continue to allow the same choice as C90.

Thanks,
Genny.

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: Gennaro Prota <gennaro.prota@yahoo.com>
Date: Sat, 17 Feb 2007 13:37:25 CST
Raw View
On Wed, 14 Feb 2007 09:25:30 CST, Gennaro Prota wrote:

>Hi,
>
>I was wondering if, as part of the synchronization with C99, the C++
>standard is going to require truncation towards zero for integer
>division or continue to allow the same choice as C90.

Well, let me please do another attempt at getting a reply. I have a
defect report concerning the specification of operator / and the
proposed resolution would depend on whether truncation "towards -inf"
is allowed or not.

It would help everyone, not least the committee which would have to
examine the DR, to know the answer to this (if it is already known
:-)).

Thanks,
Genny.

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: "Maarten Kronenburg" <M.Kronenburg@inter.nl.net>
Date: Sun, 18 Feb 2007 21:35:35 CST
Raw View
"Gennaro Prota"  wrote
> >I was wondering if, as part of the synchronization with C99, the C++
> >standard is going to require truncation towards zero for integer
> >division or continue to allow the same choice as C90.
>
> Well, let me please do another attempt at getting a reply. I have a
> defect report concerning the specification of operator / and the
> proposed resolution would depend on whether truncation "towards -inf"
> is allowed or not.
>
> It would help everyone, not least the committee which would have to
> examine the DR, to know the answer to this (if it is already known
> :-)).
>
Genny,
Of course also I don't know the answer of this, because somewhere in the
world there may be hardware with a compiler where truncation is not towards
zero. All I know is that at least on x86 the truncation is toward zero. When
the truncation is toward -inf, then technically the remainder becomes the
modulo.
In document N2134, 5.6 [expr.mul], it says "the sign of remainder is
implementation defined" with footnote 78: "According to work underway ...
the quotient is always rounded toward zero."
The problem of course is how to know if there are compilers with hardware
not truncating toward zero. So the solution may then be to have a list of
e-mail addresses of compiler vendors who can answer that question for you,
then you could check all the compiler vendors and ask them personally. When
they wouldn't respond then at least they had the opportunity to object.
Regards, Maarten.


---
[ 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.comeaucomputing.com/csc/faq.html                      ]