Topic: int b = 5, c = 3; a = b / c; // implimentation defined ?
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/03/09 Raw View
markw65@my-dejanews.com wrote:
[...]
> Yes, but it must meet certain conditions:
> i) (a/b)*b + (a%b) == a for all a, and all b!=0
> ii) abs(a%b) < abs(b) for all a, and all b!=0
> iii) a%b >= 0 for all a >= 0 & b > 0
> iv) any others Ive forgotten
Rules (i) to (iii) fix the result completely if A>=0, b>0.
Since b=0 results in undefined behaviour anyway, any additional
rule can only add something to the case where at least one of
the results is negative (in which case, (i) to (iii) generally
leave two options).
Are the following assertions guaranteed?
bool same_sign(int a, int b)
{
return (a>0 && b>0) || (a<0 && b<0) || (a==0 && b==0);
}
void assertions(int a1, int b1, int a2, int b2)
{
assert (!same_sign(a1, a2) || !same_sign(b1, b2)
|| same_sign(a1%b1, a2%b2));
// the sign of the result is a function of the signs of
// the arguments only
assert ((-a1)%(-b1) == a1%b1);
// reverting both signs doesn't change the result
}
If both assertions hold, an implementation can make only one
desicion, which could be represented by the result of -1%2.
Either it's +1, or -1.
If only the first assertion holds, the implementation can set
independently the values of -1%2, 1%-2 and -1%-2 (each one
either to 1 or to -1).
If the first assertion doesn't hold, the implementation can
define e.g. -1%2 == -1, but -1%3 == 1. This would obviously
be bad (so I suspect the first assertion holds).
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/03/09 Raw View
P.M. wrote:
....
> >> The Standard states that "(a/b)*b + a%b is equal to a" if b is non-zero.
> So
> >> that means that a/b must be rounded down, otherwise (a/b)*b will be
> greater
> >> than a.
> >
> >No, it just means that an implementation must match the value of a%b to the
> >rounding direction of a/b.
> >
> >eg if we choose -5/3 == -1, then -5%3 must be -2. If we choose -5/3 == -2,
> >then -5%3 must be 1.
>
> Forgive my ignorance, I Just want to make sure I have this right.
>
> Can we choose 8/2 == 5, then 8%2 must be -2 ?
>
> Is division implementation defined also ?
Yes, but not in that case; it's implementation defined only if one of
the operands is negative. The standard says:
"If both operands are nonnegative then the remainder is nonnegative; if
not, the sign of the remainder is implementation defined."
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: jim.hyslop@leitch.com
Date: 1999/03/09 Raw View
In article <36E073DD.6AF1B17A@wizard.net>,
James Kuyper <kuyper@wizard.net> wrote:
[snip]
> However, footnote 74 mentions the work being done on the C9X draft of
> the C standard, which will specify that the quotient is always rounded
> toward 0, for compatibility with ISO Fortran. I think we can reasonably
> expect that the next version of the C++ will also mandate rounding
> toward 0.
Yes, I saw that footnote - that was the basis on which I posted my examples in
the other message.
Actually, wouldn't this already be required by 18.2.1.2, numeric_limits
members, para 60: "Specializations for integer types shall return
round_toward_zero."
Jim
Note to recruitment agencies: I will not refer my friends or colleagues
to you nor do I want to use your services to find me a job. I stop
reading unsolicited email as soon as I determine it is job-recruitment
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/03/09 Raw View
jim.hyslop@leitch.com wrote:
....
> Actually, wouldn't this already be required by 18.2.1.2, numeric_limits
> members, para 60: "Specializations for integer types shall return
> round_toward_zero."
That seems inconsistent with section 5.6, p3, which says that the sign
of the remainder is implementation-defined, which implies that
round_toward_zero is not guaranteed. Is this a defect in the standard?
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "P.M." <pmucci@intermicro.com>
Date: 1999/03/08 Raw View
markw65@my-dejanews.com wrote in message
<7brn81$4he$1@nnrp1.dejanews.com>...
>In article <7bpkaf$f30$1@nnrp1.dejanews.com>,
> jim.hyslop@leitch.com wrote:
>>
>> In article <7bongr$83s$1@remarQ.com>,
>> "P.M." <pmucci@intermicro.com> wrote:
>> > Is the rounding that is done in integer division implementation defined
?
[snipped to save space]
>> The Standard states that "(a/b)*b + a%b is equal to a" if b is non-zero.
So
>> that means that a/b must be rounded down, otherwise (a/b)*b will be
greater
>> than a.
>
>No, it just means that an implementation must match the value of a%b to the
>rounding direction of a/b.
>
>eg if we choose -5/3 == -1, then -5%3 must be -2. If we choose -5/3 == -2,
>then -5%3 must be 1.
Forgive my ignorance, I Just want to make sure I have this right.
Can we choose 8/2 == 5, then 8%2 must be -2 ?
Is division implementation defined also ?
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: markw65@my-dejanews.com
Date: 1999/03/09 Raw View
In article <7c0ke0$7p0$1@remarQ.com>,
"P.M." <pmucci@intermicro.com> wrote:
>
> markw65@my-dejanews.com wrote in message
> >eg if we choose -5/3 == -1, then -5%3 must be -2. If we choose -5/3 == -2,
> >then -5%3 must be 1.
>
> Forgive my ignorance, I Just want to make sure I have this right.
>
> Can we choose 8/2 == 5, then 8%2 must be -2 ?
>
> Is division implementation defined also ?
Yes, but it must meet certain conditions:
i) (a/b)*b + (a%b) == a for all a, and all b!=0
ii) abs(a%b) < abs(b) for all a, and all b!=0
iii) a%b >= 0 for all a >= 0 & b > 0
iv) any others Ive forgotten
Your definition fails on ii) and iii)
Mark Williams
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: jim.hyslop@leitch.com
Date: 1999/03/05 Raw View
In article <7bongr$83s$1@remarQ.com>,
"P.M." <pmucci@intermicro.com> wrote:
> Is the rounding that is done in integer division implementation defined ?
>
> In 5.6, It says that when either of the integers are negative the
> resulting sign is implementation defined, I couldn't determine
> if all rounding results for non-negative integers where also
> implementation defined, the footnote seems to indicate it.
You're mixing two distinct operations here.
The Standard states that the sign *of the remainder* (i.e. of the modulus
operation) is implementation-defined if either operand is negative. Your
subject line said nothing about modulus.
The Standard states that "(a/b)*b + a%b is equal to a" if b is non-zero. So
that means that a/b must be rounded down, otherwise (a/b)*b will be greater
than a.
Note that this requirement, by the way, contradicts the first statement above
(about the sign of the remainder if either operand is negative) - in order
for "(a/b)*b + a%b" to equal a when a is negative (b can be either positive
or negative), the sign on a%b *must* be negative, and when a is positive and
b negative the sign on a%b *must* be positive. Work through all four
combinations of a=+-5, b=+-3 and you'll see what I mean.
Jim
Note to recruitment agencies: I will not refer my friends or colleagues
to you nor do I want to use your services to find me a job. I stop
reading unsolicited email as soon as I determine it is job-recruitment
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/03/06 Raw View
jim.hyslop@leitch.com wrote:
....
> The Standard states that the sign *of the remainder* (i.e. of the modulus
> operation) is implementation-defined if either operand is negative.
However, footnote 74 mentions the work being done on the C9X draft of
the C standard, which will specify that the quotient is always rounded
toward 0, for compatibility with ISO Fortran. I think we can reasonably
expect that the next version of the C++ will also mandate rounding
toward 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: markw65@my-dejanews.com
Date: 1999/03/06 Raw View
In article <7bpkaf$f30$1@nnrp1.dejanews.com>,
jim.hyslop@leitch.com wrote:
>
> In article <7bongr$83s$1@remarQ.com>,
> "P.M." <pmucci@intermicro.com> wrote:
> > Is the rounding that is done in integer division implementation defined ?
> >
> > In 5.6, It says that when either of the integers are negative the
> > resulting sign is implementation defined, I couldn't determine
> > if all rounding results for non-negative integers where also
> > implementation defined, the footnote seems to indicate it.
> You're mixing two distinct operations here.
>
> The Standard states that the sign *of the remainder* (i.e. of the modulus
> operation) is implementation-defined if either operand is negative. Your
> subject line said nothing about modulus.
>
> The Standard states that "(a/b)*b + a%b is equal to a" if b is non-zero. So
> that means that a/b must be rounded down, otherwise (a/b)*b will be greater
> than a.
No, it just means that an implementation must match the value of a%b to the
rounding direction of a/b.
eg if we choose -5/3 == -1, then -5%3 must be -2. If we choose -5/3 == -2,
then -5%3 must be 1.
>
> Note that this requirement, by the way, contradicts the first statement above
> (about the sign of the remainder if either operand is negative) - in order
> for "(a/b)*b + a%b" to equal a when a is negative (b can be either positive
> or negative), the sign on a%b *must* be negative, and when a is positive and
> b negative the sign on a%b *must* be positive. Work through all four
> combinations of a=+-5, b=+-3 and you'll see what I mean.
See counterexamples above...
Mark Williams
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "P.M." <pmucci@intermicro.com>
Date: 1999/03/05 Raw View
Is the rounding that is done in integer division implementation defined ?
In 5.6, It says that when either of the integers are negative the
resulting sign is implementation defined, I couldn't determine
if all rounding results for non-negative integers where also
implementation defined, the footnote seems to indicate it.
Someone I know says that rounding is implementation defined,
can someone explain/clarify this.
Thank 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://reality.sgi.com/austern_mti/std-c++/faq.html ]