Advertisement
Guest User

Untitled

a guest
Mar 10th, 2014
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.86 KB | None | 0 0
  1. #!/usr/bin/env rdmd
  2.  
  3. import core.exception;
  4. import std.stdio;
  5. import std.conv;
  6. import std.format;
  7.  
  8. void main(string[] args)
  9. {
  10.     args = args[1..$];
  11.     ulong precision, iterations;
  12.     ulong counter = 1;
  13.     double total = (4.0/counter);
  14.    
  15.     try
  16.     {
  17.         precision = to!ulong(args[0]);
  18.     }
  19.     catch(RangeError)
  20.     {
  21.         precision = 3;
  22.     }
  23.    
  24.     try
  25.     {
  26.         iterations = to!ulong(args[1]);
  27.     }
  28.     catch(RangeError)
  29.     {
  30.         iterations = 10000;
  31.     }
  32.     catch(ConvOverflowException)
  33.     {
  34.         writeln("Too many iterations!");
  35.        
  36.         return;
  37.     }
  38.    
  39.     for(int iii = 0; iii < iterations; iii++)
  40.     {
  41.         counter += 2;
  42.         total -= (4.0/counter);
  43.         counter += 2;
  44.         total += (4.0/counter);
  45.     }
  46.    
  47.     writefln("%." ~ to!string(precision) ~ "f", total);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement