Re: Please comment on my integer to string code.
manoj1978@gmail .com wrote:[color=blue][color=green]
>> For some definition of "it works" but clearly not mine: the result
>> of using the modulo operator with negative operands is implementation
>> defined in C++, i.e. it may work depending on the choice of the
>> implementation. Essentially, the sign of the result is not defined.[/color]
>
> I have a doubt here. which among these causes undefined behavior?
> Because i was relying on these :-((
>
> negetive % negetive.
> negetive % positive.
> positive % negetive[/color]
Well, they are not "undefined" but "implementa tion defined". There
is a subtle difference: for "undefined" the implementation does not
have to state what it does and actually may not even detect the
situation at all. For "implementa tion defined" the implementation
is supposed to tell somewhere how it handles the situation.
The relevant quote from the standard is this (5.6, expr.mul,
paragraph 4):
If the second operand of / or % is zero the behavior is undefined;
otherwise (a/b)*b + a%b is equal to a. If both operands are
nonnegative then the remainder is nonnegative; if not, the sign of
the remainder is implementation-defined.
--
<mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
manoj1978@gmail .com wrote:[color=blue][color=green]
>> For some definition of "it works" but clearly not mine: the result
>> of using the modulo operator with negative operands is implementation
>> defined in C++, i.e. it may work depending on the choice of the
>> implementation. Essentially, the sign of the result is not defined.[/color]
>
> I have a doubt here. which among these causes undefined behavior?
> Because i was relying on these :-((
>
> negetive % negetive.
> negetive % positive.
> positive % negetive[/color]
Well, they are not "undefined" but "implementa tion defined". There
is a subtle difference: for "undefined" the implementation does not
have to state what it does and actually may not even detect the
situation at all. For "implementa tion defined" the implementation
is supposed to tell somewhere how it handles the situation.
The relevant quote from the standard is this (5.6, expr.mul,
paragraph 4):
If the second operand of / or % is zero the behavior is undefined;
otherwise (a/b)*b + a%b is equal to a. If both operands are
nonnegative then the remainder is nonnegative; if not, the sign of
the remainder is implementation-defined.
--
<mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Comment