Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add (fairly anemic) d implementation

  • Loading branch information...
commit cdc04835f876d2f45f845f46e8200f5b1362ec3f 1 parent 7aecbde
Greg Hewgill authored
1  README.md
Source Rendered
@@ -75,6 +75,7 @@ The expected output is:
75 75
     Checking ada... ok
76 76
     Checking cpp... ok
77 77
     Checking csharp... ok
  78
+    Checking d... ok
78 79
     Checking erlang... ok
79 80
     Checking go... ok
80 81
     Checking haskell... ok
1  SConstruct
... ...
@@ -1,6 +1,7 @@
1 1
 SConscript("ada/SConscript")
2 2
 SConscript("cpp/SConscript")
3 3
 SConscript("csharp/SConscript")
  4
+SConscript("d/SConscript")
4 5
 SConscript("erlang/SConscript")
5 6
 SConscript("go/SConscript")
6 7
 SConscript("java/SConscript")
2  d/.gitignore
... ...
@@ -0,0 +1,2 @@
  1
+*.o
  2
+test
8  d/SConscript
... ...
@@ -0,0 +1,8 @@
  1
+# This SConscript file is the basic minimum needed to compile
  2
+# the D 2.0 test program with SCons on Linux.
  3
+
  4
+env = Environment(DFLAGS="-Id")
  5
+env.Program("test", Split("""
  6
+    test.d
  7
+    log_factorial.d
  8
+"""), LIBS=["phobos2", "pthread", "rt", "m"])
11  d/info.html
... ...
@@ -0,0 +1,11 @@
  1
+<p>
  2
+The D standard <a href="http://www.digitalmars.com/d/2.0/phobos/std_mathspecial.html">"std.mathspecial" module</a> provides implementations of:
  3
+</p>
  4
+
  5
+<ul>
  6
+<li><a href="http://www.digitalmars.com/d/2.0/phobos/std_mathspecial.html#erf">erf</a></li>
  7
+<li><a href="http://www.digitalmars.com/d/2.0/phobos/std_math.html#expm1">expm1</a></li>
  8
+<li><a href="http://www.digitalmars.com/d/2.0/phobos/std_mathspecial.html#normalDistribution">phi</a></li>
  9
+<li><a href="http://www.digitalmars.com/d/2.0/phobos/std_mathspecial.html#normalDistributionInverse">NormalCDFInverse</a></li>
  10
+<li><a href="http://www.digitalmars.com/d/2.0/phobos/std_mathspecial.html#gamma">Gamma</a> and <a href="http://www.digitalmars.com/d/2.0/phobos/std_mathspecial.html#logGamma">Log Gamma</a></li>
  11
+</ul>
269  d/log_factorial.d
... ...
@@ -0,0 +1,269 @@
  1
+module log_factorial;
  2
+
  3
+import std.math;
  4
+
  5
+real log_factorial(uint n) {
  6
+    if (n > 254) {
  7
+        real x = n + 1;
  8
+        return (x - 0.5)*log(x) - x + 0.5*log(2*PI) + 1.0/(12.0*x);
  9
+    } else {
  10
+        immutable lf = [
  11
+            0.000000000000000,
  12
+            0.000000000000000,
  13
+            0.693147180559945,
  14
+            1.791759469228055,
  15
+            3.178053830347946,
  16
+            4.787491742782046,
  17
+            6.579251212010101,
  18
+            8.525161361065415,
  19
+            10.604602902745251,
  20
+            12.801827480081469,
  21
+            15.104412573075516,
  22
+            17.502307845873887,
  23
+            19.987214495661885,
  24
+            22.552163853123421,
  25
+            25.191221182738683,
  26
+            27.899271383840894,
  27
+            30.671860106080675,
  28
+            33.505073450136891,
  29
+            36.395445208033053,
  30
+            39.339884187199495,
  31
+            42.335616460753485,
  32
+            45.380138898476908,
  33
+            48.471181351835227,
  34
+            51.606675567764377,
  35
+            54.784729398112319,
  36
+            58.003605222980518,
  37
+            61.261701761002001,
  38
+            64.557538627006323,
  39
+            67.889743137181526,
  40
+            71.257038967168000,
  41
+            74.658236348830158,
  42
+            78.092223553315307,
  43
+            81.557959456115029,
  44
+            85.054467017581516,
  45
+            88.580827542197682,
  46
+            92.136175603687079,
  47
+            95.719694542143202,
  48
+            99.330612454787428,
  49
+            102.968198614513810,
  50
+            106.631760260643450,
  51
+            110.320639714757390,
  52
+            114.034211781461690,
  53
+            117.771881399745060,
  54
+            121.533081515438640,
  55
+            125.317271149356880,
  56
+            129.123933639127240,
  57
+            132.952575035616290,
  58
+            136.802722637326350,
  59
+            140.673923648234250,
  60
+            144.565743946344900,
  61
+            148.477766951773020,
  62
+            152.409592584497350,
  63
+            156.360836303078800,
  64
+            160.331128216630930,
  65
+            164.320112263195170,
  66
+            168.327445448427650,
  67
+            172.352797139162820,
  68
+            176.395848406997370,
  69
+            180.456291417543780,
  70
+            184.533828861449510,
  71
+            188.628173423671600,
  72
+            192.739047287844900,
  73
+            196.866181672889980,
  74
+            201.009316399281570,
  75
+            205.168199482641200,
  76
+            209.342586752536820,
  77
+            213.532241494563270,
  78
+            217.736934113954250,
  79
+            221.956441819130360,
  80
+            226.190548323727570,
  81
+            230.439043565776930,
  82
+            234.701723442818260,
  83
+            238.978389561834350,
  84
+            243.268849002982730,
  85
+            247.572914096186910,
  86
+            251.890402209723190,
  87
+            256.221135550009480,
  88
+            260.564940971863220,
  89
+            264.921649798552780,
  90
+            269.291097651019810,
  91
+            273.673124285693690,
  92
+            278.067573440366120,
  93
+            282.474292687630400,
  94
+            286.893133295426990,
  95
+            291.323950094270290,
  96
+            295.766601350760600,
  97
+            300.220948647014100,
  98
+            304.686856765668720,
  99
+            309.164193580146900,
  100
+            313.652829949878990,
  101
+            318.152639620209300,
  102
+            322.663499126726210,
  103
+            327.185287703775200,
  104
+            331.717887196928470,
  105
+            336.261181979198450,
  106
+            340.815058870798960,
  107
+            345.379407062266860,
  108
+            349.954118040770250,
  109
+            354.539085519440790,
  110
+            359.134205369575340,
  111
+            363.739375555563470,
  112
+            368.354496072404690,
  113
+            372.979468885689020,
  114
+            377.614197873918670,
  115
+            382.258588773060010,
  116
+            386.912549123217560,
  117
+            391.575988217329610,
  118
+            396.248817051791490,
  119
+            400.930948278915760,
  120
+            405.622296161144900,
  121
+            410.322776526937280,
  122
+            415.032306728249580,
  123
+            419.750805599544780,
  124
+            424.478193418257090,
  125
+            429.214391866651570,
  126
+            433.959323995014870,
  127
+            438.712914186121170,
  128
+            443.475088120918940,
  129
+            448.245772745384610,
  130
+            453.024896238496130,
  131
+            457.812387981278110,
  132
+            462.608178526874890,
  133
+            467.412199571608080,
  134
+            472.224383926980520,
  135
+            477.044665492585580,
  136
+            481.872979229887900,
  137
+            486.709261136839360,
  138
+            491.553448223298010,
  139
+            496.405478487217580,
  140
+            501.265290891579240,
  141
+            506.132825342034830,
  142
+            511.008022665236070,
  143
+            515.890824587822520,
  144
+            520.781173716044240,
  145
+            525.679013515995050,
  146
+            530.584288294433580,
  147
+            535.496943180169520,
  148
+            540.416924105997740,
  149
+            545.344177791154950,
  150
+            550.278651724285620,
  151
+            555.220294146894960,
  152
+            560.169054037273100,
  153
+            565.124881094874350,
  154
+            570.087725725134190,
  155
+            575.057539024710200,
  156
+            580.034272767130800,
  157
+            585.017879388839220,
  158
+            590.008311975617860,
  159
+            595.005524249382010,
  160
+            600.009470555327430,
  161
+            605.020105849423770,
  162
+            610.037385686238740,
  163
+            615.061266207084940,
  164
+            620.091704128477430,
  165
+            625.128656730891070,
  166
+            630.172081847810200,
  167
+            635.221937855059760,
  168
+            640.278183660408100,
  169
+            645.340778693435030,
  170
+            650.409682895655240,
  171
+            655.484856710889060,
  172
+            660.566261075873510,
  173
+            665.653857411105950,
  174
+            670.747607611912710,
  175
+            675.847474039736880,
  176
+            680.953419513637530,
  177
+            686.065407301994010,
  178
+            691.183401114410800,
  179
+            696.307365093814040,
  180
+            701.437263808737160,
  181
+            706.573062245787470,
  182
+            711.714725802289990,
  183
+            716.862220279103440,
  184
+            722.015511873601330,
  185
+            727.174567172815840,
  186
+            732.339353146739310,
  187
+            737.509837141777440,
  188
+            742.685986874351220,
  189
+            747.867770424643370,
  190
+            753.055156230484160,
  191
+            758.248113081374300,
  192
+            763.446610112640200,
  193
+            768.650616799717000,
  194
+            773.860102952558460,
  195
+            779.075038710167410,
  196
+            784.295394535245690,
  197
+            789.521141208958970,
  198
+            794.752249825813460,
  199
+            799.988691788643450,
  200
+            805.230438803703120,
  201
+            810.477462875863580,
  202
+            815.729736303910160,
  203
+            820.987231675937890,
  204
+            826.249921864842800,
  205
+            831.517780023906310,
  206
+            836.790779582469900,
  207
+            842.068894241700490,
  208
+            847.352097970438420,
  209
+            852.640365001133090,
  210
+            857.933669825857460,
  211
+            863.231987192405430,
  212
+            868.535292100464630,
  213
+            873.843559797865740,
  214
+            879.156765776907600,
  215
+            884.474885770751830,
  216
+            889.797895749890240,
  217
+            895.125771918679900,
  218
+            900.458490711945270,
  219
+            905.796028791646340,
  220
+            911.138363043611210,
  221
+            916.485470574328820,
  222
+            921.837328707804890,
  223
+            927.193914982476710,
  224
+            932.555207148186240,
  225
+            937.921183163208070,
  226
+            943.291821191335660,
  227
+            948.667099599019820,
  228
+            954.046996952560450,
  229
+            959.431492015349480,
  230
+            964.820563745165940,
  231
+            970.214191291518320,
  232
+            975.612353993036210,
  233
+            981.015031374908400,
  234
+            986.422203146368590,
  235
+            991.833849198223450,
  236
+            997.249949600427840,
  237
+            1002.670484599700300,
  238
+            1008.095434617181700,
  239
+            1013.524780246136200,
  240
+            1018.958502249690200,
  241
+            1024.396581558613400,
  242
+            1029.838999269135500,
  243
+            1035.285736640801600,
  244
+            1040.736775094367400,
  245
+            1046.192096209724900,
  246
+            1051.651681723869200,
  247
+            1057.115513528895000,
  248
+            1062.583573670030100,
  249
+            1068.055844343701400,
  250
+            1073.532307895632800,
  251
+            1079.012946818975000,
  252
+            1084.497743752465600,
  253
+            1089.986681478622400,
  254
+            1095.479742921962700,
  255
+            1100.976911147256000,
  256
+            1106.478169357800900,
  257
+            1111.983500893733000,
  258
+            1117.492889230361000,
  259
+            1123.006317976526100,
  260
+            1128.523770872990800,
  261
+            1134.045231790853000,
  262
+            1139.570684729984800,
  263
+            1145.100113817496100,
  264
+            1150.633503306223700,
  265
+            1156.170837573242400,
  266
+        ];
  267
+        return lf[n];
  268
+    }
  269
+}
45  d/test.d
... ...
@@ -0,0 +1,45 @@
  1
+import std.array;
  2
+import std.conv;
  3
+import std.mathspecial;
  4
+import std.stdio;
  5
+
  6
+import log_factorial: log_factorial;
  7
+
  8
+void main() {
  9
+    for (;;) {
  10
+        string s = readln();
  11
+        if (s == null) {
  12
+            break;
  13
+        }
  14
+        string[] a = split(s);
  15
+        string f = a[0];
  16
+        real x = to!real(a[1]);
  17
+        switch (f) {
  18
+            case "erf":
  19
+                writefln("%.15g", erf(x));
  20
+                break;
  21
+            case "expm1":
  22
+                writefln("%.15g", expm1(x));
  23
+                break;
  24
+            case "phi":
  25
+                writefln("%.15g", normalDistribution(x));
  26
+                break;
  27
+            case "NormalCDFInverse":
  28
+                writefln("%.15g", normalDistributionInverse(x));
  29
+                break;
  30
+            case "Gamma":
  31
+                writefln("%.15g", gamma(x));
  32
+                break;
  33
+            case "LogGamma":
  34
+                writefln("%.15g", lgamma(x));
  35
+                break;
  36
+            case "LogFactorial":
  37
+                writefln("%.15g", log_factorial(cast(uint)x));
  38
+                break;
  39
+            default:
  40
+                writeln("Unknown function: ", f);
  41
+                return;
  42
+        }
  43
+        stdout.flush();
  44
+    }
  45
+}
1  gen.py
@@ -14,6 +14,7 @@
14 14
     "ada":          {"name": "Ada",                     "suffix": ".a"},
15 15
     "cpp":          {"name": "C++",                     "suffix": ".cpp"},
16 16
     "csharp":       {"name": "C#",                      "suffix": ".cs"},
  17
+    "d":            {"name": "D",                       "suffix": ".d"},
17 18
     "erlang":       {"name": "Erlang",                  "suffix": ".erl"},
18 19
     "go":           {"name": "Go",                      "suffix": ".go"},
19 20
     "haskell":      {"name": "Haskell",                 "suffix": ".hs"},
1  index.template
@@ -91,6 +91,7 @@ The expected output is:
91 91
     Checking ada... ok
92 92
     Checking cpp... ok
93 93
     Checking csharp... ok
  94
+    Checking d... ok
94 95
     Checking erlang... ok
95 96
     Checking go... ok
96 97
     Checking haskell... ok
1  test.py
@@ -7,6 +7,7 @@
7 7
     "ada":          "./test",
8 8
     "cpp":          "./test",
9 9
     "csharp":       ["mono", "test.exe"],
  10
+    "d":            "./test",
10 11
     "erlang":       ["escript", "test.erl"],
11 12
     "go":           "./test",
12 13
     "haskell":      ["runhaskell", "test.hs"],

0 notes on commit cdc0483

Please sign in to comment.
Something went wrong with that request. Please try again.