www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Example code on D homepage does not run successfully

reply Ralph Tandetzky <ralph.tandetzky gmail.com> writes:
Hi! The following code snippet, that appears on the dlang.org 
homepage does not run successfully:


// Round floating point numbers
import std.algorithm,
     std.conv,
     std.functional,
     std.math,
     std.regex,
     std.stdio;

// Transforms input into a real number,
// rounds it, then to a string
alias round = pipe!(to!real, std.math.round, to!string);

// Matches numbers that look like they need rounding
static reFloatingPoint = ctRegex!`[0-9]+\.[0-9]+`;

void main(string[] args)
{
     // If arguments, process those and exit,
     // otherwise wait around for input on stdin
     if (args.length > 1)
         args[1..$].map!round.joiner(" ").writeln;
     else
         // Replace anything that looks like a real
         // number with the rounded equivalent.
         stdin.byLine(KeepTerminator.yes)
              .map!(l => l.replaceAll!(c => c.hit.round)
                                      (reFloatingPoint))
              .copy(stdout.lockingTextWriter());
}


It produces the following error message.


std.conv.ConvException /opt/compilers/dmd2/include/std/conv.d(2417): Floating
point conversion error for input "".
----------------
./f838(pure  safe real std.conv.parse!(real, 
immutable(char)[]).parse(ref immutable(char)[])+0x43) [0x4bdd27]
./f838(pure  safe real std.conv.toImpl!(real, 
immutable(char)[]).toImpl(immutable(char)[])+0x26) [0x4bf0f2]
./f838(pure  safe real 
std.conv.to!(real).to!(immutable(char)[]).to(immutable(char)[])+0x20) [0x4bdcdc]
./f838( safe real 
std.functional.__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ.compose!(immutable(char)[]).compose(immu
able(char)[])+0x20) [0x4bdca4]
./f838( safe immutable(char)[] 
std.functional.__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ.compose!(immutable(char)[]).compose(immu
able(char)[])+0x20) [0x4bdc6c]
./f838( property  safe immutable(char)[] 
std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7composeTAAyaZ.Map
esult.front()+0x67) [0x4bffbb]
./f838(ref  safe 
std.algorithm.iteration.joiner!(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7comp
seTAAyaZ.MapResult, immutable(char)[]).joiner(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7comp
seTAAyaZ.MapResult, immutable(char)[]).Result
std.algorithm.iteration.joiner!(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7comp
seTAAyaZ.MapResult, immutable(char)[]).joiner(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7comp
seTAAyaZ.MapResult, immutable(char)[]).Result.__ctor(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7comp
seTAAyaZ.MapResult, immutable(char)[])+0x64) [0x4c0448]
./f838( safe 
std.algorithm.iteration.joiner!(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7comp
seTAAyaZ.MapResult, immutable(char)[]).joiner(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7comp
seTAAyaZ.MapResult, immutable(char)[]).Result
std.algorithm.iteration.joiner!(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7comp
seTAAyaZ.MapResult, immutable(char)[]).joiner(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7comp
seTAAyaZ.MapResult, immutable(char)[])+0x6e) [0x4c0292]
./f838(_Dmain+0x6f) [0x49e87b]
./f838(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x1f)
[0x4d1487]
./f838(void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate())+0x2a) 
[0x4d13e2]
./f838(void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).runAll()+0x2b) [0x4d1443]
./f838(void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate())+0x2a) 
[0x4d13e2]
./f838(_d_run_main+0x1d2) [0x4d1362]
./f838(main+0x12) [0x4ccd9e]
/usr/lib/libc.so.6(__libc_start_main+0xf5) [0x40967a15]


If I understand this cryptic error message correctly, the problem 
is missing or inadequate input. There is no floating point number 
to be parsed and hence the string "" is parsed unsuccessfully 
which leads to an exception producing this horrible error 
message. I would suggest to fix the program quickly, maybe by 
using a string that is not given at runtime through stdin, but as 
a fixed string like "Pi is approximately 3.14159265."

Also, I think, the code is overly complicated for first time 
viewers of the website and is not likely to get newcomers excited 
about the langage. Here's an idea for a simplified version of the 
application:


// Round floating point numbers
import std.algorithm,
     std.conv,
     std.math,
     std.regex,
     std.stdio;

// Matches numbers that look like they need rounding
static reFloatingPoint = ctRegex!`[0-9]+\.[0-9]+`;

void main(string[] args)
{
     const text = "Pi is approximately 3.14159265.";

     text.replaceAll!( c => c.hit.to!real.round.to!string )
                     ( reFloatingPoint )
         .writeln;
}


Keep up the good work!
Ralph
Oct 17 2015
next sibling parent reply NX <nightmarex1337 hotmail.com> writes:
On Saturday, 17 October 2015 at 12:57:25 UTC, Ralph Tandetzky 
wrote:
 If I understand this cryptic error message correctly, the 
 problem is missing or inadequate input. There is no floating 
 point number to be parsed and hence the string "" is parsed 
 unsuccessfully which leads to an exception producing this 
 horrible error message. I would suggest to fix the program 
 quickly, maybe by using a string that is not given at runtime 
 through stdin, but as a fixed string like "Pi is approximately 
 3.14159265."
It's not because input is empty, but rather that across-web compiling is highly broken and problematic. There is an "Input" button bottom of the code area which shows what input will be given to stdin, which is actually not empty (it's written: "2.4 plus 2.4 equals 5 for sufficiently large values of 2.") Maybe one day someone will take care of improving the infrastructure...
Oct 17 2015
next sibling parent Ralph Tandetzky <ralph.tandetzky gmail.com> writes:
On Saturday, 17 October 2015 at 17:02:07 UTC, NX wrote:
 It's not because input is empty, but rather that across-web 
 compiling is highly broken and problematic. There is an "Input" 
 button bottom of the code area which shows what input will be 
 given to stdin, which is actually not empty (it's written: "2.4 
 plus 2.4 equals 5 for sufficiently large values of 2.")

 Maybe one day someone will take care of improving the 
 infrastructure...
I see. Well, thanks for the quick reply. A quick fix would be adapting the source code, so it compiles with the given system, wouldn't it? Anyways, my aim was to bring the issue to attention and provide a possible solution. I don't know how to change the examples on the web site, so I just posted one here, so someone with the access rights can do the actual change.
Oct 17 2015
prev sibling parent bachmeier <no spam.net> writes:
On Saturday, 17 October 2015 at 17:02:07 UTC, NX wrote:
 Maybe one day someone will take care of improving the 
 infrastructure...
The right thing to do is to get it off the homepage until it's fixed. Few visitors would not move on after getting an error where one should never occur. And as stated by the OP, that particular code is way too complicated to be used to promote the language anyway.
Oct 17 2015
prev sibling parent Vladimir Panteleev <thecybershadow.lists gmail.com> writes:
On Saturday, 17 October 2015 at 12:57:25 UTC, Ralph Tandetzky 
wrote:
 If I understand this cryptic error message correctly, the 
 problem is missing or inadequate input. There is no floating 
 point number to be parsed and hence the string "" is parsed 
 unsuccessfully which leads to an exception producing this 
 horrible error message.
https://issues.dlang.org/show_bug.cgi?id=15050
Oct 17 2015