Jump to page: 1 2
Thread overview
possible bug in std.conv.parse
Apr 26, 2014
ketmar
Apr 26, 2014
Adam D. Ruppe
Apr 26, 2014
Timon Gehr
Apr 26, 2014
Adam D. Ruppe
Apr 27, 2014
Adam D. Ruppe
Apr 27, 2014
monarch_dodra
Apr 26, 2014
bearophile
Apr 27, 2014
ketmar
Apr 27, 2014
ketmar
Apr 27, 2014
ketmar
April 26, 2014
this code: std.conv.parse!byte("-128") throws error: "Overflow in integral conversion". but this is obviously not true, as signed byte can hold such value.

the question is: is it bug, or it's intended behavior to limit signed integrals to values which can be safely abs()ed?
April 26, 2014
On Saturday, 26 April 2014 at 23:36:28 UTC, ketmar wrote:
> this code: std.conv.parse!byte("-128") throws error: "Overflow in integral conversion". but this is obviously not true, as signed byte can hold such value.

Check your math... the most negative number a signed byte can hold is -127. The most positive number it can hold is 128, but negating that wouldn't fit in eight bits.
April 26, 2014
ketmar:

> this code: std.conv.parse!byte("-128") throws error: "Overflow in integral conversion". but this is obviously not true, as signed byte can hold such value.
>
> the question is: is it bug, or it's intended behavior to limit signed integrals to values which can be safely abs()ed?

This code works to me:

void main() {
    import std.conv: to, parse;
    auto s1 = "-128";
    assert(s1.parse!byte == -128);
    immutable s2 = "-128";
    assert(s2.to!byte == -128);
}


What's your compiler version?

Bye,
bearophile
April 26, 2014
On 04/27/2014 01:43 AM, Adam D. Ruppe wrote:
> On Saturday, 26 April 2014 at 23:36:28 UTC, ketmar wrote:
>> this code: std.conv.parse!byte("-128") throws error: "Overflow in
>> integral conversion". but this is obviously not true, as signed byte
>> can hold such value.
>
> Check your math... the most negative number a signed byte can hold is
> -127. The most positive number it can hold is 128, but negating that
> wouldn't fit in eight bits.

Check your math. :o)
April 26, 2014
On Saturday, 26 April 2014 at 23:43:11 UTC, Adam D. Ruppe wrote:
> Check your math

sorry, i should check my own math. I got it backwards, you're right.
April 27, 2014
On 4/26/14, 4:36 PM, ketmar wrote:
> this code: std.conv.parse!byte("-128") throws error: "Overflow in
> integral conversion". but this is obviously not true, as signed byte can
> hold such value.
>
> the question is: is it bug, or it's intended behavior to limit signed
> integrals to values which can be safely abs()ed?

Bug. -- Andrei
April 27, 2014
On 4/26/14, 4:43 PM, Adam D. Ruppe wrote:
> On Saturday, 26 April 2014 at 23:36:28 UTC, ketmar wrote:
>> this code: std.conv.parse!byte("-128") throws error: "Overflow in
>> integral conversion". but this is obviously not true, as signed byte
>> can hold such value.
>
> Check your math... the most negative number a signed byte can hold is
> -127. The most positive number it can hold is 128, but negating that
> wouldn't fit in eight bits.

Oops. No bug. -- Andrei
April 27, 2014
ah, sorry, this is my own fault, there is no bug in parser. what i'm doing is parse!byte("128") and then negating the result.

silly me.
April 27, 2014
On Sunday, 27 April 2014 at 00:01:21 UTC, Andrei Alexandrescu wrote:
> Oops. No bug. -- Andrei

Nah, sorry, that was my giant mistake, I didn't actually do the math before saying "check your math" and let my brain get confused into thinking 10000000 was 128, but it is actually -128 in twos complement, the high bit is set so it is negative.
April 27, 2014
On Sunday, 27 April 2014 at 00:04:15 UTC, ketmar wrote:
but this is definetely bug, i think:

void main() {
  import std.stdio : writeln;
  import std.conv : to;
  writeln(to!int("29a", 16)); // 666
  writeln(to!int("+29a", 16)); // Unexpected '+' when converting from type string base 16 to type int
  //writeln(to!int("-29a", 16)); // Unexpected '-' when converting from type string base 16 to type int
}


it compiles, but throws exceptions on last two lines with writeln(). base 10 accepts '+' and '-' though. why other bases aren't?
« First   ‹ Prev
1 2