Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Helper
- {
- public static void BitsMinMax(long _bits, out double _min, out double _max)
- {
- // ? unsigned range with at least one value bit
- if (_bits > 0)
- {
- _max = Math.Pow(2, _bits) - 1;
- _min = 0;
- return;
- }
- // ? signed range with one sign bit and at least one value bit
- if (_bits < -1)
- {
- _min = -Math.Pow(2, -_bits - 1);
- _max = -1 - _min;
- return;
- }
- throw new System.ArgumentOutOfRangeException("_bits", _bits,
- String.Format("-64 <= _bits <= -2 or 1 <= _bits <= 64"));
- }
- public static void CheckRange(
- double _value, double _min, double _max, bool _check = true)
- {
- if (_check && (_value < _min || _value > _max))
- {
- throw new System.ArgumentOutOfRangeException(
- "_value", _value, ValueRange2String(_value, _min, _max));
- }
- }
- public static void CheckIntegerRange(
- double _value, double _min, double _max, bool _check = true)
- {
- CheckRange(_value, _min, _max, _check);
- if (!(_value < 0 && Math.Ceiling(_value) == _value
- || _value >= 0 && Math.Floor(_value) == _value))
- {
- throw new System.ArgumentException(
- String.Format("_value ({0}) must be integral", _value));
- }
- }
- public static string ValueRange2String(
- double _value, double _min, double _max)
- {
- string format_;
- if (_value < _min)
- {
- format_ = "_value ({0}) < _min ({1}) <= _max ({2})";
- }
- else if (_value > _max)
- {
- format_ = "_min ({1}) <= _max ({2}) < _value ({0})";
- }
- else
- {
- format_ = "_min ({1}) <= _value ({0}) <= _max ({2})";
- }
- return String.Format(format_, _value, _min, _max);
- }
- public static void Print(bool _print = true,
- int _head_lines = 0, string _text = "", int _tail_lines = 0,
- int _indent_count = 0, string _indent_string = " ")
- {
- if (_print)
- {
- Console.Write(String.Format("{0}{1}{2}{3}",
- new String ('\n', _head_lines),
- string.Concat(Enumerable.Repeat(_indent_string, _indent_count)),
- _text, new String ('\n', _tail_lines)));
- }
- }
- public static void PrintRange(
- double _value, double _min, double _max, bool _print = true,
- int _head_lines = 0, string _text = "", int _tail_lines = 0,
- int _indent_count = 0, string _indent_string = " ")
- {
- if (_print)
- {
- if (_text != "")
- {
- _text = String.Format("{0}: ", _text);
- }
- Print(true, _head_lines, String.Format("{0}{1}",
- _text, ValueRange2String(_value, _min, _max)), _tail_lines,
- _indent_count, _indent_string);
- }
- }
- public static void PrintCheckRange(
- double _value, double _min, double _max, bool _print = true,
- int _head_lines = 0, string _text = "", int _tail_lines = 0,
- int _indent_count = 0, string _indent_string = " ", bool _check = true)
- {
- PrintRange(_value, _min, _max, _print,
- _head_lines, _text, _tail_lines, _indent_count, _indent_string);
- CheckRange(_value, _min, _max, _check);
- }
- }
- class Test
- {
- public static void Main()
- {
- bool is_closer_;
- Helper.Print(true, 1, String.Format("{0} {1}",
- Closer.CheckPrint(-8, 7, 6, -4, out is_closer_), is_closer_), 1);
- Helper.Print(true, 1, String.Format("{0} {1}",
- Closer.Check(-8, 7, 6, -4, out is_closer_), is_closer_), 1);
- Helper.Print(true, 1, String.Format("{0} {1}",
- Closer.CheckPrint(15, 0, 14, 4, out is_closer_), is_closer_), 1);
- Helper.Print(true, 1, String.Format("{0} {1}",
- Closer.Check(15, 0, 14, 4, out is_closer_), is_closer_), 1);
- Helper.Print(true, 1, String.Format("{0} {1}",
- Closer.I1(-128, 127, 126, out is_closer_), is_closer_), 1);
- Helper.Print(true, 1, String.Format("{0} {1}",
- Closer.N2(0, 254, 255, out is_closer_), is_closer_), 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment