yzt
2023-05-26 de4278af2fd46705a40bac58ec01122db6b7f3d7
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
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
/* This file is automatically rebuilt by the Cesium build process. */
define(['exports', './RuntimeError-4fdc4459', './when-8166c7dd', './WebGLConstants-0664004c'], (function (exports, RuntimeError, when, WebGLConstants) { 'use strict';
 
    /* This file is automatically rebuilt by the Cesium build process. */
    /*
      https://github.com/banksean wrapped Makoto Matsumoto and Takuji Nishimura's code in a namespace
      so it's better encapsulated. Now you can have multiple random number generators
      and they won't stomp all over eachother's state.
 
      If you want to use this as a substitute for Math.random(), use the random()
      method like so:
 
      var m = new MersenneTwister();
      var randomNumber = m.random();
 
      You can also call the other genrand_{foo}() methods on the instance.
 
      If you want to use a specific seed in order to get a repeatable random
      sequence, pass an integer into the constructor:
 
      var m = new MersenneTwister(123);
 
      and that will always produce the same random sequence.
 
      Sean McCullough (banksean@gmail.com)
    */
 
    /*
       A C-program for MT19937, with initialization improved 2002/1/26.
       Coded by Takuji Nishimura and Makoto Matsumoto.
 
       Before using, initialize the state by using init_seed(seed)
       or init_by_array(init_key, key_length).
 
       Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
       All rights reserved.
 
       Redistribution and use in source and binary forms, with or without
       modification, are permitted provided that the following conditions
       are met:
 
         1. Redistributions of source code must retain the above copyright
            notice, this list of conditions and the following disclaimer.
 
         2. Redistributions in binary form must reproduce the above copyright
            notice, this list of conditions and the following disclaimer in the
            documentation and/or other materials provided with the distribution.
 
         3. The names of its contributors may not be used to endorse or promote
            products derived from this software without specific prior written
            permission.
 
       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
       CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
       LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
       NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
       SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 
       Any feedback is very welcome.
       http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
       email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
    */
 
    var MersenneTwister = function(seed) {
        if (seed == undefined) {
            seed = new Date().getTime();
        }
 
        /* Period parameters */
        this.N = 624;
        this.M = 397;
        this.MATRIX_A = 0x9908b0df;   /* constant vector a */
        this.UPPER_MASK = 0x80000000; /* most significant w-r bits */
        this.LOWER_MASK = 0x7fffffff; /* least significant r bits */
 
        this.mt = new Array(this.N); /* the array for the state vector */
        this.mti=this.N+1; /* mti==N+1 means mt[N] is not initialized */
 
        if (seed.constructor == Array) {
            this.init_by_array(seed, seed.length);
        }
        else {
            this.init_seed(seed);
        }
    };
 
    /* initializes mt[N] with a seed */
    /* origin name init_genrand */
    MersenneTwister.prototype.init_seed = function(s) {
        this.mt[0] = s >>> 0;
        for (this.mti=1; this.mti<this.N; this.mti++) {
            var s = this.mt[this.mti-1] ^ (this.mt[this.mti-1] >>> 30);
            this.mt[this.mti] = (((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253)
            + this.mti;
            /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
            /* In the previous versions, MSBs of the seed affect   */
            /* only MSBs of the array mt[].                        */
            /* 2002/01/09 modified by Makoto Matsumoto             */
            this.mt[this.mti] >>>= 0;
            /* for >32 bit machines */
        }
    };
 
    /* initialize by an array with array-length */
    /* init_key is the array for initializing keys */
    /* key_length is its length */
    /* slight change for C++, 2004/2/26 */
    MersenneTwister.prototype.init_by_array = function(init_key, key_length) {
        var i, j, k;
        this.init_seed(19650218);
        i=1; j=0;
        k = (this.N>key_length ? this.N : key_length);
        for (; k; k--) {
            var s = this.mt[i-1] ^ (this.mt[i-1] >>> 30);
            this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1664525) << 16) + ((s & 0x0000ffff) * 1664525)))
            + init_key[j] + j; /* non linear */
            this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */
            i++; j++;
            if (i>=this.N) { this.mt[0] = this.mt[this.N-1]; i=1; }
            if (j>=key_length) j=0;
        }
        for (k=this.N-1; k; k--) {
            var s = this.mt[i-1] ^ (this.mt[i-1] >>> 30);
            this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1566083941) << 16) + (s & 0x0000ffff) * 1566083941))
            - i; /* non linear */
            this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */
            i++;
            if (i>=this.N) { this.mt[0] = this.mt[this.N-1]; i=1; }
        }
 
        this.mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */
    };
 
    /* generates a random number on [0,0xffffffff]-interval */
    /* origin name genrand_int32 */
    MersenneTwister.prototype.random_int = function() {
        var y;
        var mag01 = new Array(0x0, this.MATRIX_A);
        /* mag01[x] = x * MATRIX_A  for x=0,1 */
 
        if (this.mti >= this.N) { /* generate N words at one time */
            var kk;
 
            if (this.mti == this.N+1)  /* if init_seed() has not been called, */
                this.init_seed(5489);  /* a default initial seed is used */
 
            for (kk=0;kk<this.N-this.M;kk++) {
                y = (this.mt[kk]&this.UPPER_MASK)|(this.mt[kk+1]&this.LOWER_MASK);
                this.mt[kk] = this.mt[kk+this.M] ^ (y >>> 1) ^ mag01[y & 0x1];
            }
            for (;kk<this.N-1;kk++) {
                y = (this.mt[kk]&this.UPPER_MASK)|(this.mt[kk+1]&this.LOWER_MASK);
                this.mt[kk] = this.mt[kk+(this.M-this.N)] ^ (y >>> 1) ^ mag01[y & 0x1];
            }
            y = (this.mt[this.N-1]&this.UPPER_MASK)|(this.mt[0]&this.LOWER_MASK);
            this.mt[this.N-1] = this.mt[this.M-1] ^ (y >>> 1) ^ mag01[y & 0x1];
 
            this.mti = 0;
        }
 
        y = this.mt[this.mti++];
 
        /* Tempering */
        y ^= (y >>> 11);
        y ^= (y << 7) & 0x9d2c5680;
        y ^= (y << 15) & 0xefc60000;
        y ^= (y >>> 18);
 
        return y >>> 0;
    };
 
    /* generates a random number on [0,0x7fffffff]-interval */
    /* origin name genrand_int31 */
    MersenneTwister.prototype.random_int31 = function() {
        return (this.random_int()>>>1);
    };
 
    /* generates a random number on [0,1]-real-interval */
    /* origin name genrand_real1 */
    MersenneTwister.prototype.random_incl = function() {
        return this.random_int()*(1.0/4294967295.0);
        /* divided by 2^32-1 */
    };
 
    /* generates a random number on [0,1)-real-interval */
    MersenneTwister.prototype.random = function() {
        return this.random_int()*(1.0/4294967296.0);
        /* divided by 2^32 */
    };
 
    /* generates a random number on (0,1)-real-interval */
    /* origin name genrand_real3 */
    MersenneTwister.prototype.random_excl = function() {
        return (this.random_int() + 0.5)*(1.0/4294967296.0);
        /* divided by 2^32 */
    };
 
    /* generates a random number on [0,1) with 53-bit resolution*/
    /* origin name genrand_res53 */
    MersenneTwister.prototype.random_long = function() {
        var a=this.random_int()>>>5, b=this.random_int()>>>6;
        return (a*67108864.0+b)*(1.0/9007199254740992.0);
    };
 
    /* These real versions are due to Isaku Wada, 2002/01/09 added */
 
    var mersenneTwister = MersenneTwister;
 
    /**
     * Math functions.
     *
     * @exports CesiumMath
     * @alias Math
     */
    var CesiumMath = {};
 
    /**
     * 0.1
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON1 = 0.1;
 
    /**
     * 0.01
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON2 = 0.01;
 
    /**
     * 0.001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON3 = 0.001;
 
    /**
     * 0.0001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON4 = 0.0001;
 
    /**
     * 0.00001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON5 = 0.00001;
 
    /**
     * 0.000001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON6 = 0.000001;
 
    /**
     * 0.0000001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON7 = 0.0000001;
 
    /**
     * 0.00000001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON8 = 0.00000001;
 
    /**
     * 0.000000001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON9 = 0.000000001;
 
    /**
     * 0.0000000001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON10 = 0.0000000001;
 
    /**
     * 0.00000000001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON11 = 0.00000000001;
 
    /**
     * 0.000000000001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON12 = 0.000000000001;
 
    /**
     * 0.0000000000001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON13 = 0.0000000000001;
 
    /**
     * 0.00000000000001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON14 = 0.00000000000001;
 
    /**
     * 0.000000000000001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON15 = 0.000000000000001;
 
    /**
     * 0.0000000000000001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON16 = 0.0000000000000001;
 
    /**
     * 0.00000000000000001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON17 = 0.00000000000000001;
 
    /**
     * 0.000000000000000001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON18 = 0.000000000000000001;
 
    /**
     * 0.0000000000000000001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON19 = 0.0000000000000000001;
 
    /**
     * 0.00000000000000000001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON20 = 0.00000000000000000001;
 
    /**
     * 0.000000000000000000001
     * @type {Number}
     * @constant
     */
    CesiumMath.EPSILON21 = 0.000000000000000000001;
 
    /**
     * The gravitational parameter of the Earth in meters cubed
     * per second squared as defined by the WGS84 model: 3.986004418e14
     * @type {Number}
     * @constant
     */
    CesiumMath.GRAVITATIONALPARAMETER = 3.986004418e14;
 
    /**
     * Radius of the sun in meters: 6.955e8
     * @type {Number}
     * @constant
     */
    CesiumMath.SOLAR_RADIUS = 6.955e8;
 
    /**
     * The mean radius of the moon, according to the "Report of the IAU/IAG Working Group on
     * Cartographic Coordinates and Rotational Elements of the Planets and satellites: 2000",
     * Celestial Mechanics 82: 83-110, 2002.
     * @type {Number}
     * @constant
     */
    CesiumMath.LUNAR_RADIUS = 1737400.0;
 
    /**
     * 64 * 1024
     * @type {Number}
     * @constant
     */
    CesiumMath.SIXTY_FOUR_KILOBYTES = 64 * 1024;
 
    /**
     * 4 * 1024 * 1024 * 1024
     * @type {Number}
     * @constant
     */
    CesiumMath.FOUR_GIGABYTES = 4 * 1024 * 1024 * 1024;
 
    /**
     * Returns the sign of the value; 1 if the value is positive, -1 if the value is
     * negative, or 0 if the value is 0.
     *
     * @function
     * @param {Number} value The value to return the sign of.
     * @returns {Number} The sign of value.
     */
    // eslint-disable-next-line es/no-math-sign
    CesiumMath.sign = when.defaultValue(Math.sign, function sign(value) {
      value = +value; // coerce to number
      if (value === 0 || value !== value) {
        // zero or NaN
        return value;
      }
      return value > 0 ? 1 : -1;
    });
 
    /**
     * Returns 1.0 if the given value is positive or zero, and -1.0 if it is negative.
     * This is similar to {@link CesiumMath#sign} except that returns 1.0 instead of
     * 0.0 when the input value is 0.0.
     * @param {Number} value The value to return the sign of.
     * @returns {Number} The sign of value.
     */
    CesiumMath.signNotZero = function (value) {
      return value < 0.0 ? -1.0 : 1.0;
    };
 
    /**
     * Converts a scalar value in the range [-1.0, 1.0] to a SNORM in the range [0, rangeMaximum]
     * @param {Number} value The scalar value in the range [-1.0, 1.0]
     * @param {Number} [rangeMaximum=255] The maximum value in the mapped range, 255 by default.
     * @returns {Number} A SNORM value, where 0 maps to -1.0 and rangeMaximum maps to 1.0.
     *
     * @see CesiumMath.fromSNorm
     */
    CesiumMath.toSNorm = function (value, rangeMaximum) {
      rangeMaximum = when.defaultValue(rangeMaximum, 255);
      return Math.round(
        (CesiumMath.clamp(value, -1.0, 1.0) * 0.5 + 0.5) * rangeMaximum
      );
    };
 
    /**
     * Converts a SNORM value in the range [0, rangeMaximum] to a scalar in the range [-1.0, 1.0].
     * @param {Number} value SNORM value in the range [0, rangeMaximum]
     * @param {Number} [rangeMaximum=255] The maximum value in the SNORM range, 255 by default.
     * @returns {Number} Scalar in the range [-1.0, 1.0].
     *
     * @see CesiumMath.toSNorm
     */
    CesiumMath.fromSNorm = function (value, rangeMaximum) {
      rangeMaximum = when.defaultValue(rangeMaximum, 255);
      return (
        (CesiumMath.clamp(value, 0.0, rangeMaximum) / rangeMaximum) * 2.0 - 1.0
      );
    };
 
    /**
     * Converts a scalar value in the range [rangeMinimum, rangeMaximum] to a scalar in the range [0.0, 1.0]
     * @param {Number} value The scalar value in the range [rangeMinimum, rangeMaximum]
     * @param {Number} rangeMinimum The minimum value in the mapped range.
     * @param {Number} rangeMaximum The maximum value in the mapped range.
     * @returns {Number} A scalar value, where rangeMinimum maps to 0.0 and rangeMaximum maps to 1.0.
     */
    CesiumMath.normalize = function (value, rangeMinimum, rangeMaximum) {
      rangeMaximum = Math.max(rangeMaximum - rangeMinimum, 0.0);
      return rangeMaximum === 0.0
        ? 0.0
        : CesiumMath.clamp((value - rangeMinimum) / rangeMaximum, 0.0, 1.0);
    };
 
    /**
     * Returns the hyperbolic sine of a number.
     * The hyperbolic sine of <em>value</em> is defined to be
     * (<em>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></em>)/2.0
     * where <i>e</i> is Euler's number, approximately 2.71828183.
     *
     * <p>Special cases:
     *   <ul>
     *     <li>If the argument is NaN, then the result is NaN.</li>
     *
     *     <li>If the argument is infinite, then the result is an infinity
     *     with the same sign as the argument.</li>
     *
     *     <li>If the argument is zero, then the result is a zero with the
     *     same sign as the argument.</li>
     *   </ul>
     *</p>
     *
     * @function
     * @param {Number} value The number whose hyperbolic sine is to be returned.
     * @returns {Number} The hyperbolic sine of <code>value</code>.
     */
    // eslint-disable-next-line es/no-math-sinh
    CesiumMath.sinh = when.defaultValue(Math.sinh, function sinh(value) {
      return (Math.exp(value) - Math.exp(-value)) / 2.0;
    });
 
    /**
     * Returns the hyperbolic cosine of a number.
     * The hyperbolic cosine of <strong>value</strong> is defined to be
     * (<em>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></em>)/2.0
     * where <i>e</i> is Euler's number, approximately 2.71828183.
     *
     * <p>Special cases:
     *   <ul>
     *     <li>If the argument is NaN, then the result is NaN.</li>
     *
     *     <li>If the argument is infinite, then the result is positive infinity.</li>
     *
     *     <li>If the argument is zero, then the result is 1.0.</li>
     *   </ul>
     *</p>
     *
     * @function
     * @param {Number} value The number whose hyperbolic cosine is to be returned.
     * @returns {Number} The hyperbolic cosine of <code>value</code>.
     */
    // eslint-disable-next-line es/no-math-cosh
    CesiumMath.cosh = when.defaultValue(Math.cosh, function cosh(value) {
      return (Math.exp(value) + Math.exp(-value)) / 2.0;
    });
 
    /**
     * Computes the linear interpolation of two values.
     *
     * @param {Number} p The start value to interpolate.
     * @param {Number} q The end value to interpolate.
     * @param {Number} time The time of interpolation generally in the range <code>[0.0, 1.0]</code>.
     * @returns {Number} The linearly interpolated value.
     *
     * @example
     * var n = Cesium.Math.lerp(0.0, 2.0, 0.5); // returns 1.0
     */
    CesiumMath.lerp = function (p, q, time) {
      return (1.0 - time) * p + time * q;
    };
 
    /**
     * pi
     *
     * @type {Number}
     * @constant
     */
    CesiumMath.PI = Math.PI;
 
    /**
     * 1/pi
     *
     * @type {Number}
     * @constant
     */
    CesiumMath.ONE_OVER_PI = 1.0 / Math.PI;
 
    /**
     * pi/2
     *
     * @type {Number}
     * @constant
     */
    CesiumMath.PI_OVER_TWO = Math.PI / 2.0;
 
    /**
     * pi/3
     *
     * @type {Number}
     * @constant
     */
    CesiumMath.PI_OVER_THREE = Math.PI / 3.0;
 
    /**
     * pi/4
     *
     * @type {Number}
     * @constant
     */
    CesiumMath.PI_OVER_FOUR = Math.PI / 4.0;
 
    /**
     * pi/6
     *
     * @type {Number}
     * @constant
     */
    CesiumMath.PI_OVER_SIX = Math.PI / 6.0;
 
    /**
     * 3pi/2
     *
     * @type {Number}
     * @constant
     */
    CesiumMath.THREE_PI_OVER_TWO = (3.0 * Math.PI) / 2.0;
 
    /**
     * 2pi
     *
     * @type {Number}
     * @constant
     */
    CesiumMath.TWO_PI = 2.0 * Math.PI;
 
    /**
     * 1/2pi
     *
     * @type {Number}
     * @constant
     */
    CesiumMath.ONE_OVER_TWO_PI = 1.0 / (2.0 * Math.PI);
 
    /**
     * The number of radians in a degree.
     *
     * @type {Number}
     * @constant
     */
    CesiumMath.RADIANS_PER_DEGREE = Math.PI / 180.0;
 
    /**
     * The number of degrees in a radian.
     *
     * @type {Number}
     * @constant
     */
    CesiumMath.DEGREES_PER_RADIAN = 180.0 / Math.PI;
 
    /**
     * The number of radians in an arc second.
     *
     * @type {Number}
     * @constant
     */
    CesiumMath.RADIANS_PER_ARCSECOND = CesiumMath.RADIANS_PER_DEGREE / 3600.0;
 
    /**
     * Converts degrees to radians.
     * @param {Number} degrees The angle to convert in degrees.
     * @returns {Number} The corresponding angle in radians.
     */
    CesiumMath.toRadians = function (degrees) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(degrees)) {
        throw new RuntimeError.DeveloperError("degrees is required.");
      }
      //>>includeEnd('debug');
      return degrees * CesiumMath.RADIANS_PER_DEGREE;
    };
 
    /**
     * Converts radians to degrees.
     * @param {Number} radians The angle to convert in radians.
     * @returns {Number} The corresponding angle in degrees.
     */
    CesiumMath.toDegrees = function (radians) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(radians)) {
        throw new RuntimeError.DeveloperError("radians is required.");
      }
      //>>includeEnd('debug');
      return radians * CesiumMath.DEGREES_PER_RADIAN;
    };
 
    /**
     * Converts a longitude value, in radians, to the range [<code>-Math.PI</code>, <code>Math.PI</code>).
     *
     * @param {Number} angle The longitude value, in radians, to convert to the range [<code>-Math.PI</code>, <code>Math.PI</code>).
     * @returns {Number} The equivalent longitude value in the range [<code>-Math.PI</code>, <code>Math.PI</code>).
     *
     * @example
     * // Convert 270 degrees to -90 degrees longitude
     * var longitude = Cesium.Math.convertLongitudeRange(Cesium.Math.toRadians(270.0));
     */
    CesiumMath.convertLongitudeRange = function (angle) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(angle)) {
        throw new RuntimeError.DeveloperError("angle is required.");
      }
      //>>includeEnd('debug');
      var twoPi = CesiumMath.TWO_PI;
 
      var simplified = angle - Math.floor(angle / twoPi) * twoPi;
 
      if (simplified < -Math.PI) {
        return simplified + twoPi;
      }
      if (simplified >= Math.PI) {
        return simplified - twoPi;
      }
 
      return simplified;
    };
 
    /**
     * Convenience function that clamps a latitude value, in radians, to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
     * Useful for sanitizing data before use in objects requiring correct range.
     *
     * @param {Number} angle The latitude value, in radians, to clamp to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
     * @returns {Number} The latitude value clamped to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
     *
     * @example
     * // Clamp 108 degrees latitude to 90 degrees latitude
     * var latitude = Cesium.Math.clampToLatitudeRange(Cesium.Math.toRadians(108.0));
     */
    CesiumMath.clampToLatitudeRange = function (angle) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(angle)) {
        throw new RuntimeError.DeveloperError("angle is required.");
      }
      //>>includeEnd('debug');
 
      return CesiumMath.clamp(
        angle,
        -1 * CesiumMath.PI_OVER_TWO,
        CesiumMath.PI_OVER_TWO
      );
    };
 
    /**
     * Produces an angle in the range -Pi <= angle <= Pi which is equivalent to the provided angle.
     *
     * @param {Number} angle in radians
     * @returns {Number} The angle in the range [<code>-CesiumMath.PI</code>, <code>CesiumMath.PI</code>].
     */
    CesiumMath.negativePiToPi = function (angle) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(angle)) {
        throw new RuntimeError.DeveloperError("angle is required.");
      }
      //>>includeEnd('debug');
      if (angle >= -CesiumMath.PI && angle <= CesiumMath.PI) {
        // Early exit if the input is already inside the range. This avoids
        // unnecessary math which could introduce floating point error.
        return angle;
      }
      return CesiumMath.zeroToTwoPi(angle + CesiumMath.PI) - CesiumMath.PI;
    };
 
    /**
     * Produces an angle in the range 0 <= angle <= 2Pi which is equivalent to the provided angle.
     *
     * @param {Number} angle in radians
     * @returns {Number} The angle in the range [0, <code>CesiumMath.TWO_PI</code>].
     */
    CesiumMath.zeroToTwoPi = function (angle) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(angle)) {
        throw new RuntimeError.DeveloperError("angle is required.");
      }
      //>>includeEnd('debug');
      if (angle >= 0 && angle <= CesiumMath.TWO_PI) {
        // Early exit if the input is already inside the range. This avoids
        // unnecessary math which could introduce floating point error.
        return angle;
      }
      var mod = CesiumMath.mod(angle, CesiumMath.TWO_PI);
      if (
        Math.abs(mod) < CesiumMath.EPSILON14 &&
        Math.abs(angle) > CesiumMath.EPSILON14
      ) {
        return CesiumMath.TWO_PI;
      }
      return mod;
    };
 
    /**
     * The modulo operation that also works for negative dividends.
     *
     * @param {Number} m The dividend.
     * @param {Number} n The divisor.
     * @returns {Number} The remainder.
     */
    CesiumMath.mod = function (m, n) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(m)) {
        throw new RuntimeError.DeveloperError("m is required.");
      }
      if (!when.defined(n)) {
        throw new RuntimeError.DeveloperError("n is required.");
      }
      if (n === 0.0) {
        throw new RuntimeError.DeveloperError("divisor cannot be 0.");
      }
      //>>includeEnd('debug');
      if (CesiumMath.sign(m) === CesiumMath.sign(n) && Math.abs(m) < Math.abs(n)) {
        // Early exit if the input does not need to be modded. This avoids
        // unnecessary math which could introduce floating point error.
        return m;
      }
 
      return ((m % n) + n) % n;
    };
 
    /**
     * Determines if two values are equal using an absolute or relative tolerance test. This is useful
     * to avoid problems due to roundoff error when comparing floating-point values directly. The values are
     * first compared using an absolute tolerance test. If that fails, a relative tolerance test is performed.
     * Use this test if you are unsure of the magnitudes of left and right.
     *
     * @param {Number} left The first value to compare.
     * @param {Number} right The other value to compare.
     * @param {Number} [relativeEpsilon=0] The maximum inclusive delta between <code>left</code> and <code>right</code> for the relative tolerance test.
     * @param {Number} [absoluteEpsilon=relativeEpsilon] The maximum inclusive delta between <code>left</code> and <code>right</code> for the absolute tolerance test.
     * @returns {Boolean} <code>true</code> if the values are equal within the epsilon; otherwise, <code>false</code>.
     *
     * @example
     * var a = Cesium.Math.equalsEpsilon(0.0, 0.01, Cesium.Math.EPSILON2); // true
     * var b = Cesium.Math.equalsEpsilon(0.0, 0.1, Cesium.Math.EPSILON2);  // false
     * var c = Cesium.Math.equalsEpsilon(3699175.1634344, 3699175.2, Cesium.Math.EPSILON7); // true
     * var d = Cesium.Math.equalsEpsilon(3699175.1634344, 3699175.2, Cesium.Math.EPSILON9); // false
     */
    CesiumMath.equalsEpsilon = function (
      left,
      right,
      relativeEpsilon,
      absoluteEpsilon
    ) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(left)) {
        throw new RuntimeError.DeveloperError("left is required.");
      }
      if (!when.defined(right)) {
        throw new RuntimeError.DeveloperError("right is required.");
      }
      //>>includeEnd('debug');
 
      relativeEpsilon = when.defaultValue(relativeEpsilon, 0.0);
      absoluteEpsilon = when.defaultValue(absoluteEpsilon, relativeEpsilon);
      var absDiff = Math.abs(left - right);
      return (
        absDiff <= absoluteEpsilon ||
        absDiff <= relativeEpsilon * Math.max(Math.abs(left), Math.abs(right))
      );
    };
 
    /**
     * Determines if the left value is less than the right value. If the two values are within
     * <code>absoluteEpsilon</code> of each other, they are considered equal and this function returns false.
     *
     * @param {Number} left The first number to compare.
     * @param {Number} right The second number to compare.
     * @param {Number} absoluteEpsilon The absolute epsilon to use in comparison.
     * @returns {Boolean} <code>true</code> if <code>left</code> is less than <code>right</code> by more than
     *          <code>absoluteEpsilon<code>. <code>false</code> if <code>left</code> is greater or if the two
     *          values are nearly equal.
     */
    CesiumMath.lessThan = function (left, right, absoluteEpsilon) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(left)) {
        throw new RuntimeError.DeveloperError("first is required.");
      }
      if (!when.defined(right)) {
        throw new RuntimeError.DeveloperError("second is required.");
      }
      if (!when.defined(absoluteEpsilon)) {
        throw new RuntimeError.DeveloperError("absoluteEpsilon is required.");
      }
      //>>includeEnd('debug');
      return left - right < -absoluteEpsilon;
    };
 
    /**
     * Determines if the left value is less than or equal to the right value. If the two values are within
     * <code>absoluteEpsilon</code> of each other, they are considered equal and this function returns true.
     *
     * @param {Number} left The first number to compare.
     * @param {Number} right The second number to compare.
     * @param {Number} absoluteEpsilon The absolute epsilon to use in comparison.
     * @returns {Boolean} <code>true</code> if <code>left</code> is less than <code>right</code> or if the
     *          the values are nearly equal.
     */
    CesiumMath.lessThanOrEquals = function (left, right, absoluteEpsilon) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(left)) {
        throw new RuntimeError.DeveloperError("first is required.");
      }
      if (!when.defined(right)) {
        throw new RuntimeError.DeveloperError("second is required.");
      }
      if (!when.defined(absoluteEpsilon)) {
        throw new RuntimeError.DeveloperError("absoluteEpsilon is required.");
      }
      //>>includeEnd('debug');
      return left - right < absoluteEpsilon;
    };
 
    /**
     * Determines if the left value is greater the right value. If the two values are within
     * <code>absoluteEpsilon</code> of each other, they are considered equal and this function returns false.
     *
     * @param {Number} left The first number to compare.
     * @param {Number} right The second number to compare.
     * @param {Number} absoluteEpsilon The absolute epsilon to use in comparison.
     * @returns {Boolean} <code>true</code> if <code>left</code> is greater than <code>right</code> by more than
     *          <code>absoluteEpsilon<code>. <code>false</code> if <code>left</code> is less or if the two
     *          values are nearly equal.
     */
    CesiumMath.greaterThan = function (left, right, absoluteEpsilon) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(left)) {
        throw new RuntimeError.DeveloperError("first is required.");
      }
      if (!when.defined(right)) {
        throw new RuntimeError.DeveloperError("second is required.");
      }
      if (!when.defined(absoluteEpsilon)) {
        throw new RuntimeError.DeveloperError("absoluteEpsilon is required.");
      }
      //>>includeEnd('debug');
      return left - right > absoluteEpsilon;
    };
 
    /**
     * Determines if the left value is greater than or equal to the right value. If the two values are within
     * <code>absoluteEpsilon</code> of each other, they are considered equal and this function returns true.
     *
     * @param {Number} left The first number to compare.
     * @param {Number} right The second number to compare.
     * @param {Number} absoluteEpsilon The absolute epsilon to use in comparison.
     * @returns {Boolean} <code>true</code> if <code>left</code> is greater than <code>right</code> or if the
     *          the values are nearly equal.
     */
    CesiumMath.greaterThanOrEquals = function (left, right, absoluteEpsilon) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(left)) {
        throw new RuntimeError.DeveloperError("first is required.");
      }
      if (!when.defined(right)) {
        throw new RuntimeError.DeveloperError("second is required.");
      }
      if (!when.defined(absoluteEpsilon)) {
        throw new RuntimeError.DeveloperError("absoluteEpsilon is required.");
      }
      //>>includeEnd('debug');
      return left - right > -absoluteEpsilon;
    };
 
    var factorials = [1];
 
    /**
     * Computes the factorial of the provided number.
     *
     * @param {Number} n The number whose factorial is to be computed.
     * @returns {Number} The factorial of the provided number or undefined if the number is less than 0.
     *
     * @exception {DeveloperError} A number greater than or equal to 0 is required.
     *
     *
     * @example
     * //Compute 7!, which is equal to 5040
     * var computedFactorial = Cesium.Math.factorial(7);
     *
     * @see {@link http://en.wikipedia.org/wiki/Factorial|Factorial on Wikipedia}
     */
    CesiumMath.factorial = function (n) {
      //>>includeStart('debug', pragmas.debug);
      if (typeof n !== "number" || n < 0) {
        throw new RuntimeError.DeveloperError(
          "A number greater than or equal to 0 is required."
        );
      }
      //>>includeEnd('debug');
 
      var length = factorials.length;
      if (n >= length) {
        var sum = factorials[length - 1];
        for (var i = length; i <= n; i++) {
          var next = sum * i;
          factorials.push(next);
          sum = next;
        }
      }
      return factorials[n];
    };
 
    /**
     * Increments a number with a wrapping to a minimum value if the number exceeds the maximum value.
     *
     * @param {Number} [n] The number to be incremented.
     * @param {Number} [maximumValue] The maximum incremented value before rolling over to the minimum value.
     * @param {Number} [minimumValue=0.0] The number reset to after the maximum value has been exceeded.
     * @returns {Number} The incremented number.
     *
     * @exception {DeveloperError} Maximum value must be greater than minimum value.
     *
     * @example
     * var n = Cesium.Math.incrementWrap(5, 10, 0); // returns 6
     * var n = Cesium.Math.incrementWrap(10, 10, 0); // returns 0
     */
    CesiumMath.incrementWrap = function (n, maximumValue, minimumValue) {
      minimumValue = when.defaultValue(minimumValue, 0.0);
 
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(n)) {
        throw new RuntimeError.DeveloperError("n is required.");
      }
      if (maximumValue <= minimumValue) {
        throw new RuntimeError.DeveloperError("maximumValue must be greater than minimumValue.");
      }
      //>>includeEnd('debug');
 
      ++n;
      if (n > maximumValue) {
        n = minimumValue;
      }
      return n;
    };
 
    /**
     * Determines if a non-negative integer is a power of two.
     * The maximum allowed input is (2^32)-1 due to 32-bit bitwise operator limitation in Javascript.
     *
     * @param {Number} n The integer to test in the range [0, (2^32)-1].
     * @returns {Boolean} <code>true</code> if the number if a power of two; otherwise, <code>false</code>.
     *
     * @exception {DeveloperError} A number between 0 and (2^32)-1 is required.
     *
     * @example
     * var t = Cesium.Math.isPowerOfTwo(16); // true
     * var f = Cesium.Math.isPowerOfTwo(20); // false
     */
    CesiumMath.isPowerOfTwo = function (n) {
      //>>includeStart('debug', pragmas.debug);
      if (typeof n !== "number" || n < 0 || n > 4294967295) {
        throw new RuntimeError.DeveloperError("A number between 0 and (2^32)-1 is required.");
      }
      //>>includeEnd('debug');
 
      return n !== 0 && (n & (n - 1)) === 0;
    };
 
    /**
     * Computes the next power-of-two integer greater than or equal to the provided non-negative integer.
     * The maximum allowed input is 2^31 due to 32-bit bitwise operator limitation in Javascript.
     *
     * @param {Number} n The integer to test in the range [0, 2^31].
     * @returns {Number} The next power-of-two integer.
     *
     * @exception {DeveloperError} A number between 0 and 2^31 is required.
     *
     * @example
     * var n = Cesium.Math.nextPowerOfTwo(29); // 32
     * var m = Cesium.Math.nextPowerOfTwo(32); // 32
     */
    CesiumMath.nextPowerOfTwo = function (n) {
      //>>includeStart('debug', pragmas.debug);
      if (typeof n !== "number" || n < 0 || n > 2147483648) {
        throw new RuntimeError.DeveloperError("A number between 0 and 2^31 is required.");
      }
      //>>includeEnd('debug');
 
      // From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
      --n;
      n |= n >> 1;
      n |= n >> 2;
      n |= n >> 4;
      n |= n >> 8;
      n |= n >> 16;
      ++n;
 
      return n;
    };
 
    /**
     * Computes the previous power-of-two integer less than or equal to the provided non-negative integer.
     * The maximum allowed input is (2^32)-1 due to 32-bit bitwise operator limitation in Javascript.
     *
     * @param {Number} n The integer to test in the range [0, (2^32)-1].
     * @returns {Number} The previous power-of-two integer.
     *
     * @exception {DeveloperError} A number between 0 and (2^32)-1 is required.
     *
     * @example
     * var n = Cesium.Math.previousPowerOfTwo(29); // 16
     * var m = Cesium.Math.previousPowerOfTwo(32); // 32
     */
    CesiumMath.previousPowerOfTwo = function (n) {
      //>>includeStart('debug', pragmas.debug);
      if (typeof n !== "number" || n < 0 || n > 4294967295) {
        throw new RuntimeError.DeveloperError("A number between 0 and (2^32)-1 is required.");
      }
      //>>includeEnd('debug');
 
      n |= n >> 1;
      n |= n >> 2;
      n |= n >> 4;
      n |= n >> 8;
      n |= n >> 16;
      n |= n >> 32;
 
      // The previous bitwise operations implicitly convert to signed 32-bit. Use `>>>` to convert to unsigned
      n = (n >>> 0) - (n >>> 1);
 
      return n;
    };
 
    /**
     * Constraint a value to lie between two values.
     *
     * @param {Number} value The value to constrain.
     * @param {Number} min The minimum value.
     * @param {Number} max The maximum value.
     * @returns {Number} The value clamped so that min <= value <= max.
     */
    CesiumMath.clamp = function (value, min, max) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(value)) {
        throw new RuntimeError.DeveloperError("value is required");
      }
      if (!when.defined(min)) {
        throw new RuntimeError.DeveloperError("min is required.");
      }
      if (!when.defined(max)) {
        throw new RuntimeError.DeveloperError("max is required.");
      }
      //>>includeEnd('debug');
      return value < min ? min : value > max ? max : value;
    };
 
    var randomNumberGenerator = new mersenneTwister();
 
    /**
     * Sets the seed used by the random number generator
     * in {@link CesiumMath#nextRandomNumber}.
     *
     * @param {Number} seed An integer used as the seed.
     */
    CesiumMath.setRandomNumberSeed = function (seed) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(seed)) {
        throw new RuntimeError.DeveloperError("seed is required.");
      }
      //>>includeEnd('debug');
 
      randomNumberGenerator = new mersenneTwister(seed);
    };
 
    /**
     * Generates a random floating point number in the range of [0.0, 1.0)
     * using a Mersenne twister.
     *
     * @returns {Number} A random number in the range of [0.0, 1.0).
     *
     * @see CesiumMath.setRandomNumberSeed
     * @see {@link http://en.wikipedia.org/wiki/Mersenne_twister|Mersenne twister on Wikipedia}
     */
    CesiumMath.nextRandomNumber = function () {
      return randomNumberGenerator.random();
    };
 
    /**
     * Generates a random number between two numbers.
     *
     * @param {Number} min The minimum value.
     * @param {Number} max The maximum value.
     * @returns {Number} A random number between the min and max.
     */
    CesiumMath.randomBetween = function (min, max) {
      return CesiumMath.nextRandomNumber() * (max - min) + min;
    };
 
    /**
     * Computes <code>Math.acos(value)</code>, but first clamps <code>value</code> to the range [-1.0, 1.0]
     * so that the function will never return NaN.
     *
     * @param {Number} value The value for which to compute acos.
     * @returns {Number} The acos of the value if the value is in the range [-1.0, 1.0], or the acos of -1.0 or 1.0,
     *          whichever is closer, if the value is outside the range.
     */
    CesiumMath.acosClamped = function (value) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(value)) {
        throw new RuntimeError.DeveloperError("value is required.");
      }
      //>>includeEnd('debug');
      return Math.acos(CesiumMath.clamp(value, -1.0, 1.0));
    };
 
    /**
     * Computes <code>Math.asin(value)</code>, but first clamps <code>value</code> to the range [-1.0, 1.0]
     * so that the function will never return NaN.
     *
     * @param {Number} value The value for which to compute asin.
     * @returns {Number} The asin of the value if the value is in the range [-1.0, 1.0], or the asin of -1.0 or 1.0,
     *          whichever is closer, if the value is outside the range.
     */
    CesiumMath.asinClamped = function (value) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(value)) {
        throw new RuntimeError.DeveloperError("value is required.");
      }
      //>>includeEnd('debug');
      return Math.asin(CesiumMath.clamp(value, -1.0, 1.0));
    };
 
    /**
     * Finds the chord length between two points given the circle's radius and the angle between the points.
     *
     * @param {Number} angle The angle between the two points.
     * @param {Number} radius The radius of the circle.
     * @returns {Number} The chord length.
     */
    CesiumMath.chordLength = function (angle, radius) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(angle)) {
        throw new RuntimeError.DeveloperError("angle is required.");
      }
      if (!when.defined(radius)) {
        throw new RuntimeError.DeveloperError("radius is required.");
      }
      //>>includeEnd('debug');
      return 2.0 * radius * Math.sin(angle * 0.5);
    };
 
    /**
     * Finds the logarithm of a number to a base.
     *
     * @param {Number} number The number.
     * @param {Number} base The base.
     * @returns {Number} The result.
     */
    CesiumMath.logBase = function (number, base) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(number)) {
        throw new RuntimeError.DeveloperError("number is required.");
      }
      if (!when.defined(base)) {
        throw new RuntimeError.DeveloperError("base is required.");
      }
      //>>includeEnd('debug');
      return Math.log(number) / Math.log(base);
    };
 
    /**
     * Finds the cube root of a number.
     * Returns NaN if <code>number</code> is not provided.
     *
     * @function
     * @param {Number} [number] The number.
     * @returns {Number} The result.
     */
    // eslint-disable-next-line es/no-math-cbrt
    CesiumMath.cbrt = when.defaultValue(Math.cbrt, function cbrt(number) {
      var result = Math.pow(Math.abs(number), 1.0 / 3.0);
      return number < 0.0 ? -result : result;
    });
 
    /**
     * Finds the base 2 logarithm of a number.
     *
     * @function
     * @param {Number} number The number.
     * @returns {Number} The result.
     */
    // eslint-disable-next-line es/no-math-log2
    CesiumMath.log2 = when.defaultValue(Math.log2, function log2(number) {
      return Math.log(number) * Math.LOG2E;
    });
 
    /**
     * @private
     */
    CesiumMath.fog = function (distanceToCamera, density) {
      var scalar = distanceToCamera * density;
      return 1.0 - Math.exp(-(scalar * scalar));
    };
 
    /**
     * Computes a fast approximation of Atan for input in the range [-1, 1].
     *
     * Based on Michal Drobot's approximation from ShaderFastLibs,
     * which in turn is based on "Efficient approximations for the arctangent function,"
     * Rajan, S. Sichun Wang Inkol, R. Joyal, A., May 2006.
     * Adapted from ShaderFastLibs under MIT License.
     *
     * @param {Number} x An input number in the range [-1, 1]
     * @returns {Number} An approximation of atan(x)
     */
    CesiumMath.fastApproximateAtan = function (x) {
      //>>includeStart('debug', pragmas.debug);
      RuntimeError.Check.typeOf.number("x", x);
      //>>includeEnd('debug');
 
      return x * (-0.1784 * Math.abs(x) - 0.0663 * x * x + 1.0301);
    };
 
    /**
     * Computes a fast approximation of Atan2(x, y) for arbitrary input scalars.
     *
     * Range reduction math based on nvidia's cg reference implementation: http://developer.download.nvidia.com/cg/atan2.html
     *
     * @param {Number} x An input number that isn't zero if y is zero.
     * @param {Number} y An input number that isn't zero if x is zero.
     * @returns {Number} An approximation of atan2(x, y)
     */
    CesiumMath.fastApproximateAtan2 = function (x, y) {
      //>>includeStart('debug', pragmas.debug);
      RuntimeError.Check.typeOf.number("x", x);
      RuntimeError.Check.typeOf.number("y", y);
      //>>includeEnd('debug');
 
      // atan approximations are usually only reliable over [-1, 1]
      // So reduce the range by flipping whether x or y is on top based on which is bigger.
      var opposite;
      var adjacent;
      var t = Math.abs(x); // t used as swap and atan result.
      opposite = Math.abs(y);
      adjacent = Math.max(t, opposite);
      opposite = Math.min(t, opposite);
 
      var oppositeOverAdjacent = opposite / adjacent;
      //>>includeStart('debug', pragmas.debug);
      if (isNaN(oppositeOverAdjacent)) {
        throw new RuntimeError.DeveloperError("either x or y must be nonzero");
      }
      //>>includeEnd('debug');
      t = CesiumMath.fastApproximateAtan(oppositeOverAdjacent);
 
      // Undo range reduction
      t = Math.abs(y) > Math.abs(x) ? CesiumMath.PI_OVER_TWO - t : t;
      t = x < 0.0 ? CesiumMath.PI - t : t;
      t = y < 0.0 ? -t : t;
      return t;
    };
 
    /**
     * WebGL component datatypes.  Components are intrinsics,
     * which form attributes, which form vertices.
     *
     * @enum {Number}
     */
    var ComponentDatatype = {
      /**
       * 8-bit signed byte corresponding to <code>gl.BYTE</code> and the type
       * of an element in <code>Int8Array</code>.
       *
       * @type {Number}
       * @constant
       */
      BYTE: WebGLConstants.WebGLConstants.BYTE,
 
      /**
       * 8-bit unsigned byte corresponding to <code>UNSIGNED_BYTE</code> and the type
       * of an element in <code>Uint8Array</code>.
       *
       * @type {Number}
       * @constant
       */
      UNSIGNED_BYTE: WebGLConstants.WebGLConstants.UNSIGNED_BYTE,
 
      /**
       * 16-bit signed short corresponding to <code>SHORT</code> and the type
       * of an element in <code>Int16Array</code>.
       *
       * @type {Number}
       * @constant
       */
      SHORT: WebGLConstants.WebGLConstants.SHORT,
 
      /**
       * 16-bit unsigned short corresponding to <code>UNSIGNED_SHORT</code> and the type
       * of an element in <code>Uint16Array</code>.
       *
       * @type {Number}
       * @constant
       */
      UNSIGNED_SHORT: WebGLConstants.WebGLConstants.UNSIGNED_SHORT,
 
      /**
       * 32-bit signed int corresponding to <code>INT</code> and the type
       * of an element in <code>Int32Array</code>.
       *
       * @memberOf ComponentDatatype
       *
       * @type {Number}
       * @constant
       */
      INT: WebGLConstants.WebGLConstants.INT,
 
      /**
       * 32-bit unsigned int corresponding to <code>UNSIGNED_INT</code> and the type
       * of an element in <code>Uint32Array</code>.
       *
       * @memberOf ComponentDatatype
       *
       * @type {Number}
       * @constant
       */
      UNSIGNED_INT: WebGLConstants.WebGLConstants.UNSIGNED_INT,
 
      /**
       * 32-bit floating-point corresponding to <code>FLOAT</code> and the type
       * of an element in <code>Float32Array</code>.
       *
       * @type {Number}
       * @constant
       */
      FLOAT: WebGLConstants.WebGLConstants.FLOAT,
 
      /**
       * 64-bit floating-point corresponding to <code>gl.DOUBLE</code> (in Desktop OpenGL;
       * this is not supported in WebGL, and is emulated in Cesium via {@link GeometryPipeline.encodeAttribute})
       * and the type of an element in <code>Float64Array</code>.
       *
       * @memberOf ComponentDatatype
       *
       * @type {Number}
       * @constant
       * @default 0x140A
       */
      DOUBLE: WebGLConstants.WebGLConstants.DOUBLE,
    };
 
    /**
     * Returns the size, in bytes, of the corresponding datatype.
     *
     * @param {ComponentDatatype} componentDatatype The component datatype to get the size of.
     * @returns {Number} The size in bytes.
     *
     * @exception {DeveloperError} componentDatatype is not a valid value.
     *
     * @example
     * // Returns Int8Array.BYTES_PER_ELEMENT
     * var size = Cesium.ComponentDatatype.getSizeInBytes(Cesium.ComponentDatatype.BYTE);
     */
    ComponentDatatype.getSizeInBytes = function (componentDatatype) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(componentDatatype)) {
        throw new RuntimeError.DeveloperError("value is required.");
      }
      //>>includeEnd('debug');
 
      switch (componentDatatype) {
        case ComponentDatatype.BYTE:
          return Int8Array.BYTES_PER_ELEMENT;
        case ComponentDatatype.UNSIGNED_BYTE:
          return Uint8Array.BYTES_PER_ELEMENT;
        case ComponentDatatype.SHORT:
          return Int16Array.BYTES_PER_ELEMENT;
        case ComponentDatatype.UNSIGNED_SHORT:
          return Uint16Array.BYTES_PER_ELEMENT;
        case ComponentDatatype.INT:
          return Int32Array.BYTES_PER_ELEMENT;
        case ComponentDatatype.UNSIGNED_INT:
          return Uint32Array.BYTES_PER_ELEMENT;
        case ComponentDatatype.FLOAT:
          return Float32Array.BYTES_PER_ELEMENT;
        case ComponentDatatype.DOUBLE:
          return Float64Array.BYTES_PER_ELEMENT;
        //>>includeStart('debug', pragmas.debug);
        default:
          throw new RuntimeError.DeveloperError("componentDatatype is not a valid value.");
        //>>includeEnd('debug');
      }
    };
 
    /**
     * Gets the {@link ComponentDatatype} for the provided TypedArray instance.
     *
     * @param {Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} array The typed array.
     * @returns {ComponentDatatype} The ComponentDatatype for the provided array, or undefined if the array is not a TypedArray.
     */
    ComponentDatatype.fromTypedArray = function (array) {
      if (array instanceof Int8Array) {
        return ComponentDatatype.BYTE;
      }
      if (array instanceof Uint8Array) {
        return ComponentDatatype.UNSIGNED_BYTE;
      }
      if (array instanceof Int16Array) {
        return ComponentDatatype.SHORT;
      }
      if (array instanceof Uint16Array) {
        return ComponentDatatype.UNSIGNED_SHORT;
      }
      if (array instanceof Int32Array) {
        return ComponentDatatype.INT;
      }
      if (array instanceof Uint32Array) {
        return ComponentDatatype.UNSIGNED_INT;
      }
      if (array instanceof Float32Array) {
        return ComponentDatatype.FLOAT;
      }
      if (array instanceof Float64Array) {
        return ComponentDatatype.DOUBLE;
      }
    };
 
    /**
     * Validates that the provided component datatype is a valid {@link ComponentDatatype}
     *
     * @param {ComponentDatatype} componentDatatype The component datatype to validate.
     * @returns {Boolean} <code>true</code> if the provided component datatype is a valid value; otherwise, <code>false</code>.
     *
     * @example
     * if (!Cesium.ComponentDatatype.validate(componentDatatype)) {
     *   throw new Cesium.DeveloperError('componentDatatype must be a valid value.');
     * }
     */
    ComponentDatatype.validate = function (componentDatatype) {
      return (
        when.defined(componentDatatype) &&
        (componentDatatype === ComponentDatatype.BYTE ||
          componentDatatype === ComponentDatatype.UNSIGNED_BYTE ||
          componentDatatype === ComponentDatatype.SHORT ||
          componentDatatype === ComponentDatatype.UNSIGNED_SHORT ||
          componentDatatype === ComponentDatatype.INT ||
          componentDatatype === ComponentDatatype.UNSIGNED_INT ||
          componentDatatype === ComponentDatatype.FLOAT ||
          componentDatatype === ComponentDatatype.DOUBLE)
      );
    };
 
    /**
     * Creates a typed array corresponding to component data type.
     *
     * @param {ComponentDatatype} componentDatatype The component data type.
     * @param {Number|Array} valuesOrLength The length of the array to create or an array.
     * @returns {Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} A typed array.
     *
     * @exception {DeveloperError} componentDatatype is not a valid value.
     *
     * @example
     * // creates a Float32Array with length of 100
     * var typedArray = Cesium.ComponentDatatype.createTypedArray(Cesium.ComponentDatatype.FLOAT, 100);
     */
    ComponentDatatype.createTypedArray = function (
      componentDatatype,
      valuesOrLength
    ) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(componentDatatype)) {
        throw new RuntimeError.DeveloperError("componentDatatype is required.");
      }
      if (!when.defined(valuesOrLength)) {
        throw new RuntimeError.DeveloperError("valuesOrLength is required.");
      }
      //>>includeEnd('debug');
 
      switch (componentDatatype) {
        case ComponentDatatype.BYTE:
          return new Int8Array(valuesOrLength);
        case ComponentDatatype.UNSIGNED_BYTE:
          return new Uint8Array(valuesOrLength);
        case ComponentDatatype.SHORT:
          return new Int16Array(valuesOrLength);
        case ComponentDatatype.UNSIGNED_SHORT:
          return new Uint16Array(valuesOrLength);
        case ComponentDatatype.INT:
          return new Int32Array(valuesOrLength);
        case ComponentDatatype.UNSIGNED_INT:
          return new Uint32Array(valuesOrLength);
        case ComponentDatatype.FLOAT:
          return new Float32Array(valuesOrLength);
        case ComponentDatatype.DOUBLE:
          return new Float64Array(valuesOrLength);
        //>>includeStart('debug', pragmas.debug);
        default:
          throw new RuntimeError.DeveloperError("componentDatatype is not a valid value.");
        //>>includeEnd('debug');
      }
    };
 
    /**
     * Creates a typed view of an array of bytes.
     *
     * @param {ComponentDatatype} componentDatatype The type of the view to create.
     * @param {ArrayBuffer} buffer The buffer storage to use for the view.
     * @param {Number} [byteOffset] The offset, in bytes, to the first element in the view.
     * @param {Number} [length] The number of elements in the view.
     * @returns {Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} A typed array view of the buffer.
     *
     * @exception {DeveloperError} componentDatatype is not a valid value.
     */
    ComponentDatatype.createArrayBufferView = function (
      componentDatatype,
      buffer,
      byteOffset,
      length
    ) {
      //>>includeStart('debug', pragmas.debug);
      if (!when.defined(componentDatatype)) {
        throw new RuntimeError.DeveloperError("componentDatatype is required.");
      }
      if (!when.defined(buffer)) {
        throw new RuntimeError.DeveloperError("buffer is required.");
      }
      //>>includeEnd('debug');
 
      byteOffset = when.defaultValue(byteOffset, 0);
      length = when.defaultValue(
        length,
        (buffer.byteLength - byteOffset) /
          ComponentDatatype.getSizeInBytes(componentDatatype)
      );
 
      switch (componentDatatype) {
        case ComponentDatatype.BYTE:
          return new Int8Array(buffer, byteOffset, length);
        case ComponentDatatype.UNSIGNED_BYTE:
          return new Uint8Array(buffer, byteOffset, length);
        case ComponentDatatype.SHORT:
          return new Int16Array(buffer, byteOffset, length);
        case ComponentDatatype.UNSIGNED_SHORT:
          return new Uint16Array(buffer, byteOffset, length);
        case ComponentDatatype.INT:
          return new Int32Array(buffer, byteOffset, length);
        case ComponentDatatype.UNSIGNED_INT:
          return new Uint32Array(buffer, byteOffset, length);
        case ComponentDatatype.FLOAT:
          return new Float32Array(buffer, byteOffset, length);
        case ComponentDatatype.DOUBLE:
          return new Float64Array(buffer, byteOffset, length);
        //>>includeStart('debug', pragmas.debug);
        default:
          throw new RuntimeError.DeveloperError("componentDatatype is not a valid value.");
        //>>includeEnd('debug');
      }
    };
 
    /**
     * Get the ComponentDatatype from its name.
     *
     * @param {String} name The name of the ComponentDatatype.
     * @returns {ComponentDatatype} The ComponentDatatype.
     *
     * @exception {DeveloperError} name is not a valid value.
     */
    ComponentDatatype.fromName = function (name) {
      switch (name) {
        case "BYTE":
          return ComponentDatatype.BYTE;
        case "UNSIGNED_BYTE":
          return ComponentDatatype.UNSIGNED_BYTE;
        case "SHORT":
          return ComponentDatatype.SHORT;
        case "UNSIGNED_SHORT":
          return ComponentDatatype.UNSIGNED_SHORT;
        case "INT":
          return ComponentDatatype.INT;
        case "UNSIGNED_INT":
          return ComponentDatatype.UNSIGNED_INT;
        case "FLOAT":
          return ComponentDatatype.FLOAT;
        case "DOUBLE":
          return ComponentDatatype.DOUBLE;
        //>>includeStart('debug', pragmas.debug);
        default:
          throw new RuntimeError.DeveloperError("name is not a valid value.");
        //>>includeEnd('debug');
      }
    };
    var ComponentDatatype$1 = Object.freeze(ComponentDatatype);
 
    exports.CesiumMath = CesiumMath;
    exports.ComponentDatatype = ComponentDatatype$1;
 
}));