chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Text;
 
namespace OpenTap
{
    class Range : IEnumerable<BigFloat>
    {
        public readonly BigFloat Start;
        public readonly BigFloat Stop;
        public readonly BigFloat Step;
 
        public IEnumerator<BigFloat> GetEnumerator()
        {
            var start = Start;
            var stop = Stop;
            var step = Step;
 
            if (Start == Stop)
            { // A a range of 0 elements will be created otherwise. So return Start.
                yield return Start;
                yield break;
            }
            // This calculation is based on linear extrapolation that includes the end point 
            // on the condition that it's within double.epsilon
            var approximate_steps = ((stop - start) / step).Abs();
 
            // Cancel the last step if it's the calculated spot is more than double.epsilon further away.
            BigInteger steps = approximate_steps.Rounded();
            for (BigInteger i = 0; i <= steps; i++)
            {
                yield return new BigFloat(i) * step + start;
            }
        }
 
        public Range(BigFloat start, BigFloat stop)
        {
            Start = start;
            Stop = stop;
            Step = (Stop - Start).Sign();
            CheckRange();
        }
 
        public Range(BigFloat start, BigFloat stop, BigFloat step)
        {
            Start = start;
            Stop = stop;
            Step = step;
            CheckRange();
        }
 
        public void CheckRange()
        {
            if (Stop == Start) return; // Math.Sign(0) == 0.
            if (Step == 0 || (Stop - Start).Sign() != Step.Sign())
                throw new Exception("Infinite range not supported.");
        }
 
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
 
        public override string ToString()
        {
            return ToString(null);
        }
 
        public string ToString(Func<object, string> conv)
        {
            conv = conv ?? ((v) => v.ToString());
 
            if (Step == new BigFloat(1.0) || Step == new BigFloat(-1.0))
            {
                return string.Format("{0} : {1}", conv(Start), conv(Stop));
            }
            return string.Format("{0} : {1} : {2}", conv(Start), conv(Step), conv(Stop));
        }
    }
 
    class UnitFormatter
    {
        static BigFloat peta = 1000000000000000L;
        static BigFloat tera = 1000000000000L;
        static BigFloat giga = 1000000000L;
        static BigFloat mega = 1000000;
        static BigFloat kilo = 1000;
        static BigFloat one = 1;
        static BigFloat mili = kilo.Invert();
        static BigFloat micro = mega.Invert();
        static BigFloat nano = giga.Invert();
        static BigFloat pico = tera.Invert();
        static BigFloat femto = peta.Invert();
 
 
        static BigFloat engineeringPrefixLevel(char l)
        {
            switch (l)
            {
                case 'T': return tera;
                case 'G': return giga;
                case 'M': return mega;
                case 'k': return kilo;
                case ' ': return one;
                case 'm': return mili;
                case 'u': return micro;
                case 'n': return nano;
                case 'p': return pico;
                case 'f': return femto;
                default: throw new Exception("Invalid engineering prefix");
            }
        }
        static char[] levels = { 'T', 'G', 'M', 'k', ' ', 'm', 'u', 'n', 'p', 'f' };
 
        static char findLevel(BigFloat value)
        {
            foreach (char level in levels)
            {
                var eng = engineeringPrefixLevel(level);
                if (value >= eng || value <= -eng)
                {
                    return level;
                }
            }
            return ' ';
        }
 
        public static void Format(StringBuilder sb, BigFloat value, bool prefix, string unit, string format, CultureInfo culture,
            bool compact = false)
        {
            char level = prefix ? findLevel(value) : ' ';
            BigFloat scaling = engineeringPrefixLevel(level);
            BigFloat post_scale = value / scaling;
 
            if (string.IsNullOrEmpty(format))
                post_scale.AppendTo(sb, culture);
            else
            {
                if (format.StartsWith("x", StringComparison.OrdinalIgnoreCase))
                    sb.Append(((long)post_scale.Rounded()).ToString(format, culture));
                else if (format.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
                    sb.Append("0x" + ((long)post_scale.Rounded()).ToString(format.Substring(1), culture));
                else
                {
                    try
                    {
                        sb.Append(((decimal)post_scale.ConvertTo(typeof(decimal))).ToString(format, culture));
                    }
                    catch
                    {
                        post_scale.AppendTo(sb, culture);
                    }
                }
            }
 
            if (level == ' ' && string.IsNullOrEmpty(unit))
            {
                return;
            }
 
            string space = compact ? "" : " ";
            sb.Append(space);
            if (level != ' ')
                sb.Append(level);
            if(unit != null)
                sb.Append(unit);
        }
 
        public static string Format(BigFloat value, bool prefix, string unit, string format, CultureInfo culture, bool compact = false)
        {
            char level = prefix ? findLevel(value) : ' ';
            BigFloat scaling = engineeringPrefixLevel(level);
            BigFloat post_scale = value / scaling;
            string final_string;
 
            if (string.IsNullOrEmpty(format))
                final_string = post_scale.ToString("R", culture);
            else
            {
                if (format.StartsWith("x", StringComparison.OrdinalIgnoreCase))
                    final_string = ((long)post_scale.Rounded()).ToString(format, culture);
                else if (format.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
                    final_string = "0x" + ((long)post_scale.Rounded()).ToString(format.Substring(1), culture);
                else
                {
                    try
                    {
                        final_string = ((decimal)post_scale.ConvertTo(typeof(decimal))).ToString(format, culture);
                    }
                    catch
                    {
                        final_string = post_scale.ToString(format, culture);
                    }
                }
            }
 
            if (level == ' ' && string.IsNullOrEmpty(unit))
            {
                return final_string;
            }
 
            string space = compact ? "" : " ";
 
            if (level == ' ')
                return string.Format("{0}{2}{1}", final_string, unit, space);
            else
                return string.Format("{0}{3}{1}{2}", final_string, level, unit ?? "", space);
        }
        static object parse(string str, string unit, string format, CultureInfo culture)
        {
            if (str == null)
                return new ArgumentNullException("str");
            if (unit == null)
                return new ArgumentNullException("unit");
            if (format == null)
                return new ArgumentNullException("format");
            if (culture == null)
                return new ArgumentNullException("culture");
 
            str = str.Trim();
 
            bool IsHex =
                (str.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ||
                str.StartsWith("-0x", StringComparison.OrdinalIgnoreCase));
            int HexSkip = 2;
 
            if ((!IsHex) &&
               (format.StartsWith("x", StringComparison.OrdinalIgnoreCase)))
            {
                IsHex = true;
                HexSkip = 0;
            }
 
            if (unit.Length > 0 && str.EndsWith(unit, StringComparison.OrdinalIgnoreCase))
                str = str.Remove(str.Length - unit.Length);
            str = str.TrimEnd();
            if (str.Length == 0)
            {
                return new FormatException("Invalid format");
            }
 
            char prefix = str[str.Length - 1];
            BigFloat multiplier = 1.0;
            if (levels.Contains(prefix))
            {
                // Handle case of "femto" unit
                if (!(IsHex && (prefix == 'f') && !str.EndsWith(" f", StringComparison.Ordinal)))
                {
                    multiplier = engineeringPrefixLevel(prefix);
                    if (str[str.Length - 1] == prefix)
                        str = str.Substring(0, str.Length - 1).TrimEnd();
                }
            }
 
            if (IsHex && str.StartsWith("-", StringComparison.OrdinalIgnoreCase))
                return new BigFloat(-Int64.Parse(str.Substring(HexSkip + 1).ToUpper(), NumberStyles.HexNumber, culture)) * multiplier;
            else if (IsHex)
                return new BigFloat(BigInteger.Parse("0" + str.Substring(HexSkip).ToUpper(), NumberStyles.HexNumber, culture)) * multiplier;
 
            if (str.StartsWith("0b", StringComparison.OrdinalIgnoreCase) || str.StartsWith("-0b", StringComparison.OrdinalIgnoreCase))
            {
                string bits = "";
                if (str.StartsWith("-0b", StringComparison.OrdinalIgnoreCase))
                {
                    multiplier = -multiplier;
                    bits = str.Substring(2);
                }
                else
                {
                    bits = str.Substring(2);
                }
 
                BigInteger v = 0;
                foreach (var bit in bits)
                {
                    if (bit == '1')
                    {
                        v = v << 1 | 1;
                    }
                    else if (bit == '0')
                    {
                        v = v << 1;
                    }
                    else if (char.IsWhiteSpace(bit))
                        continue;
                    else
                    {
                        return new FormatException("Invalid binary format.");
                    }
                }
                return new BigFloat(v) * multiplier;
            }
            var r = BigFloat.Parse(str, culture);
            if (r is BigFloat bf)
                return bf * multiplier;
            return r;
        }
        public static BigFloat Parse(string str, string unit, string format, CultureInfo culture)
        {
            var result = parse(str, unit, format, culture);
            if (result is BigFloat bf)
                return bf;
            else throw (Exception)result;
        }
 
        public static bool TryParse(string str, string unit, string format, CultureInfo culture, out BigFloat bf)
        {
            var result = parse(str, unit, format, culture);
            if (result is BigFloat _bf)
            {
                bf = _bf;
                return true;
            }
            bf = default(BigFloat);
 
            return false;
 
        }
    }
 
    /// <summary>
    /// A number of combined number sequences.
    /// </summary>
    public interface ICombinedNumberSequence : IEnumerable
    {
        /// <summary>
        /// Inner values representing the sequence.
        /// </summary>
        List<IEnumerable<double>> Sequences { get; }
 
        /// <summary>
        /// Casts the number sequence to a specific type. Should be a numeric type, e.g. typeof(float).
        /// </summary>
        /// <param name="elementType"></param>
        /// <returns></returns>
        ICombinedNumberSequence CastTo(Type elementType);
    }
 
    /// <summary>
    /// Generic number sequence type.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface ICombinedNumberSequence<T> : IEnumerable<T>, ICombinedNumberSequence
    {
        /// <summary>
        /// Casts this to a new number sequence type.
        /// </summary>
        /// <typeparam name="T2"></typeparam>
        /// <returns></returns>
        ICombinedNumberSequence<T2> CastTo<T2>();
    }
 
    /// <summary>
    /// Extensions for ICombinedNumberSequence.
    /// </summary>
    public static class ICombinedNumberSequenceExtension
    {
        /// <summary> Casts one type ICombinedNumberSequence to another. </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="seq"></param>
        /// <returns></returns>
        public static ICombinedNumberSequence<T> CastTo<T>(this ICombinedNumberSequence seq)
        {
            return (ICombinedNumberSequence<T>)seq.CastTo(typeof(T));
        }
    }
 
    interface _ICombinedNumberSequence
    {
        List<IEnumerable<BigFloat>> Sequences { get; set; }
    }
 
    class CombinedNumberSequences<T> : IEnumerable<T>, ICombinedNumberSequence<T>, _ICombinedNumberSequence
    {
        public List<IEnumerable<BigFloat>> Sequences { get; set; }
 
        List<IEnumerable<double>> ICombinedNumberSequence.Sequences { get {return Sequences.Select(x => x.Select(y => (double)y.ConvertTo(typeof(double)))).ToList(); } }
 
        public CombinedNumberSequences(List<IEnumerable<BigFloat>> sequences)
        {
            this.Sequences = sequences;
        }
 
        IEnumerable<T> getValues()
        {
            foreach (var seq in Sequences)
            {
                foreach (BigFloat val in seq)
                {
                    yield return (T)val.ConvertTo(typeof(T));
                }
            }
        }
 
        public IEnumerator<T> GetEnumerator()
        {
            return getValues().GetEnumerator();
        }
 
        IEnumerator IEnumerable.GetEnumerator()
        {
            return getValues().GetEnumerator();
        }
 
        public ICombinedNumberSequence<T1> CastTo<T1>()
        {
            return new CombinedNumberSequences<T1>(Sequences);
        }
 
        public ICombinedNumberSequence CastTo(Type elemType)
        {
            return (ICombinedNumberSequence)Activator.CreateInstance(typeof(CombinedNumberSequences<>).MakeGenericType(elemType), Sequences);
        }
    }
 
    /// <summary>
    /// Parser / back parser for numbers and sequences of numbers.
    /// </summary>
    public class NumberFormatter
    {
        CultureInfo culture;
        string separator;
        /// <summary>
        /// Argument to string.Format.
        /// </summary>
        public string Format = "";
        /// <summary>
        /// Unit of the numbers (e.g. 'Hz').
        /// </summary>
        public string Unit = "";
        /// <summary>
        /// Boolean setting. When true, parse number into prefixes. For example, '10000 Hz' becomes '10 kHz'. 
        /// </summary>
        public bool UsePrefix = false;
        /// <summary>
        /// Pre-scales numbers before converting.
        /// </summary>
        public double PreScaling = 1.0;
        /// <summary>
        /// Boolean setting. When true, numbers are parsed back into ranges. When false, separate values as used as their raw representation. 
        /// </summary>
        public bool UseRanges = true;
 
        /// <summary>
        /// Print using compact representation.
        /// </summary>
        public bool IsCompact = false;
 
        /// <summary> </summary>
        /// <param name="culture"> The culture used to parse/write numbers.</param>
        public NumberFormatter(CultureInfo culture)
        {
            if (culture == null)
                throw new ArgumentNullException("culture");
            this.culture = culture;
            separator = culture.NumberFormat.NumberGroupSeparator + " ";
        }
 
        /// <summary>
        /// Creates a number parser based on a UnitAttribute.
        /// </summary>
        /// <param name="culture"></param>
        /// <param name="unit"></param>
        public NumberFormatter(CultureInfo culture, UnitAttribute unit) : this(culture)
        {
            if (unit != null)
            {
                Format = unit.StringFormat;
                Unit = unit.Unit;
                UsePrefix = unit.UseEngineeringPrefix;
                PreScaling = unit.PreScaling;
                UseRanges = unit.UseRanges;
            }
        }
 
        const bool enableFeatureFractions = false;
        BigFloat parseNumber(string trimmed)
        {
            // support fractions e.g 1/3 disabled because its hard to convert back from 0.333333... to 1/3, so this gives problems with formatting.
            if (enableFeatureFractions && trimmed.Contains('/'))
                return trimmed.Split('/').Select(part => parseNumber(part.Trim())).Aggregate((x, y) => x * PreScaling / y);
            return UnitFormatter.Parse(trimmed, Unit ?? "", Format, culture) * PreScaling;
        }
 
        bool tryParseNumber(string trimmed, out BigFloat val)
        {
            return UnitFormatter.TryParse(trimmed, Unit ?? "", Format, culture, out val);
        }
        
        void parseBackNumber(BigFloat number, StringBuilder sb)
        {
            if (PreScaling != 1.0)
                number = number / PreScaling;
            UnitFormatter.Format(sb, number , UsePrefix, Unit ?? "", Format, culture, IsCompact);
        }
 
        string parseBackNumber(BigFloat number)
        {
            return UnitFormatter.Format(number / PreScaling, UsePrefix, Unit ?? "", Format, culture, IsCompact);
        }
 
        void parseBackRange(BigFloat Start, BigFloat Step, BigFloat Stop, StringBuilder sb)
        {
            parseBackNumber(Start, sb);
            if (Step != PreScaling)
            {
                sb.Append(" : ");
                parseBackNumber(Step, sb);
            }
            sb.Append(" : ");
            parseBackNumber(Stop, sb);
        }
        
        string parseBackRange(Range rng)
        {
            if (rng.Step == PreScaling)
            {
                return string.Format("{0} : {1}", parseBackNumber(rng.Start), parseBackNumber(rng.Stop));
            }
            return string.Format("{0} : {1} : {2}", parseBackNumber(rng.Start), parseBackNumber(rng.Step), parseBackNumber(rng.Stop));
        }
 
        Range parseRange(string formatted)
        {
            var parts = formatted.Split(':').Select(s => s.Trim());
            var rangeitems = parts.Select(parseNumber).ToArray();
            Range result = null;
            if (rangeitems.Length == 3)
            {
                result = new Range(rangeitems[0], rangeitems[2], rangeitems[1]);
            }
            else
            if (rangeitems.Length == 2)
            {
                result = new Range(rangeitems[0], rangeitems[1], PreScaling * (rangeitems[1] - rangeitems[0]).Sign());
            }
            else
            {
                throw new FormatException(string.Format("Unable to parse Range from {0}", formatted));
            }
            result.CheckRange();
            return result;
        }
 
        /// <summary>
        /// Parses a string to a sequence of doubles.
        /// supports ranges, sequences, units and prefixes.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public ICombinedNumberSequence<double> Parse(string value)
        {
            if (value == null)
                throw new ArgumentNullException("value");
            string separator = culture.NumberFormat.NumberGroupSeparator;
            var splits = value.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries);
            List<IEnumerable<BigFloat>> parts = new List<IEnumerable<BigFloat>>();
            foreach (var split in splits)
            {
                var trimmed = split.Trim();
                if (trimmed.Contains(':'))
                {
                    Range rng = parseRange(trimmed);
                    parts.Add(rng);
                }
                else
                {
                    var result = parseNumber(trimmed);
                    if (parts.Count == 0 || parts[parts.Count - 1] is Range)
                    {
                        parts.Add(new List<BigFloat> { result });
                    }
                    else
                    {
                        ((IList<BigFloat>)parts[parts.Count - 1]).Add(result);
                    }
                }
            }
            List<IEnumerable<BigFloat>> parts2 = new List<IEnumerable<BigFloat>>();
 
            if (UseRanges)
            {
                // this for loop 'compresses' the parts by trying to reuse and create Range objects.
                for (int i = 0; i < parts.Count; i++)
                {
                    var item = parts[i];
                    if (item is Range)
                    {
                        parts2.Add(item);
                        continue;
                    }
                    var vals = item as IList<BigFloat>;
                    bool reuse_last = false;
                    BigFloat start = 0, stop = 0, step = 0;
                    int nitems = 0;
                    var last = parts2.LastOrDefault() as Range;
                    if (last != null)
                    {
                        start = last.Start;
                        stop = last.Stop;
                        step = last.Step;
                        nitems = (int)((((stop - start) / step) + 0.5).Rounded() + 1);
                        reuse_last = true;
                    }
 
                    Action submit = () =>
                    {
                        if (nitems == 0)
                            return;
                        if (reuse_last)
                        {
                            parts2[parts2.Count - 1] = new Range(start, stop, step);
                        }
                        else
                        {
                            if (nitems > 2)
                            {
                                if (step == 0)
                                {
                                    BigFloat[] values = new BigFloat[nitems];
                                    for (int j = 0; j < nitems; j++)
                                        values[j] = start;
                                    parts2.Add(values);
                                }
                                else
                                {
                                    parts2.Add(new Range(start, stop, step));
                                }
                            }
                            else if (nitems == 2)
                            {
                                parts2.Add(new BigFloat[] { start, stop });
                            }
                            else
                            {
                                parts2.Add(new BigFloat[] { start });
                            }
                        }
                        nitems = 0;
                        step = 0;
                    };
 
                    foreach (var val in vals)
                    {
                        start:
                        if (nitems == 0)
                        {
                            start = val;
                            nitems = 1;
                            continue;
                        }
                        if (nitems == 1)
                        {
                            stop = val;
                            step = stop - start;
                            nitems = 2;
                            continue;
                        }
 
                        BigFloat nextval;
                        if (step == 0)
                            nextval = stop; // Avoid NaN nextval.
                        else
                            nextval = start + (step * (1 + ((stop - start) / step).Round()));
 
                        if ((val - nextval) == 0)
                        {
                            stop = val;
                            nitems += 1;
                        }
                        else
                        {
                            submit();
                            reuse_last = false;
                            goto start; // run again with val as start.
                        }
                    }
                    submit();
 
                }
            }
            else
                parts2 = parts.Select(x => x.ToList() as IEnumerable<BigFloat>).ToList();
            return new CombinedNumberSequences<BigFloat>(parts2).CastTo<double>();
 
        }
 
        void pushSeq(StringBuilder sb, BigFloat val)
        {
            if (sb.Length != 0)
                sb.Append(separator);
            parseBackNumber(val, sb);
        }
        
        void pushSeq(StringBuilder sb, IList<BigFloat> seq, BigFloat step)
        {
            if (seq.Count > 2 && step.IsZero == false)
            {
                if (sb.Length != 0)
                    sb.Append(separator);
                parseBackRange(seq[0], step, seq[seq.Count - 1], sb);
            }
            else
            {
                foreach (var val in seq)
                {
                    if (sb.Length != 0)
                        sb.Append(separator);
                    parseBackNumber(val, sb);
                }
            }
        }
 
        /// <summary>
        /// Parses a number back to a string.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public string FormatNumber(object value)
        {
            if (value == null)
                return "";
            return parseBackNumber(BigFloat.Convert(value));
        }
 
        /// <summary>
        /// Parses a single number from a string.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public object ParseNumber(string str, Type t)
        {
            if (str == null) throw new ArgumentNullException("str");
            if (t == null) throw new ArgumentNullException("t");
            try
            {
                
                return parseNumber(str).ConvertTo(t);
            }
            catch (OverflowException)
            {
                throw new FormatException(string.Format("Unable to parse '{0}' to a {1}", str, t.Name));
            }
        }
 
        /// <summary>
        /// Try to parse a single number from a string.
        /// </summary>
        /// <param name="str">the string to parse.</param>
        /// <param name="t">the return type of value. must be numeric. </param>
        /// <param name="val">resulting value. Null if parsing failed.</param>
        /// <returns></returns>
        public bool TryParseNumber(string str, Type t, out object val)
        {
            if(tryParseNumber(str, out BigFloat val2))
            {
                val = val2.ConvertTo(t);
                return true;
            }
            val = null;
            return false;
        }
 
        /// <summary>
        /// Parses a sequence of numbers back into a string.
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public string FormatRange(IEnumerable values)
        {
            if (values == null)
                throw new ArgumentNullException("values");
            { // Check if values is a ICombinedNumberSequence. 
              // If so, it can be parsed back faster. (without iterating through all the ranges).
                var seqs = values as _ICombinedNumberSequence;
                if (seqs != null)
                {
                    StringBuilder sb = StringBuilderCache.GetStringBuilder();
                    foreach (var subseq in seqs.Sequences)
                    {
                        var range = subseq as Range;
                        if (range != null)
                        {
                            if (sb.Length != 0)
                                sb.Append(separator);
                            sb.Append(parseBackRange(range));
                        }
                        else
                        {
                            foreach (var val in subseq)
                            {
                                if (sb.Length != 0)
                                    sb.Append(separator);
                                sb.Append(parseBackNumber(val));
                            }
                        }
                    }
                    return sb.ToString();
                }
            }
            if (UseRanges)
            { // Parse the slow way.
                StringBuilder sb = StringBuilderCache.GetStringBuilder();
                List<BigFloat> sequence = new List<BigFloat>();
                BigFloat seq_step = 0;
                foreach (var _val in values)
                {
                    var val = BigFloat.Convert(_val);
                    if (sequence.Count < 2)
                    {
                        sequence.Add(val);
                    }
                    else
                    {
                        seq_step = sequence[1] - sequence[0];
                        var nextVal = sequence[0] + seq_step * (1 + ((sequence.Last() - sequence[0]) / seq_step).Round());
                        if ((nextVal - val) == BigFloat.Zero)
                        {
                            sequence.Add(val);
                        }
                        else
                        {
                            if (sequence.Count == 2)
                            {
                                pushSeq(sb, sequence[0]);
                                sequence.RemoveAt(0);
                            }
                            else
                            {
                                pushSeq(sb, sequence, seq_step);
                                sequence.Clear();
                            }
 
                            sequence.Add(val);
                        }
                    }
                }
                pushSeq(sb, sequence, seq_step);
                return sb.ToString();
            }
            if (!UsePrefix)
            {
                // this ca be done really fast since we dont have to use BigFloat.
                var sb = StringBuilderCache.GetStringBuilder();
                foreach (var _val in values)
                {
                    if (sb.Length != 0)
                        sb.Append(separator);
                    switch (_val)
                    {
                        case float i:
                            sb.Append(i.ToString("R", culture));
                            break;
                        case decimal i: 
                            sb.Append(i.ToString("G", culture));
                            break;
                        case double i: 
                            sb.Append(i.ToString("R17", culture));
                            break;
                        default:
                            sb.Append(_val);
                            break;
                    }
 
                    if (string.IsNullOrEmpty(Unit) == false)
                    {
                        sb.Append(" ");
                        sb.Append(Unit);
                    }
                }
 
                return sb.ToString();
            }
            
            {
                StringBuilder sb = StringBuilderCache.GetStringBuilder();
                foreach (var _val in values)
                {
                    var val = BigFloat.Convert(_val);
 
                    if (sb.Length != 0)
                        sb.Append(separator);
                    parseBackNumber(val, sb);
                }
                return sb.ToString();
            }
        }
    }
}