www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Odd to!string call

reply Andrej Mitrovic <none none.none> writes:
I found this by accident:

import std.stdio;
import std.conv;

void main()
{
    writeln(to!string(2, 2));  // writes 10
    writeln(to!string(1, 0));  // std.conv.ConvException: Radix error
}

I'm not sure why "std.conv.to" would even take multiple arguments. Bugzilla?
Dec 21 2010
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 21 Dec 2010 13:38:06 -0500, Andrej Mitrovic <none none.none> wrote:

 I found this by accident:

 import std.stdio;
 import std.conv;

 void main()
 {
     writeln(to!string(2, 2));  // writes 10
     writeln(to!string(1, 0));  // std.conv.ConvException: Radix error
 }

 I'm not sure why "std.conv.to" would even take multiple arguments.  
 Bugzilla?
Would guess that the second arg is the base to use? 2 in base-2 (binary is 10), and radix usually means the base. Just looked it up, go to this page and search for 'radix': http://www.digitalmars.com/d/2.0/phobos/std_conv.html -Steve
Dec 21 2010
prev sibling next sibling parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Andrej Mitrovic <none none.none> wrote:

 I found this by accident:

 import std.stdio;
 import std.conv;

 void main()
 {
     writeln(to!string(2, 2));  // writes 10
     writeln(to!string(1, 0));  // std.conv.ConvException: Radix error
 }

 I'm not sure why "std.conv.to" would even take multiple arguments.  
 Bugzilla?
The second parameter is (as indicated by the exception) the radix[1]. With a radix of 2, 2 is written 10, as radix is binary. Radix 0 makes no sense, and thus gives an exception. It could be said though, that std.conv's documentation is confusing at best, and this could be worth putting in Bugzilla. [1]: http://en.wikipedia.org/wiki/Radix#Bases_and_positional_numeral_systems -- Simen
Dec 21 2010
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Right. Thanks, guys.

I do see how this could possibly cause bugs for the uninitiated.
Someone who is new to D might attempt to use to!string with multiple
arguments, and end up with buggy code like this:

import std.stdio;
import std.conv;

void main()
{
    int x = 2;
    int y = 4;

    // more code..

    writeln(to!string(x, y));  // writes "2", not "2 4", and not "24"
}


On 12/21/10, Simen kjaeraas <simen.kjaras gmail.com> wrote:
 Andrej Mitrovic <none none.none> wrote:

 I found this by accident:

 import std.stdio;
 import std.conv;

 void main()
 {
     writeln(to!string(2, 2));  // writes 10
     writeln(to!string(1, 0));  // std.conv.ConvException: Radix error
 }

 I'm not sure why "std.conv.to" would even take multiple arguments.
 Bugzilla?
The second parameter is (as indicated by the exception) the radix[1]. With a radix of 2, 2 is written 10, as radix is binary. Radix 0 makes no sense, and thus gives an exception. It could be said though, that std.conv's documentation is confusing at best, and this could be worth putting in Bugzilla. [1]: http://en.wikipedia.org/wiki/Radix#Bases_and_positional_numeral_systems -- Simen
Dec 21 2010
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
And yes i know writeln() doesn't need std.conv, writeln could be any
other function expecting a string.

On 12/21/10, Andrej Mitrovic <andrej.mitrovich gmail.com> wrote:
 Right. Thanks, guys.

 I do see how this could possibly cause bugs for the uninitiated.
 Someone who is new to D might attempt to use to!string with multiple
 arguments, and end up with buggy code like this:

 import std.stdio;
 import std.conv;

 void main()
 {
     int x = 2;
     int y = 4;

     // more code..

     writeln(to!string(x, y));  // writes "2", not "2 4", and not "24"
 }


 On 12/21/10, Simen kjaeraas <simen.kjaras gmail.com> wrote:
 Andrej Mitrovic <none none.none> wrote:

 I found this by accident:

 import std.stdio;
 import std.conv;

 void main()
 {
     writeln(to!string(2, 2));  // writes 10
     writeln(to!string(1, 0));  // std.conv.ConvException: Radix error
 }

 I'm not sure why "std.conv.to" would even take multiple arguments.
 Bugzilla?
The second parameter is (as indicated by the exception) the radix[1]. With a radix of 2, 2 is written 10, as radix is binary. Radix 0 makes no sense, and thus gives an exception. It could be said though, that std.conv's documentation is confusing at best, and this could be worth putting in Bugzilla. [1]: http://en.wikipedia.org/wiki/Radix#Bases_and_positional_numeral_systems -- Simen
Dec 21 2010
prev sibling parent Pelle <pelle.mansson gmail.com> writes:
On 12/21/2010 07:38 PM, Andrej Mitrovic wrote:
 I found this by accident:

 import std.stdio;
 import std.conv;

 void main()
 {
      writeln(to!string(2, 2));  // writes 10
      writeln(to!string(1, 0));  // std.conv.ConvException: Radix error
 }

 I'm not sure why "std.conv.to" would even take multiple arguments. Bugzilla?
to! does some fancy stuff, like here: auto myarr = [1,2,3]; writeln(to!string(myarr, "myarr:\n", "\n", "\n-----\n"); will write (untested, but should work :-) myarr: 1 2 3 ----- I find this most useful a lot of the time.
Dec 26 2010