digitalmars.D - possible bug in std.conv.parse
- ketmar (5/5) Apr 26 2014 this code: std.conv.parse!byte("-128") throws error: "Overflow in
- Adam D. Ruppe (4/7) Apr 26 2014 Check your math... the most negative number a signed byte can
- Timon Gehr (2/9) Apr 26 2014 Check your math. :o)
- Adam D. Ruppe (3/4) Apr 26 2014 sorry, i should check my own math. I got it backwards, you're
- Andrei Alexandrescu (2/9) Apr 26 2014 Oops. No bug. -- Andrei
- Adam D. Ruppe (6/7) Apr 26 2014 Nah, sorry, that was my giant mistake, I didn't actually do the
- monarch_dodra (8/15) Apr 27 2014 Yeah, or just remember that signed representation are evenly
- bearophile (12/17) Apr 26 2014 This code works to me:
- ketmar (3/3) Apr 26 2014 ah, sorry, this is my own fault, there is no bug in parser. what
- Andrei Alexandrescu (2/7) Apr 26 2014 Bug. -- Andrei
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?
Apr 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.
Apr 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:Check your math. :o)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.
Apr 26 2014
On Saturday, 26 April 2014 at 23:43:11 UTC, Adam D. Ruppe wrote:Check your mathsorry, i should check my own math. I got it backwards, you're right.
Apr 26 2014
On 4/26/14, 4:43 PM, Adam D. Ruppe wrote:On Saturday, 26 April 2014 at 23:36:28 UTC, ketmar wrote:Oops. No bug. -- Andreithis 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.
Apr 26 2014
On Sunday, 27 April 2014 at 00:01:21 UTC, Andrei Alexandrescu wrote:Oops. No bug. -- AndreiNah, 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.
Apr 26 2014
On Sunday, 27 April 2014 at 00:07:22 UTC, Adam D. Ruppe wrote:On Sunday, 27 April 2014 at 00:01:21 UTC, Andrei Alexandrescu wrote:Yeah, or just remember that signed representation are evenly split between negatives and positives, but positives get the '0': Positives: [0 .. high); Negatives: [-high .. 0); It's an interesting source of bugs, and the reason you should "never" store an absolute value in a singed object. Since "-int.min" is still "int.min".Oops. No bug. -- AndreiNah, 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.
Apr 27 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
Apr 26 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.
Apr 26 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?
Apr 26 2014
ah, i see: if (radix == 10) return parse!Target(s); in Target parse(Target, Source)(ref Source s, uint radix) it cheating a little and using 'general' decimal number parser, which accepts '+' and '-'. for other bases it uses another code though, where '+' and '-' threats as digits, which leads to error. i think that it should either not accept sign in any radix, or accept always (given the resulting type is signed, of course). the later is much more logical if you'll ask me.
Apr 26 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
Apr 26 2014