Example code on D homepage does not run successfully

Ralph Tandetzky via Digitalmars-d digitalmars-d at puremagic.com
Sat Oct 17 05:57:24 PDT 2015


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(immutable(char)[])+0x20) [0x4bdca4]
./f838(@safe immutable(char)[] 
std.functional.__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ.compose!(immutable(char)[]).compose(immutable(char)[])+0x20) [0x4bdc6c]
./f838(@property @safe immutable(char)[] 
std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7composeTAAyaZ.MapResult.front()+0x67) [0x4bffbb]
./f838(ref @safe 
std.algorithm.iteration.joiner!(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7composeTAAyaZ.MapResult, immutable(char)[]).joiner(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7composeTAAyaZ.MapResult, immutable(char)[]).Result std.algorithm.iteration.joiner!(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7composeTAAyaZ.MapResult, immutable(char)[]).joiner(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7composeTAAyaZ.MapResult, immutable(char)[]).Result.__ctor(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7composeTAAyaZ.MapResult, immutable(char)[])+0x64) [0x4c0448]
./f838(@safe 
std.algorithm.iteration.joiner!(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7composeTAAyaZ.MapResult, immutable(char)[]).joiner(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7composeTAAyaZ.MapResult, immutable(char)[]).Result std.algorithm.iteration.joiner!(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7composeTAAyaZ.MapResult, immutable(char)[]).joiner(std.algorithm.iteration.__T9MapResultS1633std10functional136__T7composeS253std4conv11__T2toTAyaZ2toS933std10functional67__T7composeS27_D3std4math5roundFNbNiNeeZeS223std4conv9__T2toTeZ2toZ7composeZ7composeTAAyaZ.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


More information about the Digitalmars-d mailing list