From fffae3b519e4958ec5f7313b2da788fffd38f689 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 19 Jan 2022 23:14:56 -0500 Subject: [PATCH] Show decimal part by default in tick format fn --- .../spark_core/data/ticks/TickExtensions.cs | 786 +++++++++--------- 1 file changed, 396 insertions(+), 390 deletions(-) diff --git a/src/client/spark_core/data/ticks/TickExtensions.cs b/src/client/spark_core/data/ticks/TickExtensions.cs index ce3e23726..4a60f3053 100644 --- a/src/client/spark_core/data/ticks/TickExtensions.cs +++ b/src/client/spark_core/data/ticks/TickExtensions.cs @@ -1,390 +1,396 @@ -using bts; -using bts.api; -using bts.utils; -using mbts.spark; -using mbts.user.settings; -using System; -using System.Globalization; -using static bts.utils.MathUtils; - -namespace mbts { - public static class TickExtensions { - public static string _nan_text => TickDisplaySettings.Default.nan_text; - - #region Public Extensions (is_valid, format, parse, tick_up, tick_down) - public static bool is_valid(this TickFactors tick) => - (tick.tick_unit > 0 && tick.tick_unit <= (tick.tick_base * 0.5)) || - tick.tick_unit == tick.tick_base; - - public static bool is_valid(this RegionalTickFactors regional_tick) => - regional_tick.low_tick.is_valid() && regional_tick.high_tick.is_valid(); - - public static bool is_integer(this TickFactors tick) => - tick.tick_unit == tick.tick_base; - - public static double tick_unit_frac(this TickFactors tick) => - tick.tick_unit / tick.tick_base; - - public static double reduced_base(this TickFactors tick) => factor_out(tick.tick_base, 10); - - public static string format_without_decimal_part(this TickFactors tick, double value) => - format(tick, value, double.NaN, false, _nan_text, true); - - public static string format(this TickFactors tick, double value) => - format(tick, value, double.NaN, false, _nan_text); - - public static string format(this TickFactors tick, double value, bool show_commas) => - format(tick, value, double.NaN, show_commas, _nan_text); - - public static string format(this TickFactors tick, double value, double display_multiplier) => - format(tick, value, display_multiplier, false, _nan_text); - - public static string format(this TickFactors tick, double value, double display_multiplier, bool show_commas) => - format(tick, value, display_multiplier, show_commas, _nan_text); - - public static string format(this TickFactors tick, double value, TickDisplaySettings settings) { - if (settings is null) - settings = TickDisplaySettings.Default; - if (double.IsNaN(value)) return settings.nan_text; - if (double.IsInfinity(value)) return value.ToString(); - if (!tick.is_valid()) return _format_no_tick(value, settings.display_multiplier, settings.show_commas); - return _format_tick(tick, value, settings); - } - - public static string format(this TickFactors tick, double value, double display_multiplier, bool show_commas, string nan_text, bool show_decimal_part = false) => - tick.format(value, TickDisplaySettings.Default.with( - display_multiplier: display_multiplier, - show_commas: show_commas, - nan_text: nan_text, - show_decimal_part: show_decimal_part)); - - public static string format_without_decimal_part(this RegionalTickFactors regional_tick, double value) => - format_without_decimal_part(value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick, value); - - public static string format(this RegionalTickFactors regional_tick, double value, TickDisplaySettings settings = null) => - format(value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick, value, settings); - - public static string format(this RegionalTickFactors regional_tick, double value, double display_multiplier) => - format(value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick, value, display_multiplier); - - public static string format(this RegionalTickFactors regional_tick, double value, double display_multiplier, bool show_commas, string nan_text) => - format(value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick, value, display_multiplier, show_commas, nan_text); - - public static double parse(this TickFactors tick, string text) => - parse(tick, text, double.NaN, _nan_text); - - public static double parse(this TickFactors tick, string text, double display_multiplier) => - parse(tick, text, display_multiplier, _nan_text); - - public static double parse(this TickFactors tick, string text, double display_multiplier, string nan_text) { - if (!tick.is_valid()) return _parse_no_tick(text, display_multiplier); - return _parse(tick, text, display_multiplier, nan_text); - } - - public static double parse(this RegionalTickFactors regional_tick, string text) => - parse(regional_tick, text, double.NaN, _nan_text); - - public static double parse(this RegionalTickFactors regional_tick, string text, double display_multiplier) => - parse(regional_tick, text, display_multiplier, _nan_text); - - public static double parse(this RegionalTickFactors regional_tick, string text, double display_multiplier, string nan_text) { - if (!regional_tick.is_valid()) return _parse_no_tick(text, display_multiplier); - return _parse(regional_tick, text, display_multiplier, nan_text); - } - - public static double tickup(this TickFactors tick, double input) { return tick.tickup(input, 1); } - public static double tickup(this TickFactors tick, double input, uint count) { - if (DoubleHelper.IsNaNOrInfinity(input)) return input; - double tuf = tick.tick_unit_frac(); - input -= input % tuf; - double val = input + tuf * count; - return _round_tick(tuf, val); - } - - public static double tickup(this RegionalTickFactors regional_tick, double input) => - regional_tick.tickup(input, 1); - - public static double tickup(this RegionalTickFactors regional_tick, double input, uint count) => - regional_tick.tickup(input, count, TickUtils.precision_const); - - public static double tickup(this RegionalTickFactors regional_tick, double input, uint count, double precision, bool bling = true) { - if (!precision.is_real_positive()) - throw new ArgumentException(nameof(precision), "Must be a real positive number."); - if (DoubleHelper.IsNaNOrInfinity(input) || count == 0) return input; - double val, tuf; - if (input < regional_tick.low_tick_inclusive_end) { - tuf = regional_tick.low_tick.tick_unit_frac(); - double ieeer = Math.IEEERemainder(input, tuf); - input -= ieeer; - if (ieeer < -precision) count--; - val = input + tuf * count; - if (val > regional_tick.low_tick_inclusive_end) { - count -= (uint)MathUtils.round_up((regional_tick.low_tick_inclusive_end - input) / tuf, precision); - tuf = regional_tick.high_tick.tick_unit_frac(); - val = regional_tick.low_tick_inclusive_end + tuf * count; - } - } else { - tuf = regional_tick.high_tick.tick_unit_frac(); - double ieeer = Math.IEEERemainder(input, tuf); - input -= ieeer; - if (ieeer < -precision) count--; - val = input + tuf * count; - } - return _round_tick(tuf, val); - } - - public static ContractSpecification to_spec(this TickFactors tick) { - if (tick == null) - return null; - double tick_frac = tick.tick_unit_frac(); - return new ContractSpecification { - LowTick = tick_frac, - HighTick = tick_frac, - PriceBreak = double.MaxValue, - MainFraction = (uint)factor_out(tick.tick_base, 10), - PriceDisplayFormat = (uint)tick.display_digits(), - PriceMultiplier = 1, - CashPerPoint = 1 - }; - } - - public static bool try_get_tickdiff(this RegionalTickFactors regional_tick, double x, double y, out int n_low, out int n_high) { - if (DoubleHelper.IsNaNOrInfinity(x) || DoubleHelper.IsNaNOrInfinity(y)) { - n_low = default; - n_high = default; - return false; - } - (n_low, n_high) = tickdiff_unchecked(regional_tick, x, y); - return true; - } - - /// - /// Callers should be certain that and are not nan or infinity. - /// - /// - /// - /// - internal static (int n_low, int n_high) tickdiff_unchecked(this RegionalTickFactors regional_tick, double x, double y) { - double ltie = regional_tick.low_tick_inclusive_end; - double raw_diff = x - y; - if (x < ltie) { - double low_unit_frac = regional_tick.low_tick.tick_unit_frac(); - if (y < ltie) { - return (RoundFractionalUnits(raw_diff, low_unit_frac), 0); - } else { - double high_unit_frac = regional_tick.high_tick.tick_unit_frac(); - double low_diff = ltie - x; - double high_diff = raw_diff - low_diff; - int n_low = RoundFractionalUnits(low_diff, low_unit_frac); - int n_high = RoundFractionalUnits(high_diff, high_unit_frac); - return (n_low, n_high); - } - } else { - double high_unit_frac = regional_tick.high_tick.tick_unit_frac(); - if (y < ltie) { - double low_unit_frac = regional_tick.low_tick.tick_unit_frac(); - double high_diff = x - ltie; - double low_diff = raw_diff - high_diff; - int n_low = RoundFractionalUnits(low_diff, low_unit_frac); - int n_high = RoundFractionalUnits(high_diff, high_unit_frac); - return (n_low, n_high); - } else { - return (0, RoundFractionalUnits(raw_diff, high_unit_frac)); - } - } - } - - static int _count_ticks(double tick_unit_frac, double value) => - (int)Math.Round(value / tick_unit_frac); - - public static double tickdown(this TickFactors tick, double input) { return tick.tickdown(input, 1); } - public static double tickdown(this TickFactors tick, double input, uint count) { - if (DoubleHelper.IsNaNOrInfinity(input)) return input; - double tuf = tick.tick_unit_frac(); - double remainder = input % tuf; - if (remainder > 0) input += tuf - remainder; - double val = input - tuf * count; - return _round_tick(tuf, val); - } - - public static double tickdown(this RegionalTickFactors regional_tick, double input) => - regional_tick.tickdown(input, 1); - - public static double tickdown(this RegionalTickFactors regional_tick, double input, uint count) => - tickdown(regional_tick, input, count, TickUtils.precision_const); - - public static double tickdown(this RegionalTickFactors regional_tick, double input, uint count, double precision) { - if (!precision.is_real_positive()) - throw new ArgumentException(nameof(precision), "Must be a real positive number."); - if (DoubleHelper.IsNaNOrInfinity(input) || count == 0) return input; - double val, tuf; - if (input > regional_tick.low_tick_inclusive_end) { - tuf = regional_tick.high_tick.tick_unit_frac(); - double ieeer = Math.IEEERemainder(input, tuf); - input -= ieeer; - if (ieeer > precision) count--; - val = input - count * tuf; - if (val < regional_tick.low_tick_inclusive_end) { - count -= (uint)MathUtils.round_up((input - regional_tick.low_tick_inclusive_end) / tuf, precision); - tuf = regional_tick.low_tick.tick_unit_frac(); - val = regional_tick.low_tick_inclusive_end - tuf * count; - } - } else { - tuf = regional_tick.low_tick.tick_unit_frac(); - double ieeer = Math.IEEERemainder(input, tuf); - input -= ieeer; - if (ieeer > precision) count--; - val = input - tuf * count; - } - return _round_tick(tuf, val); - } - - public static double round(this TickFactors tick, double input) { return _round_tick(tick.tick_unit_frac(), input); } - public static double round(this RegionalTickFactors regional_tick, double input) { - if (DoubleHelper.IsNaNOrInfinity(input)) return input; - return input <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick.round(input) : regional_tick.high_tick.round(input); - } - public static int display_digits(this TickFactors tick) { - if (tick.tick_unit == tick.tick_base) { - return (int)Math.Floor(Math.Log10(tick.tick_base)); - } - return display_digits(tick.tick_base); - } - #endregion - - public static int display_digits(double tick_base) { - double log_tick_base = Math.Log10(tick_base); - int floor = (int)Math.Floor(log_tick_base); - double dec = log_tick_base - floor; - return Math.Max(1, dec > 0 ? ++floor : floor); - } - - #region Private Members - private static decimal tick_unit_frac_dec(this TickFactors tick) { return new decimal(tick.tick_unit / tick.tick_base); } - private static char separator(this TickFactors tick) { - double tb_log10 = Math.Log10(tick.tick_base); - return tb_log10 >= 1 && tb_log10 % 1 == 0 ? '.' : '-'; - } - - private static string _format_no_tick(double value, double display_multiplier, bool show_commas) { - if (!double.IsNaN(display_multiplier)) value *= display_multiplier; - return value.ToString(show_commas ? "#,0" : "0"); - } - - private static double _parse_no_tick(string text, double display_multiplier) { - double result; - if (!double.TryParse(text, out result)) return double.NaN; - return double.IsNaN(display_multiplier) || display_multiplier == 0.0 ? result : result / display_multiplier; - } - - private static double _parsed_or_nan(string text) { return double.TryParse(text, out double d) ? d : double.NaN; } - private static double _get_integer_part(string integer_text) { - return string.IsNullOrEmpty(integer_text) ? 0 : int.TryParse(integer_text, NumberStyles.Any, CultureInfo.CurrentCulture, out int result) ? result : double.NaN; - } - - private static double _get_numerator_part(string numerator_text, double tick_base, double tick_unit) { - if (string.IsNullOrEmpty(numerator_text)) return 0; - double numerator_part; - if (!double.TryParse(numerator_text, out numerator_part)) return double.NaN; - //e.g [tick_base 64] 1989-80 == 1989-8, or 1989-320 == 1989-32 - if (numerator_part >= tick_base) numerator_part /= Math.Pow(10, Math.Ceiling(Math.Log10(numerator_part) - Math.Log10(tick_base))); - else { - //e.g [tick_base 640] 1989-8 == 1989-80, or 1989-32 == 1989-320 - int ndd_diff = display_digits(tick_base) - numerator_text.Length; - if (ndd_diff > 0) numerator_part *= Math.Pow(10, ndd_diff); - if (numerator_part >= tick_base) numerator_part /= 10; - } - return numerator_part; - } - - private static double _parse(RegionalTickFactors regional_tick, string text, double display_multiplier, string nan_text) { - double result = _parse(regional_tick.low_tick, text, display_multiplier, nan_text); - double ltie = double.IsNaN(display_multiplier) ? regional_tick.low_tick_inclusive_end : regional_tick.low_tick_inclusive_end * display_multiplier; - if (result > ltie) result = _parse(regional_tick.high_tick, text, display_multiplier, nan_text); - return result; - } - - private static double _parse(TickFactors tick, string text, double display_multiplier, string nan_text) { - if (string.IsNullOrWhiteSpace(text) || text == nan_text) return double.NaN; - text = text.Replace(" ", string.Empty); - if (text[0] == '-') return text.Length > 1 ? -_parse(tick, text.Substring(1), display_multiplier, nan_text) : double.NaN; - double d; - if (double.TryParse(text, out d)) return double.IsNaN(display_multiplier) ? d : d * display_multiplier; - var parts = text.Split(tick.separator()); - double integer_part = _get_integer_part(parts[0]); - double result = parts.Length == 1 ? integer_part : _parse(tick, integer_part, parts[1]); - return double.IsNaN(display_multiplier) ? result : display_multiplier * result; - } - - private static double _parse(TickFactors tick, double integer_part, string numerator_part_text) { - double numerator_part = _get_numerator_part(numerator_part_text, tick.tick_base, tick.tick_unit); - return integer_part + numerator_part / tick.tick_base; - } - - private static string _format_tick(TickFactors tick, double value, TickDisplaySettings settings) { - if (!settings.display_multiplier.is_nan()) - value *= settings.display_multiplier; - // Hide the sign from the 'core_impl.' If the value is negative, - // flip it before calling core_impl, then prepend a '-' to the resultant string. - bool negate = value < 0; - if (negate) - value = -value; - var text = core_impl(); - if (!settings.show_zero_integer) { - // Hide the integer if its zero. This could be better optimized, - // but speed shouldn't be an issue, so it doesn't seem worth the time/effort. - var parts = text.Split(tick.separator()); - if (parts.Length > 1 && parts[0] == "0") - text = parts[1]; - } - if (negate) - text = "-" + text; - return text; - // Formats a 'normalized' value. This method expects 'value' to be positive, - // and [already] multiplied by the display multiplier (if non-nan). - string core_impl() { - try { - checked { - int integer_part = (int)Math.Floor(value); - double numerator_dec = value - integer_part; - double unit_frac = tick.tick_unit / tick.tick_base; - int n_units = (int)Math.Round(numerator_dec / unit_frac); - if (n_units == tick.tick_base) { - if (settings.show_decimal_part) { - return (integer_part + 1).ToString() + tick.separator() + new string('0', - settings.pad_with_leading_zeros ? display_digits(tick.tick_base) : 1); - } else { - return integer_part.ToString(); - } - } - double decimal_part = n_units * tick.tick_unit; - if (decimal_part == tick.tick_base) { - ++integer_part; - decimal_part = 0; - } else { - // some ticks are not formatted with 100% precision, using less than the required display_digits - // for such cases, we need to truncate the decimal part rather than round it. - decimal_part = Math.Floor(decimal_part); - } - string integer_part_text = settings.show_commas ? integer_part.ToString("#,0") : integer_part.ToString(); - if (settings.show_decimal_part) { - return integer_part_text + tick.separator() + decimal_part.ToString( - settings.pad_with_leading_zeros ? new string('0', display_digits(tick.tick_base)) : "0"); - } else { - return integer_part_text; - } - } - } - catch (OverflowException ex) { - Log.Ui.warn($"Arithmetic overflow exception in format_tick (tick:{tick}, value:{value}):{ex}"); - return "Overflow"; - } - } - } - - private static double _round_tick(double tick_unit_frac, double input) { - return input - Math.IEEERemainder(input, tick_unit_frac); - } - #endregion - } -} +using bts; +using bts.api; +using bts.utils; +using mbts.spark; +using mbts.user.settings; +using System; +using System.Globalization; +using static bts.utils.MathUtils; + +namespace mbts { + public static class TickExtensions { + public static string _nan_text => TickDisplaySettings.Default.nan_text; + + #region Public Extensions (is_valid, format, parse, tick_up, tick_down) + public static bool is_valid(this TickFactors tick) => + (tick.tick_unit > 0 && tick.tick_unit <= (tick.tick_base * 0.5)) || + tick.tick_unit == tick.tick_base; + + public static bool is_valid(this RegionalTickFactors regional_tick) => + regional_tick.low_tick.is_valid() && regional_tick.high_tick.is_valid(); + + public static bool is_integer(this TickFactors tick) => + tick.tick_unit == tick.tick_base; + + public static double tick_unit_frac(this TickFactors tick) => + tick.tick_unit / tick.tick_base; + + public static double reduced_base(this TickFactors tick) => factor_out(tick.tick_base, 10); + + public static string format_without_decimal_part(this TickFactors tick, double value) => + format(tick, value, double.NaN, false, _nan_text, true); + + public static string format(this TickFactors tick, double value) => + format(tick, value, double.NaN, false, _nan_text); + + public static string format(this TickFactors tick, double value, bool show_commas) => + format(tick, value, double.NaN, show_commas, _nan_text); + + public static string format(this TickFactors tick, double value, double display_multiplier) => + format(tick, value, display_multiplier, false, _nan_text); + + public static string format(this TickFactors tick, double value, double display_multiplier, bool show_commas) => + format(tick, value, display_multiplier, show_commas, _nan_text); + + public static string format(this TickFactors tick, double value, TickDisplaySettings settings) { + if (settings is null) + settings = TickDisplaySettings.Default; + if (double.IsNaN(value)) return settings.nan_text; + if (double.IsInfinity(value)) return value.ToString(); + if (!tick.is_valid()) return _format_no_tick(value, settings.display_multiplier, settings.show_commas); + return _format_tick(tick, value, settings); + } + + public static string format( + this TickFactors tick, + double value, + double display_multiplier, + bool show_commas, + string nan_text, + bool show_decimal_part = true) => + tick.format(value, TickDisplaySettings.Default.with( + display_multiplier: display_multiplier, + show_commas: show_commas, + nan_text: nan_text, + show_decimal_part: show_decimal_part)); + + public static string format_without_decimal_part(this RegionalTickFactors regional_tick, double value) => + format_without_decimal_part(value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick, value); + + public static string format(this RegionalTickFactors regional_tick, double value, TickDisplaySettings settings = null) => + format(value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick, value, settings); + + public static string format(this RegionalTickFactors regional_tick, double value, double display_multiplier) => + format(value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick, value, display_multiplier); + + public static string format(this RegionalTickFactors regional_tick, double value, double display_multiplier, bool show_commas, string nan_text) => + format(value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick, value, display_multiplier, show_commas, nan_text); + + public static double parse(this TickFactors tick, string text) => + parse(tick, text, double.NaN, _nan_text); + + public static double parse(this TickFactors tick, string text, double display_multiplier) => + parse(tick, text, display_multiplier, _nan_text); + + public static double parse(this TickFactors tick, string text, double display_multiplier, string nan_text) { + if (!tick.is_valid()) return _parse_no_tick(text, display_multiplier); + return _parse(tick, text, display_multiplier, nan_text); + } + + public static double parse(this RegionalTickFactors regional_tick, string text) => + parse(regional_tick, text, double.NaN, _nan_text); + + public static double parse(this RegionalTickFactors regional_tick, string text, double display_multiplier) => + parse(regional_tick, text, display_multiplier, _nan_text); + + public static double parse(this RegionalTickFactors regional_tick, string text, double display_multiplier, string nan_text) { + if (!regional_tick.is_valid()) return _parse_no_tick(text, display_multiplier); + return _parse(regional_tick, text, display_multiplier, nan_text); + } + + public static double tickup(this TickFactors tick, double input) { return tick.tickup(input, 1); } + public static double tickup(this TickFactors tick, double input, uint count) { + if (DoubleHelper.IsNaNOrInfinity(input)) return input; + double tuf = tick.tick_unit_frac(); + input -= input % tuf; + double val = input + tuf * count; + return _round_tick(tuf, val); + } + + public static double tickup(this RegionalTickFactors regional_tick, double input) => + regional_tick.tickup(input, 1); + + public static double tickup(this RegionalTickFactors regional_tick, double input, uint count) => + regional_tick.tickup(input, count, TickUtils.precision_const); + + public static double tickup(this RegionalTickFactors regional_tick, double input, uint count, double precision, bool bling = true) { + if (!precision.is_real_positive()) + throw new ArgumentException(nameof(precision), "Must be a real positive number."); + if (DoubleHelper.IsNaNOrInfinity(input) || count == 0) return input; + double val, tuf; + if (input < regional_tick.low_tick_inclusive_end) { + tuf = regional_tick.low_tick.tick_unit_frac(); + double ieeer = Math.IEEERemainder(input, tuf); + input -= ieeer; + if (ieeer < -precision) count--; + val = input + tuf * count; + if (val > regional_tick.low_tick_inclusive_end) { + count -= (uint)MathUtils.round_up((regional_tick.low_tick_inclusive_end - input) / tuf, precision); + tuf = regional_tick.high_tick.tick_unit_frac(); + val = regional_tick.low_tick_inclusive_end + tuf * count; + } + } else { + tuf = regional_tick.high_tick.tick_unit_frac(); + double ieeer = Math.IEEERemainder(input, tuf); + input -= ieeer; + if (ieeer < -precision) count--; + val = input + tuf * count; + } + return _round_tick(tuf, val); + } + + public static ContractSpecification to_spec(this TickFactors tick) { + if (tick == null) + return null; + double tick_frac = tick.tick_unit_frac(); + return new ContractSpecification { + LowTick = tick_frac, + HighTick = tick_frac, + PriceBreak = double.MaxValue, + MainFraction = (uint)factor_out(tick.tick_base, 10), + PriceDisplayFormat = (uint)tick.display_digits(), + PriceMultiplier = 1, + CashPerPoint = 1 + }; + } + + public static bool try_get_tickdiff(this RegionalTickFactors regional_tick, double x, double y, out int n_low, out int n_high) { + if (DoubleHelper.IsNaNOrInfinity(x) || DoubleHelper.IsNaNOrInfinity(y)) { + n_low = default; + n_high = default; + return false; + } + (n_low, n_high) = tickdiff_unchecked(regional_tick, x, y); + return true; + } + + /// + /// Callers should be certain that and are not nan or infinity. + /// + /// + /// + /// + internal static (int n_low, int n_high) tickdiff_unchecked(this RegionalTickFactors regional_tick, double x, double y) { + double ltie = regional_tick.low_tick_inclusive_end; + double raw_diff = x - y; + if (x < ltie) { + double low_unit_frac = regional_tick.low_tick.tick_unit_frac(); + if (y < ltie) { + return (RoundFractionalUnits(raw_diff, low_unit_frac), 0); + } else { + double high_unit_frac = regional_tick.high_tick.tick_unit_frac(); + double low_diff = ltie - x; + double high_diff = raw_diff - low_diff; + int n_low = RoundFractionalUnits(low_diff, low_unit_frac); + int n_high = RoundFractionalUnits(high_diff, high_unit_frac); + return (n_low, n_high); + } + } else { + double high_unit_frac = regional_tick.high_tick.tick_unit_frac(); + if (y < ltie) { + double low_unit_frac = regional_tick.low_tick.tick_unit_frac(); + double high_diff = x - ltie; + double low_diff = raw_diff - high_diff; + int n_low = RoundFractionalUnits(low_diff, low_unit_frac); + int n_high = RoundFractionalUnits(high_diff, high_unit_frac); + return (n_low, n_high); + } else { + return (0, RoundFractionalUnits(raw_diff, high_unit_frac)); + } + } + } + + static int _count_ticks(double tick_unit_frac, double value) => + (int)Math.Round(value / tick_unit_frac); + + public static double tickdown(this TickFactors tick, double input) { return tick.tickdown(input, 1); } + public static double tickdown(this TickFactors tick, double input, uint count) { + if (DoubleHelper.IsNaNOrInfinity(input)) return input; + double tuf = tick.tick_unit_frac(); + double remainder = input % tuf; + if (remainder > 0) input += tuf - remainder; + double val = input - tuf * count; + return _round_tick(tuf, val); + } + + public static double tickdown(this RegionalTickFactors regional_tick, double input) => + regional_tick.tickdown(input, 1); + + public static double tickdown(this RegionalTickFactors regional_tick, double input, uint count) => + tickdown(regional_tick, input, count, TickUtils.precision_const); + + public static double tickdown(this RegionalTickFactors regional_tick, double input, uint count, double precision) { + if (!precision.is_real_positive()) + throw new ArgumentException(nameof(precision), "Must be a real positive number."); + if (DoubleHelper.IsNaNOrInfinity(input) || count == 0) return input; + double val, tuf; + if (input > regional_tick.low_tick_inclusive_end) { + tuf = regional_tick.high_tick.tick_unit_frac(); + double ieeer = Math.IEEERemainder(input, tuf); + input -= ieeer; + if (ieeer > precision) count--; + val = input - count * tuf; + if (val < regional_tick.low_tick_inclusive_end) { + count -= (uint)MathUtils.round_up((input - regional_tick.low_tick_inclusive_end) / tuf, precision); + tuf = regional_tick.low_tick.tick_unit_frac(); + val = regional_tick.low_tick_inclusive_end - tuf * count; + } + } else { + tuf = regional_tick.low_tick.tick_unit_frac(); + double ieeer = Math.IEEERemainder(input, tuf); + input -= ieeer; + if (ieeer > precision) count--; + val = input - tuf * count; + } + return _round_tick(tuf, val); + } + + public static double round(this TickFactors tick, double input) { return _round_tick(tick.tick_unit_frac(), input); } + public static double round(this RegionalTickFactors regional_tick, double input) { + if (DoubleHelper.IsNaNOrInfinity(input)) return input; + return input <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick.round(input) : regional_tick.high_tick.round(input); + } + public static int display_digits(this TickFactors tick) { + if (tick.tick_unit == tick.tick_base) { + return (int)Math.Floor(Math.Log10(tick.tick_base)); + } + return display_digits(tick.tick_base); + } + #endregion + + public static int display_digits(double tick_base) { + double log_tick_base = Math.Log10(tick_base); + int floor = (int)Math.Floor(log_tick_base); + double dec = log_tick_base - floor; + return Math.Max(1, dec > 0 ? ++floor : floor); + } + + #region Private Members + private static decimal tick_unit_frac_dec(this TickFactors tick) { return new decimal(tick.tick_unit / tick.tick_base); } + private static char separator(this TickFactors tick) { + double tb_log10 = Math.Log10(tick.tick_base); + return tb_log10 >= 1 && tb_log10 % 1 == 0 ? '.' : '-'; + } + + private static string _format_no_tick(double value, double display_multiplier, bool show_commas) { + if (!double.IsNaN(display_multiplier)) value *= display_multiplier; + return value.ToString(show_commas ? "#,0" : "0"); + } + + private static double _parse_no_tick(string text, double display_multiplier) { + double result; + if (!double.TryParse(text, out result)) return double.NaN; + return double.IsNaN(display_multiplier) || display_multiplier == 0.0 ? result : result / display_multiplier; + } + + private static double _parsed_or_nan(string text) { return double.TryParse(text, out double d) ? d : double.NaN; } + private static double _get_integer_part(string integer_text) { + return string.IsNullOrEmpty(integer_text) ? 0 : int.TryParse(integer_text, NumberStyles.Any, CultureInfo.CurrentCulture, out int result) ? result : double.NaN; + } + + private static double _get_numerator_part(string numerator_text, double tick_base, double tick_unit) { + if (string.IsNullOrEmpty(numerator_text)) return 0; + double numerator_part; + if (!double.TryParse(numerator_text, out numerator_part)) return double.NaN; + //e.g [tick_base 64] 1989-80 == 1989-8, or 1989-320 == 1989-32 + if (numerator_part >= tick_base) numerator_part /= Math.Pow(10, Math.Ceiling(Math.Log10(numerator_part) - Math.Log10(tick_base))); + else { + //e.g [tick_base 640] 1989-8 == 1989-80, or 1989-32 == 1989-320 + int ndd_diff = display_digits(tick_base) - numerator_text.Length; + if (ndd_diff > 0) numerator_part *= Math.Pow(10, ndd_diff); + if (numerator_part >= tick_base) numerator_part /= 10; + } + return numerator_part; + } + + private static double _parse(RegionalTickFactors regional_tick, string text, double display_multiplier, string nan_text) { + double result = _parse(regional_tick.low_tick, text, display_multiplier, nan_text); + double ltie = double.IsNaN(display_multiplier) ? regional_tick.low_tick_inclusive_end : regional_tick.low_tick_inclusive_end * display_multiplier; + if (result > ltie) result = _parse(regional_tick.high_tick, text, display_multiplier, nan_text); + return result; + } + + private static double _parse(TickFactors tick, string text, double display_multiplier, string nan_text) { + if (string.IsNullOrWhiteSpace(text) || text == nan_text) return double.NaN; + text = text.Replace(" ", string.Empty); + if (text[0] == '-') return text.Length > 1 ? -_parse(tick, text.Substring(1), display_multiplier, nan_text) : double.NaN; + double d; + if (double.TryParse(text, out d)) return double.IsNaN(display_multiplier) ? d : d * display_multiplier; + var parts = text.Split(tick.separator()); + double integer_part = _get_integer_part(parts[0]); + double result = parts.Length == 1 ? integer_part : _parse(tick, integer_part, parts[1]); + return double.IsNaN(display_multiplier) ? result : display_multiplier * result; + } + + private static double _parse(TickFactors tick, double integer_part, string numerator_part_text) { + double numerator_part = _get_numerator_part(numerator_part_text, tick.tick_base, tick.tick_unit); + return integer_part + numerator_part / tick.tick_base; + } + + private static string _format_tick(TickFactors tick, double value, TickDisplaySettings settings) { + if (!settings.display_multiplier.is_nan()) + value *= settings.display_multiplier; + // Hide the sign from the 'core_impl.' If the value is negative, + // flip it before calling core_impl, then prepend a '-' to the resultant string. + bool negate = value < 0; + if (negate) + value = -value; + var text = core_impl(); + if (!settings.show_zero_integer) { + // Hide the integer if its zero. This could be better optimized, + // but speed shouldn't be an issue, so it doesn't seem worth the time/effort. + var parts = text.Split(tick.separator()); + if (parts.Length > 1 && parts[0] == "0") + text = parts[1]; + } + if (negate) + text = "-" + text; + return text; + // Formats a 'normalized' value. This method expects 'value' to be positive, + // and [already] multiplied by the display multiplier (if non-nan). + string core_impl() { + try { + checked { + int integer_part = (int)Math.Floor(value); + double numerator_dec = value - integer_part; + double unit_frac = tick.tick_unit / tick.tick_base; + int n_units = (int)Math.Round(numerator_dec / unit_frac); + if (n_units == tick.tick_base) { + if (settings.show_decimal_part) { + return (integer_part + 1).ToString() + tick.separator() + new string('0', + settings.pad_with_leading_zeros ? display_digits(tick.tick_base) : 1); + } else { + return integer_part.ToString(); + } + } + double decimal_part = n_units * tick.tick_unit; + if (decimal_part == tick.tick_base) { + ++integer_part; + decimal_part = 0; + } else { + // some ticks are not formatted with 100% precision, using less than the required display_digits + // for such cases, we need to truncate the decimal part rather than round it. + decimal_part = Math.Floor(decimal_part); + } + string integer_part_text = settings.show_commas ? integer_part.ToString("#,0") : integer_part.ToString(); + if (settings.show_decimal_part) { + return integer_part_text + tick.separator() + decimal_part.ToString( + settings.pad_with_leading_zeros ? new string('0', display_digits(tick.tick_base)) : "0"); + } else { + return integer_part_text; + } + } + } + catch (OverflowException ex) { + Log.Ui.warn($"Arithmetic overflow exception in format_tick (tick:{tick}, value:{value}):{ex}"); + return "Overflow"; + } + } + } + + private static double _round_tick(double tick_unit_frac, double input) { + return input - Math.IEEERemainder(input, tick_unit_frac); + } + #endregion + } +}