carpetemporem

c# Closer Helper

Apr 22nd, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.26 KB | None | 0 0
  1. public class Helper
  2. {
  3.     public static void BitsMinMax(long _bits, out double _min, out double _max)
  4.     {
  5.         // ? unsigned range with at least one value bit
  6.         if (_bits > 0)
  7.         {
  8.             _max = Math.Pow(2, _bits) - 1;
  9.             _min = 0;
  10.  
  11.             return;
  12.         }
  13.  
  14.         // ? signed range with one sign bit and at least one value bit
  15.         if (_bits < -1)
  16.         {
  17.             _min = -Math.Pow(2, -_bits - 1);
  18.             _max = -1 - _min;
  19.  
  20.             return;
  21.         }
  22.  
  23.         throw new System.ArgumentOutOfRangeException("_bits", _bits,
  24.          String.Format("-64 <= _bits <= -2 or 1 <= _bits <= 64"));
  25.     }
  26.  
  27.  
  28.     public static void CheckRange(
  29.      double _value, double _min, double _max, bool _check = true)
  30.     {
  31.         if (_check && (_value < _min || _value > _max))
  32.         {
  33.             throw new System.ArgumentOutOfRangeException(
  34.              "_value", _value, ValueRange2String(_value, _min, _max));
  35.         }
  36.     }
  37.  
  38.  
  39.     public static void CheckIntegerRange(
  40.      double _value, double _min, double _max, bool _check = true)
  41.     {
  42.         CheckRange(_value, _min, _max, _check);
  43.  
  44.         if (!(_value < 0 && Math.Ceiling(_value) == _value
  45.          || _value >= 0 && Math.Floor(_value) == _value))
  46.         {
  47.             throw new System.ArgumentException(
  48.              String.Format("_value ({0}) must be integral", _value));
  49.         }
  50.     }
  51.  
  52.  
  53.     public static string ValueRange2String(
  54.      double _value, double _min, double _max)
  55.     {
  56.         string format_;
  57.  
  58.         if (_value < _min)
  59.         {
  60.             format_ = "_value ({0}) < _min ({1}) <= _max ({2})";
  61.         }
  62.         else if (_value > _max)
  63.         {
  64.             format_ = "_min ({1}) <= _max ({2}) < _value ({0})";
  65.         }
  66.         else
  67.         {
  68.             format_ = "_min ({1}) <= _value ({0}) <= _max ({2})";
  69.         }
  70.  
  71.         return String.Format(format_, _value, _min, _max);
  72.     }
  73.  
  74.  
  75.     public static void Print(bool _print = true,
  76.      int _head_lines = 0, string _text = "", int _tail_lines = 0,
  77.      int _indent_count = 0, string _indent_string = " ")
  78.     {
  79.         if (_print)
  80.         {
  81.             Console.Write(String.Format("{0}{1}{2}{3}",
  82.              new String ('\n', _head_lines),
  83.              string.Concat(Enumerable.Repeat(_indent_string, _indent_count)),
  84.              _text, new String ('\n', _tail_lines)));
  85.         }
  86.     }
  87.  
  88.  
  89.     public static void PrintRange(
  90.      double _value, double _min, double _max, bool _print = true,
  91.      int _head_lines = 0, string _text = "", int _tail_lines = 0,
  92.      int _indent_count = 0, string _indent_string = " ")
  93.     {
  94.         if (_print)
  95.         {
  96.             if (_text != "")
  97.             {
  98.                 _text = String.Format("{0}: ", _text);
  99.             }
  100.  
  101.             Print(true, _head_lines, String.Format("{0}{1}",
  102.              _text, ValueRange2String(_value, _min, _max)), _tail_lines,
  103.              _indent_count, _indent_string);
  104.         }
  105.     }
  106.  
  107.  
  108.     public static void PrintCheckRange(
  109.      double _value, double _min, double _max, bool _print = true,
  110.      int _head_lines = 0, string _text = "", int _tail_lines = 0,
  111.      int _indent_count = 0, string _indent_string = " ", bool _check = true)
  112.     {
  113.         PrintRange(_value, _min, _max, _print,
  114.          _head_lines, _text, _tail_lines, _indent_count, _indent_string);
  115.         CheckRange(_value, _min, _max, _check);
  116.     }
  117. }
  118.  
  119.  
  120. class Test
  121. {
  122.     public static void Main()
  123.     {
  124.         bool is_closer_;
  125.  
  126.         Helper.Print(true, 1, String.Format("{0} {1}",
  127.          Closer.CheckPrint(-8, 7, 6, -4, out is_closer_), is_closer_), 1);
  128.  
  129.         Helper.Print(true, 1, String.Format("{0} {1}",
  130.          Closer.Check(-8, 7, 6, -4, out is_closer_), is_closer_), 1);
  131.  
  132.         Helper.Print(true, 1, String.Format("{0} {1}",
  133.          Closer.CheckPrint(15, 0, 14, 4, out is_closer_), is_closer_), 1);
  134.  
  135.         Helper.Print(true, 1, String.Format("{0} {1}",
  136.          Closer.Check(15, 0, 14, 4, out is_closer_), is_closer_), 1);
  137.  
  138.         Helper.Print(true, 1, String.Format("{0} {1}",
  139.          Closer.I1(-128, 127, 126, out is_closer_), is_closer_), 1);
  140.  
  141.         Helper.Print(true, 1, String.Format("{0} {1}",
  142.          Closer.N2(0, 254, 255, out is_closer_), is_closer_), 1);
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment