referrerpolicy=no-referrer-when-downgrade
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
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Types that implement [`PerThing`](PerThing) can be used as a floating-point alternative for
//! numbers that operate within the realm of `[0, 1]`. The primary types may you encounter in
//! Substrate would be the following:
//! - [`Percent`](Percent) - parts of one hundred.
//! - [`Permill`](Permill) - parts of a million.
//! - [`Perbill`](Perbill) - parts of a billion.
//!
//! In use, you may see them being used as follows:
//!
//! > **[`Perbill`](Perbill), parts of a billion**
#![doc = docify::embed!("./src/lib.rs", perbill_example)]
//! > **[`Percent`](Percent), parts of a hundred**
#![doc = docify::embed!("./src/lib.rs", percent_example)]
//!
//! Note that `Percent` is represented as a _rounded down_, fixed point
//! number (see the example above). Unlike primitive types, types that implement
//! [`PerThing`](PerThing) will also not overflow, and are therefore safe to use.
//! They adopt the same behavior that a saturated calculation would provide, meaning that if one is
//! to go over "100%", it wouldn't overflow, but simply stop at the upper or lower bound.
//!
//! For use cases which require precision beyond the range of `[0, 1]`, there are fixed-point types
//! which can be used.
//!
//! Each of these can be used to construct and represent ratios within our runtime.
//! You will find types like [`Perbill`](Perbill) being used often in pallet
//! development.  `pallet_referenda` is a good example of a pallet which makes good use of fixed
//! point arithmetic, as it relies on representing various curves and thresholds relating to
//! governance.
//!
//! #### Fixed Point Arithmetic with [`PerThing`](PerThing)
//!
//! As stated, one can also perform mathematics using these types directly. For example, finding the
//! percentage of a particular item:

#![doc = docify::embed!("./src/lib.rs", percent_mult)]

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::traits::{
	BaseArithmetic, Bounded, CheckedAdd, CheckedMul, CheckedSub, One, SaturatedConversion,
	Saturating, UniqueSaturatedInto, Unsigned, Zero,
};
use codec::{CompactAs, Encode};
use core::{
	fmt, ops,
	ops::{Add, Sub},
};
use num_traits::{Pow, SaturatingAdd, SaturatingSub};

/// Get the inner type of a `PerThing`.
pub type InnerOf<P> = <P as PerThing>::Inner;

/// Get the upper type of a `PerThing`.
pub type UpperOf<P> = <P as PerThing>::Upper;

pub trait RationalArg:
	Clone
	+ Ord
	+ ops::Div<Self, Output = Self>
	+ ops::Rem<Self, Output = Self>
	+ ops::Add<Self, Output = Self>
	+ ops::AddAssign<Self>
	+ Unsigned
	+ Zero
	+ One
	+ crate::MultiplyRational
{
}

impl<
		T: Clone
			+ Ord
			+ ops::Div<Self, Output = Self>
			+ ops::Rem<Self, Output = Self>
			+ ops::Add<Self, Output = Self>
			+ ops::AddAssign<Self>
			+ Unsigned
			+ Zero
			+ One
			+ crate::MultiplyRational,
	> RationalArg for T
{
}

pub trait MultiplyArg:
	Clone
	+ ops::Rem<Self, Output = Self>
	+ ops::Div<Self, Output = Self>
	+ ops::Mul<Self, Output = Self>
	+ ops::Add<Self, Output = Self>
	+ Unsigned
{
}

impl<
		T: Clone
			+ ops::Rem<Self, Output = Self>
			+ ops::Div<Self, Output = Self>
			+ ops::Mul<Self, Output = Self>
			+ ops::Add<Self, Output = Self>
			+ Unsigned,
	> MultiplyArg for T
{
}

pub trait ReciprocalArg: MultiplyArg + Saturating {}
impl<T: MultiplyArg + Saturating> ReciprocalArg for T {}

/// Something that implements a fixed point ration with an arbitrary granularity `X`, as _parts per
/// `X`_.
pub trait PerThing:
	Sized
	+ Saturating
	+ Copy
	+ Default
	+ Eq
	+ PartialEq
	+ Ord
	+ PartialOrd
	+ Bounded
	+ fmt::Debug
	+ ops::Div<Output = Self>
	+ ops::Mul<Output = Self>
	+ Pow<usize, Output = Self>
{
	/// The data type used to build this per-thingy.
	type Inner: BaseArithmetic + Unsigned + Copy + Into<u128> + fmt::Debug + crate::MultiplyRational;

	/// A data type larger than `Self::Inner`, used to avoid overflow in some computations.
	/// It must be able to compute `ACCURACY^2`.
	type Upper: BaseArithmetic
		+ Copy
		+ From<Self::Inner>
		+ TryInto<Self::Inner>
		+ UniqueSaturatedInto<Self::Inner>
		+ Unsigned
		+ fmt::Debug
		+ crate::MultiplyRational;

	/// The accuracy of this type.
	const ACCURACY: Self::Inner;

	/// Equivalent to `Self::from_parts(0)`.
	fn zero() -> Self {
		Self::from_parts(Self::Inner::zero())
	}

	/// Return `true` if this is nothing.
	fn is_zero(&self) -> bool {
		self.deconstruct() == Self::Inner::zero()
	}

	/// Equivalent to `Self::from_parts(Self::ACCURACY)`.
	fn one() -> Self {
		Self::from_parts(Self::ACCURACY)
	}

	/// Return `true` if this is one.
	fn is_one(&self) -> bool {
		self.deconstruct() == Self::ACCURACY
	}

	/// Return the next lower value to `self` or `self` if it is already zero.
	fn less_epsilon(self) -> Self {
		if self.is_zero() {
			return self
		}
		Self::from_parts(self.deconstruct() - One::one())
	}

	/// Return the next lower value to `self` or an error with the same value if `self` is already
	/// zero.
	fn try_less_epsilon(self) -> Result<Self, Self> {
		if self.is_zero() {
			return Err(self)
		}
		Ok(Self::from_parts(self.deconstruct() - One::one()))
	}

	/// Return the next higher value to `self` or `self` if it is already one.
	fn plus_epsilon(self) -> Self {
		if self.is_one() {
			return self
		}
		Self::from_parts(self.deconstruct() + One::one())
	}

	/// Return the next higher value to `self` or an error with the same value if `self` is already
	/// one.
	fn try_plus_epsilon(self) -> Result<Self, Self> {
		if self.is_one() {
			return Err(self)
		}
		Ok(Self::from_parts(self.deconstruct() + One::one()))
	}

	/// Build this type from a percent. Equivalent to `Self::from_parts(x * Self::ACCURACY / 100)`
	/// but more accurate and can cope with potential type overflows.
	fn from_percent(x: Self::Inner) -> Self {
		let a: Self::Inner = x.min(100.into());
		let b: Self::Inner = 100.into();
		Self::from_rational::<Self::Inner>(a, b)
	}

	/// Return the product of multiplication of this value by itself.
	fn square(self) -> Self {
		let p = Self::Upper::from(self.deconstruct());
		let q = Self::Upper::from(Self::ACCURACY);
		Self::from_rational::<Self::Upper>(p * p, q * q)
	}

	/// Return the part left when `self` is saturating-subtracted from `Self::one()`.
	fn left_from_one(self) -> Self {
		Self::one().saturating_sub(self)
	}

	/// Multiplication that always rounds down to a whole number. The standard `Mul` rounds to the
	/// nearest whole number.
	///
	/// ```rust
	/// # use sp_arithmetic::{Percent, PerThing};
	/// # fn main () {
	/// // round to nearest
	/// assert_eq!(Percent::from_percent(34) * 10u64, 3);
	/// assert_eq!(Percent::from_percent(36) * 10u64, 4);
	///
	/// // round down
	/// assert_eq!(Percent::from_percent(34).mul_floor(10u64), 3);
	/// assert_eq!(Percent::from_percent(36).mul_floor(10u64), 3);
	/// # }
	/// ```
	fn mul_floor<N>(self, b: N) -> N
	where
		N: MultiplyArg + UniqueSaturatedInto<Self::Inner>,
		Self::Inner: Into<N>,
	{
		overflow_prune_mul::<N, Self>(b, self.deconstruct(), Rounding::Down)
	}

	/// Multiplication that always rounds the result up to a whole number. The standard `Mul`
	/// rounds to the nearest whole number.
	///
	/// ```rust
	/// # use sp_arithmetic::{Percent, PerThing};
	/// # fn main () {
	/// // round to nearest
	/// assert_eq!(Percent::from_percent(34) * 10u64, 3);
	/// assert_eq!(Percent::from_percent(36) * 10u64, 4);
	///
	/// // round up
	/// assert_eq!(Percent::from_percent(34).mul_ceil(10u64), 4);
	/// assert_eq!(Percent::from_percent(36).mul_ceil(10u64), 4);
	/// # }
	/// ```
	fn mul_ceil<N>(self, b: N) -> N
	where
		N: MultiplyArg + UniqueSaturatedInto<Self::Inner>,
		Self::Inner: Into<N>,
	{
		overflow_prune_mul::<N, Self>(b, self.deconstruct(), Rounding::Up)
	}

	/// Saturating multiplication by the reciprocal of `self`.	The result is rounded to the
	/// nearest whole number and saturates at the numeric bounds instead of overflowing.
	///
	/// ```rust
	/// # use sp_arithmetic::{Percent, PerThing};
	/// # fn main () {
	/// assert_eq!(Percent::from_percent(50).saturating_reciprocal_mul(10u64), 20);
	/// # }
	/// ```
	fn saturating_reciprocal_mul<N>(self, b: N) -> N
	where
		N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
		Self::Inner: Into<N>,
	{
		saturating_reciprocal_mul::<N, Self>(b, self.deconstruct(), Rounding::NearestPrefUp)
	}

	/// Saturating multiplication by the reciprocal of `self`.	The result is rounded down to the
	/// nearest whole number and saturates at the numeric bounds instead of overflowing.
	///
	/// ```rust
	/// # use sp_arithmetic::{Percent, PerThing};
	/// # fn main () {
	/// // round to nearest
	/// assert_eq!(Percent::from_percent(60).saturating_reciprocal_mul(10u64), 17);
	/// // round down
	/// assert_eq!(Percent::from_percent(60).saturating_reciprocal_mul_floor(10u64), 16);
	/// # }
	/// ```
	fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N
	where
		N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
		Self::Inner: Into<N>,
	{
		saturating_reciprocal_mul::<N, Self>(b, self.deconstruct(), Rounding::Down)
	}

	/// Saturating multiplication by the reciprocal of `self`.	The result is rounded up to the
	/// nearest whole number and saturates at the numeric bounds instead of overflowing.
	///
	/// ```rust
	/// # use sp_arithmetic::{Percent, PerThing};
	/// # fn main () {
	/// // round to nearest
	/// assert_eq!(Percent::from_percent(61).saturating_reciprocal_mul(10u64), 16);
	/// // round up
	/// assert_eq!(Percent::from_percent(61).saturating_reciprocal_mul_ceil(10u64), 17);
	/// # }
	/// ```
	fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N
	where
		N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
		Self::Inner: Into<N>,
	{
		saturating_reciprocal_mul::<N, Self>(b, self.deconstruct(), Rounding::Up)
	}

	/// Consume self and return the number of parts per thing.
	fn deconstruct(self) -> Self::Inner;

	/// Build this type from a number of parts per thing.
	fn from_parts(parts: Self::Inner) -> Self;

	/// Converts a fraction into `Self`.
	#[cfg(feature = "std")]
	fn from_float(x: f64) -> Self;

	/// Same as `Self::from_float`.
	#[deprecated = "Use from_float instead"]
	#[cfg(feature = "std")]
	fn from_fraction(x: f64) -> Self {
		Self::from_float(x)
	}

	/// Approximate the fraction `p/q` into a per-thing fraction. This will never overflow.
	///
	/// The computation of this approximation is performed in the generic type `N`. Given
	/// `M` as the data type that can hold the maximum value of this per-thing (e.g. u32 for
	/// perbill), this can only work if `N == M` or `N: From<M> + TryInto<M>`.
	///
	/// Note that this always rounds _down_, i.e.
	///
	/// ```rust
	/// # use sp_arithmetic::{Percent, PerThing};
	/// # fn main () {
	/// // 989/1000 is technically closer to 99%.
	/// assert_eq!(
	/// 	Percent::from_rational(989u64, 1000),
	/// 	Percent::from_parts(98),
	/// );
	/// # }
	/// ```
	fn from_rational<N>(p: N, q: N) -> Self
	where
		N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
		Self::Inner: Into<N>,
	{
		Self::from_rational_with_rounding(p, q, Rounding::Down).unwrap_or_else(|_| Self::one())
	}

	/// Approximate the fraction `p/q` into a per-thing fraction.
	///
	/// The computation of this approximation is performed in the generic type `N`. Given
	/// `M` as the data type that can hold the maximum value of this per-thing (e.g. `u32` for
	/// `Perbill`), this can only work if `N == M` or `N: From<M> + TryInto<M>`.
	///
	/// In the case of an overflow (or divide by zero), an `Err` is returned.
	///
	/// Rounding is determined by the parameter `rounding`, i.e.
	///
	/// ```rust
	/// # use sp_arithmetic::{Percent, PerThing, Rounding::*};
	/// # fn main () {
	/// // 989/100 is technically closer to 99%.
	/// assert_eq!(
	/// 	Percent::from_rational_with_rounding(989u64, 1000, Down).unwrap(),
	/// 	Percent::from_parts(98),
	/// );
	/// assert_eq!(
	/// 	Percent::from_rational_with_rounding(984u64, 1000, NearestPrefUp).unwrap(),
	/// 	Percent::from_parts(98),
	/// );
	/// assert_eq!(
	/// 	Percent::from_rational_with_rounding(985u64, 1000, NearestPrefDown).unwrap(),
	/// 	Percent::from_parts(98),
	/// );
	/// assert_eq!(
	/// 	Percent::from_rational_with_rounding(985u64, 1000, NearestPrefUp).unwrap(),
	/// 	Percent::from_parts(99),
	/// );
	/// assert_eq!(
	/// 	Percent::from_rational_with_rounding(986u64, 1000, NearestPrefDown).unwrap(),
	/// 	Percent::from_parts(99),
	/// );
	/// assert_eq!(
	/// 	Percent::from_rational_with_rounding(981u64, 1000, Up).unwrap(),
	/// 	Percent::from_parts(99),
	/// );
	/// assert_eq!(
	/// 	Percent::from_rational_with_rounding(1001u64, 1000, Up),
	/// 	Err(()),
	/// );
	/// # }
	/// ```
	///
	/// ```rust
	/// # use sp_arithmetic::{Percent, PerThing, Rounding::*};
	/// # fn main () {
	/// assert_eq!(
	/// 	Percent::from_rational_with_rounding(981u64, 1000, Up).unwrap(),
	/// 	Percent::from_parts(99),
	/// );
	/// # }
	/// ```
	fn from_rational_with_rounding<N>(p: N, q: N, rounding: Rounding) -> Result<Self, ()>
	where
		N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
		Self::Inner: Into<N>;

	/// Same as `Self::from_rational`.
	#[deprecated = "Use from_rational instead"]
	fn from_rational_approximation<N>(p: N, q: N) -> Self
	where
		N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
		Self::Inner: Into<N>,
	{
		Self::from_rational(p, q)
	}
}

/// The rounding method to use for unsigned quantities.
#[derive(Copy, Clone, core::fmt::Debug)]
pub enum Rounding {
	// Towards infinity.
	Up,
	// Towards zero.
	Down,
	// Nearest integer, rounding as `Up` when equidistant.
	NearestPrefUp,
	// Nearest integer, rounding as `Down` when equidistant.
	NearestPrefDown,
}

/// The rounding method to use.
#[derive(Copy, Clone, core::fmt::Debug)]
pub enum SignedRounding {
	// Towards positive infinity.
	High,
	// Towards negative infinity.
	Low,
	// Nearest integer, rounding as `High` when exactly equidistant.
	NearestPrefHigh,
	// Nearest integer, rounding as `Low` when exactly equidistant.
	NearestPrefLow,
	// Away from zero (up when positive, down when negative). When positive, equivalent to `High`.
	Major,
	// Towards zero (down when positive, up when negative). When positive, equivalent to `Low`.
	Minor,
	// Nearest integer, rounding as `Major` when exactly equidistant.
	NearestPrefMajor,
	// Nearest integer, rounding as `Minor` when exactly equidistant.
	NearestPrefMinor,
}

impl Rounding {
	/// Returns the value for `Rounding` which would give the same result ignorant of the sign.
	pub const fn from_signed(rounding: SignedRounding, negative: bool) -> Self {
		use Rounding::*;
		use SignedRounding::*;
		match (rounding, negative) {
			(Low, true) | (Major, _) | (High, false) => Up,
			(High, true) | (Minor, _) | (Low, false) => Down,
			(NearestPrefMajor, _) | (NearestPrefHigh, false) | (NearestPrefLow, true) =>
				NearestPrefUp,
			(NearestPrefMinor, _) | (NearestPrefLow, false) | (NearestPrefHigh, true) =>
				NearestPrefDown,
		}
	}
}

/// Saturating reciprocal multiplication. Compute `x / self`, saturating at the numeric
/// bounds instead of overflowing.
fn saturating_reciprocal_mul<N, P>(x: N, part: P::Inner, rounding: Rounding) -> N
where
	N: Clone
		+ UniqueSaturatedInto<P::Inner>
		+ ops::Div<N, Output = N>
		+ ops::Mul<N, Output = N>
		+ ops::Add<N, Output = N>
		+ ops::Rem<N, Output = N>
		+ Saturating
		+ Unsigned,
	P: PerThing,
	P::Inner: Into<N>,
{
	let maximum: N = P::ACCURACY.into();
	let c = rational_mul_correction::<N, P>(x.clone(), P::ACCURACY, part, rounding);
	(x / part.into()).saturating_mul(maximum).saturating_add(c)
}

/// Overflow-prune multiplication. Accurately multiply a value by `self` without overflowing.
fn overflow_prune_mul<N, P>(x: N, part: P::Inner, rounding: Rounding) -> N
where
	N: MultiplyArg + UniqueSaturatedInto<P::Inner>,
	P: PerThing,
	P::Inner: Into<N>,
{
	let maximum: N = P::ACCURACY.into();
	let part_n: N = part.into();
	let c = rational_mul_correction::<N, P>(x.clone(), part, P::ACCURACY, rounding);
	(x / maximum) * part_n + c
}

/// Compute the error due to integer division in the expression `x / denom * numer`.
///
/// Take the remainder of `x / denom` and multiply by  `numer / denom`. The result can be added
/// to `x / denom * numer` for an accurate result.
fn rational_mul_correction<N, P>(x: N, numer: P::Inner, denom: P::Inner, rounding: Rounding) -> N
where
	N: MultiplyArg + UniqueSaturatedInto<P::Inner>,
	P: PerThing,
	P::Inner: Into<N>,
{
	let numer_upper = P::Upper::from(numer);
	let denom_n: N = denom.into();
	let denom_upper = P::Upper::from(denom);
	let rem = x.rem(denom_n);
	// `rem` is less than `denom`, which fits in `P::Inner`.
	let rem_inner = rem.saturated_into::<P::Inner>();
	// `P::Upper` always fits `P::Inner::max_value().pow(2)`, thus it fits `rem * numer`.
	let rem_mul_upper = P::Upper::from(rem_inner) * numer_upper;
	// `rem` is less than `denom`, so `rem * numer / denom` is less than `numer`, which fits in
	// `P::Inner`.
	let mut rem_mul_div_inner = (rem_mul_upper / denom_upper).saturated_into::<P::Inner>();
	match rounding {
		// Already rounded down
		Rounding::Down => {},
		// Round up if the fractional part of the result is non-zero.
		Rounding::Up => {
			if rem_mul_upper % denom_upper > 0.into() {
				// `rem * numer / denom` is less than `numer`, so this will not overflow.
				rem_mul_div_inner += 1.into();
			}
		},
		Rounding::NearestPrefDown => {
			if rem_mul_upper % denom_upper > denom_upper / 2.into() {
				// `rem * numer / denom` is less than `numer`, so this will not overflow.
				rem_mul_div_inner += 1.into();
			}
		},
		Rounding::NearestPrefUp => {
			if rem_mul_upper % denom_upper >= denom_upper / 2.into() + denom_upper % 2.into() {
				// `rem * numer / denom` is less than `numer`, so this will not overflow.
				rem_mul_div_inner += 1.into();
			}
		},
	}
	rem_mul_div_inner.into()
}

macro_rules! implement_per_thing {
	(
		$name:ident,
		$test_mod:ident,
		[$($test_units:tt),+],
		$max:tt,
		$type:ty,
		$upper_type:ty,
		$title:expr $(,)?
	) => {
		/// A fixed point representation of a number in the range [0, 1].
		///
		#[doc = $title]
		#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
		#[derive(Encode, Copy, Clone, PartialEq, Eq, codec::MaxEncodedLen, PartialOrd, Ord, scale_info::TypeInfo)]
		pub struct $name($type);

		/// Implementation makes any compact encoding of `PerThing::Inner` valid,
		/// when decoding it will saturate up to `PerThing::ACCURACY`.
		impl CompactAs for $name {
			type As = $type;
			fn encode_as(&self) -> &Self::As {
				&self.0
			}
			fn decode_from(x: Self::As) -> Result<Self, codec::Error> {
				// Saturates if `x` is more than `$max` internally.
				Ok(Self::from_parts(x))
			}
		}

		impl From<codec::Compact<$name>> for $name {
			fn from(x: codec::Compact<$name>) -> $name {
				x.0
			}
		}

		#[cfg(feature = "std")]
		impl core::fmt::Debug for $name {
			fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
				if $max == <$type>::max_value() {
					// Not a power of ten: show as N/D and approx %
					let pc = (self.0 as f64) / (self.0 as f64) * 100f64;
					write!(fmt, "{:.2}% ({}/{})", pc, self.0, $max)
				} else {
					// A power of ten: calculate exact percent
					let divisor = $max / 100;
					let units = self.0 / divisor;
					let rest = self.0 % divisor;
					write!(fmt, "{}", units)?;
					if rest > 0 {
						write!(fmt, ".")?;
						let mut m = $max / 100;
						while rest % m > 0 {
							m /= 10;
							write!(fmt, "{:01}", rest / m % 10)?;
						}
					}
					write!(fmt, "%")
				}
			}
		}

		#[cfg(not(feature = "std"))]
		impl core::fmt::Debug for $name {
			fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
				if $max == <$type>::max_value() {
					// Not a power of ten: show as N/D and approx %
					write!(fmt, "{}/{}", self.0, $max)
				} else {
					// A power of ten: calculate exact percent
					let units = self.0 / ($max / 100);
					let rest = self.0 % ($max / 100);
					write!(fmt, "{}", units)?;
					if rest > 0 {
						write!(fmt, ".")?;
						let mut m = $max / 100;
						while rest % m > 0 {
							m /= 10;
							write!(fmt, "{:01}", rest / m % 10)?;
						}
					}
					write!(fmt, "%")
				}
			}
		}

		impl PerThing for $name {
			type Inner = $type;
			type Upper = $upper_type;

			const ACCURACY: Self::Inner = $max;

			/// Consume self and return the number of parts per thing.
			fn deconstruct(self) -> Self::Inner { self.0 }

			/// Build this type from a number of parts per thing.
			fn from_parts(parts: Self::Inner) -> Self { Self(parts.min($max)) }

			/// NOTE: saturate to 0 or 1 if x is beyond `[0, 1]`
			#[cfg(feature = "std")]
			fn from_float(x: f64) -> Self {
				Self::from_parts((x.max(0.).min(1.) * $max as f64) as Self::Inner)
			}

			fn from_rational_with_rounding<N>(p: N, q: N, r: Rounding) -> Result<Self, ()>
			where
				N: Clone
					+ Ord
					+ TryInto<Self::Inner>
					+ TryInto<Self::Upper>
					+ ops::Div<N, Output = N>
					+ ops::Rem<N, Output = N>
					+ ops::Add<N, Output = N>
					+ ops::AddAssign<N>
					+ Unsigned
					+ Zero
					+ One
					+ $crate::MultiplyRational,
				Self::Inner: Into<N>
			{
				// q cannot be zero.
				if q.is_zero() { return Err(()) }
				// p should not be bigger than q.
				if p > q { return Err(()) }

				let max: N = $max.into();
				max.multiply_rational(p, q, r).ok_or(())?.try_into().map(|x| $name(x)).map_err(|_| ())
			}
		}

		impl $name {
			/// From an explicitly defined number of parts per maximum of the type.
			///
			// needed only for peru16. Since peru16 is the only type in which $max ==
			// $type::max_value(), rustc is being a smart-a** here by warning that the comparison
			// is not needed.
			#[allow(unused_comparisons)]
			pub const fn from_parts(parts: $type) -> Self {
				Self([parts, $max][(parts > $max) as usize])
			}

			/// Converts a percent into `Self`. Equal to `x / 100`.
			///
			/// This can be created at compile time.
			pub const fn from_percent(x: $type) -> Self {
				Self(([x, 100][(x > 100) as usize] as $upper_type * $max as $upper_type / 100) as $type)
			}

			/// See [`PerThing::one`]
			pub const fn one() -> Self {
				Self::from_parts($max)
			}

			/// See [`PerThing::is_one`].
			pub fn is_one(&self) -> bool {
				PerThing::is_one(self)
			}

			/// See [`PerThing::zero`].
			pub const fn zero() -> Self {
				Self::from_parts(0)
			}

			/// See [`PerThing::is_zero`].
			pub fn is_zero(&self) -> bool {
				PerThing::is_zero(self)
			}

			/// See [`PerThing::deconstruct`].
			pub const fn deconstruct(self) -> $type {
				self.0
			}

			/// See [`PerThing::square`].
			pub fn square(self) -> Self {
				PerThing::square(self)
			}

			/// See [`PerThing::from_float`].
			#[cfg(feature = "std")]
			pub fn from_float(x: f64) -> Self {
				<Self as PerThing>::from_float(x)
			}

			/// See [`PerThing::from_rational`].
			#[deprecated = "Use `PerThing::from_rational` instead"]
			pub fn from_rational_approximation<N>(p: N, q: N) -> Self
			where
				N: RationalArg+ TryInto<$type> + TryInto<$upper_type>,
				$type: Into<N>
			{
				<Self as PerThing>::from_rational(p, q)
			}

			/// See [`PerThing::from_rational`].
			pub fn from_rational<N>(p: N, q: N) -> Self
			where
				N: RationalArg+ TryInto<$type> + TryInto<$upper_type>,
				$type: Into<N>
			{
				<Self as PerThing>::from_rational(p, q)
			}

			/// Integer multiplication with another value, saturating at 1.
			pub fn int_mul(self, b: $type) -> Self {
				PerThing::from_parts(self.0.saturating_mul(b))
			}

			/// Integer division with another value, rounding down.
			pub fn int_div(self, b: Self) -> $type {
				self.0 / b.0
			}

			/// See [`PerThing::mul_floor`].
			pub fn mul_floor<N>(self, b: N) -> N
				where
					N: MultiplyArg + UniqueSaturatedInto<$type>,
					$type: Into<N>,

			{
				PerThing::mul_floor(self, b)
			}

			/// See [`PerThing::mul_ceil`].
			pub fn mul_ceil<N>(self, b: N) -> N
				where
					N: MultiplyArg + UniqueSaturatedInto<$type>,
					$type: Into<N>,
			{
				PerThing::mul_ceil(self, b)
			}

			/// See [`PerThing::saturating_reciprocal_mul`].
			pub fn saturating_reciprocal_mul<N>(self, b: N) -> N
				where
					N: ReciprocalArg + UniqueSaturatedInto<$type>,
					$type: Into<N>,
			{
				PerThing::saturating_reciprocal_mul(self, b)
			}

			/// See [`PerThing::saturating_reciprocal_mul_floor`].
			pub fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N
				where
					N: ReciprocalArg + UniqueSaturatedInto<$type>,
					$type: Into<N>,
			{
				PerThing::saturating_reciprocal_mul_floor(self, b)
			}

			/// See [`PerThing::saturating_reciprocal_mul_ceil`].
			pub fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N
				where
					N: ReciprocalArg + UniqueSaturatedInto<$type>,
					$type: Into<N>,
			{
				PerThing::saturating_reciprocal_mul_ceil(self, b)
			}

			/// Saturating division. Compute `self / rhs`, saturating at one if `rhs < self`.
			///
			/// The `rounding` method must be specified. e.g.:
			///
			/// ```rust
			/// # use sp_arithmetic::{Percent, PerThing, Rounding::*};
			/// # fn main () {
			/// let pc = |x| Percent::from_percent(x);
			/// assert_eq!(
			/// 	pc(2).saturating_div(pc(3), Down),
			/// 	pc(66),
			/// );
			/// assert_eq!(
			/// 	pc(1).saturating_div(pc(3), NearestPrefUp),
			/// 	pc(33),
			/// );
			/// assert_eq!(
			/// 	pc(2).saturating_div(pc(3), NearestPrefDown),
			/// 	pc(67),
			/// );
			/// assert_eq!(
			/// 	pc(1).saturating_div(pc(3), Up),
			/// 	pc(34),
			/// );
			/// # }
			/// ```
			pub fn saturating_div(self, rhs: Self, r: Rounding) -> Self {
				let p = self.0;
				let q = rhs.0;
				Self::from_rational_with_rounding(p, q, r).unwrap_or_else(|_| Self::one())
			}
		}

		impl Saturating for $name {
			/// Saturating addition. Compute `self + rhs`, saturating at the numeric bounds instead of
			/// overflowing. This operation is lossless if it does not saturate.
			fn saturating_add(self, rhs: Self) -> Self {
				// defensive-only: since `$max * 2 < $type::max_value()`, this can never overflow.
				Self::from_parts(self.0.saturating_add(rhs.0))
			}

			/// Saturating subtraction. Compute `self - rhs`, saturating at the numeric bounds instead of
			/// overflowing. This operation is lossless if it does not saturate.
			fn saturating_sub(self, rhs: Self) -> Self {
				Self::from_parts(self.0.saturating_sub(rhs.0))
			}

			/// Saturating multiply. Compute `self * rhs`, saturating at the numeric bounds instead of
			/// overflowing. This operation is lossy.
			fn saturating_mul(self, rhs: Self) -> Self {
				self * rhs
			}

			/// Saturating exponentiation. Computes `self.pow(exp)`, saturating at the numeric
			/// bounds instead of overflowing. This operation is lossy.
			fn saturating_pow(self, exp: usize) -> Self {
				self.pow(exp)
			}
		}

		impl codec::Decode for $name {
			fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
				let inner = <$type as codec::Decode>::decode(input)?;

				if inner <= <Self as PerThing>::ACCURACY {
					Ok(Self(inner))
				} else {
					Err("Value is greater than allowed maximum!".into())
				}
			}
		}

		impl Bounded for $name {
			fn min_value() -> Self {
				<Self as PerThing>::zero()
			}

			fn max_value() -> Self {
				<Self as PerThing>::one()
			}
		}

		impl ops::Mul for $name {
			type Output = Self;

			fn mul(self, rhs: Self) -> Self::Output {
				let a = self.0 as $upper_type;
				let b = rhs.0 as $upper_type;
				let m = <$upper_type>::from($max);
				let parts = a * b / m;
				// This will always fit into $type.
				Self::from_parts(parts as $type)
			}
		}

		impl Pow<usize> for $name {
			type Output = Self;

			fn pow(mut self, exp: usize) -> Self::Output {
				if exp == 0 || self.is_one() {
					return Self::one()
				}

				let mut result = self;
				let mut exp = exp - 1;
				while exp > 0 && !result.is_zero() {
					if exp % 2 != 0 {
						result = result * self;
						exp -= 1;
					}
					self = self.square();
					exp /= 2;
				}
				result
			}
		}

		impl ops::Div for $name {
			type Output = Self;

			fn div(self, rhs: Self) -> Self::Output {
				let p = self.0;
				let q = rhs.0;
				Self::from_rational(p, q)
			}
		}

		impl Default for $name {
			fn default() -> Self {
				<Self as PerThing>::zero()
			}
		}

		/// Non-overflow multiplication.
		///
		/// This is tailored to be used with a balance type.
		impl<N> ops::Mul<N> for $name
		where
			N: Clone + UniqueSaturatedInto<$type> + ops::Rem<N, Output=N>
				+ ops::Div<N, Output=N> + ops::Mul<N, Output=N> + ops::Add<N, Output=N> + Unsigned,
			$type: Into<N>,
		{
			type Output = N;
			fn mul(self, b: N) -> Self::Output {
				overflow_prune_mul::<N, Self>(b, self.deconstruct(), Rounding::NearestPrefDown)
			}
		}

		impl<N> ops::Div<N> for $name where $type: TryFrom<N> {
			type Output = Self;
			fn div(self, b: N) -> Self::Output {
				<$type>::try_from(b).map_or(Self::zero(), |d| Self::from_parts(self.0 / d))
			}
		}

		impl Add<Self> for $name {
			type Output = $name;

			// For PerU16, $max == u16::MAX, so we need this `allow`.
			#[allow(unused_comparisons)]
			#[inline]
			fn add(self, rhs: Self) -> Self::Output {
				let inner = self.deconstruct().add(rhs.deconstruct());
				debug_assert!(inner <= $max);
				$name::from_parts(inner)
			}
		}

		impl CheckedAdd for $name {
			// For PerU16, $max == u16::MAX, so we need this `allow`.
			#[allow(unused_comparisons)]
			#[inline]
			fn checked_add(&self, rhs: &Self) -> Option<Self> {
				self.deconstruct()
					.checked_add(rhs.deconstruct())
					.map(|inner| if inner > $max { None } else { Some($name::from_parts(inner)) })
					.flatten()
			}
		}

		impl Sub<Self> for $name {
			type Output = $name;

			#[inline]
			fn sub(self, rhs: Self) -> Self::Output {
				$name::from_parts(self.deconstruct().sub(rhs.deconstruct()))
			}
		}

		impl CheckedSub for $name {
			#[inline]
			fn checked_sub(&self, v: &Self) -> Option<Self> {
				self.deconstruct().checked_sub(v.deconstruct()).map($name::from_parts)
			}
		}

		impl SaturatingAdd for $name {
			#[inline]
			fn saturating_add(&self, v: &Self) -> Self {
				$name::from_parts(self.deconstruct().saturating_add(v.deconstruct()))
			}
		}

		impl SaturatingSub for $name {
			#[inline]
			fn saturating_sub(&self, v: &Self) -> Self {
				$name::from_parts(self.deconstruct().saturating_sub(v.deconstruct()))
			}
		}

		/// # Note
		/// CheckedMul will never fail for PerThings.
		impl CheckedMul for $name {
			#[inline]
			fn checked_mul(&self, rhs: &Self) -> Option<Self> {
				Some(*self * *rhs)
			}
		}

		impl $crate::traits::Zero for $name {
			fn zero() -> Self {
				Self::zero()
			}

			fn is_zero(&self) -> bool {
				self == &Self::zero()
			}
		}

		impl $crate::traits::One for $name {
			fn one() -> Self {
				Self::one()
			}
		}

		#[cfg(test)]
		mod $test_mod {
			use codec::{Encode, Decode};
			use super::{$name, Saturating, PerThing};
			use crate::traits::Zero;

			#[test]
			fn macro_expanded_correctly() {
				// needed for the `from_percent` to work. UPDATE: this is no longer needed; yet note
				// that tests that use percentage or fractions such as $name::from_float(0.2) to
				// create values will most likely be inaccurate when used with per_things that are
				// not multiples of 100.
				// assert!($max >= 100);
				// assert!($max % 100 == 0);

				// needed for `from_rational`
				assert!(2 * ($max as $upper_type) < <$upper_type>::max_value());
				assert!(<$upper_type>::from($max) < <$upper_type>::max_value());

				// for something like percent they can be the same.
				assert!((<$type>::max_value() as $upper_type) <= <$upper_type>::max_value());
				assert!(<$upper_type>::from($max).checked_mul($max.into()).is_some());

				// make sure saturating_pow won't overflow the upper type
				assert!(<$upper_type>::from($max) * <$upper_type>::from($max) < <$upper_type>::max_value());
			}

			#[derive(Encode, Decode, PartialEq, Eq, Debug)]
			struct WithCompact<T: codec::HasCompact> {
				data: T,
			}

			#[test]
			fn has_compact() {
				let data = WithCompact { data: $name(1) };
				let encoded = data.encode();
				assert_eq!(data, WithCompact::<$name>::decode(&mut &encoded[..]).unwrap());
			}

			#[test]
			fn compact_encoding() {
				let tests = [
					// assume all per_things have the size u8 at least.
					(0 as $type, 1usize),
					(1 as $type, 1usize),
					(63, 1),
					(64, 2),
					(65, 2),
					// (<$type>::max_value(), <$type>::max_value().encode().len() + 1)
				];
				for &(n, l) in &tests {
					let compact: codec::Compact<$name> = $name(n).into();
					let encoded = compact.encode();
					assert_eq!(encoded.len(), l);
					let decoded = <codec::Compact<$name>>::decode(&mut & encoded[..])
						.unwrap();
					let per_thingy: $name = decoded.into();
					assert_eq!(per_thingy, $name(n));
				}
			}

			#[test]
			fn from_parts_cannot_overflow() {
				assert_eq!(<$name>::from_parts($max.saturating_add(1)), <$name>::one());
			}

			#[test]
			fn has_max_encoded_len() {
				struct AsMaxEncodedLen<T: codec::MaxEncodedLen> {
					_data: T,
				}

				let _ = AsMaxEncodedLen { _data: $name(1) };
			}

			#[test]
			fn fail_on_invalid_encoded_value() {
				let value = <$upper_type>::from($max) * 2;
				let casted = value as $type;
				let encoded = casted.encode();

				// For types where `$max == $type::maximum()` we can not
				if <$upper_type>::from(casted) == value {
					assert_eq!(
						$name::decode(&mut &encoded[..]),
						Err("Value is greater than allowed maximum!".into()),
					);
				}
			}

			#[test]
			fn per_thing_api_works() {
				// some really basic stuff
				assert_eq!($name::zero(), $name::from_parts(Zero::zero()));
				assert_eq!($name::one(), $name::from_parts($max));
				assert_eq!($name::ACCURACY, $max);

				assert_eq!($name::from_percent(0), $name::from_parts(Zero::zero()));
				assert_eq!($name::from_percent(10), $name::from_parts($max / 10));
				assert_eq!($name::from_percent(50), $name::from_parts($max / 2));
				assert_eq!($name::from_percent(100), $name::from_parts($max));
				assert_eq!($name::from_percent(200), $name::from_parts($max));

				assert_eq!($name::from_float(0.0), $name::from_parts(Zero::zero()));
				assert_eq!($name::from_float(0.1), $name::from_parts($max / 10));
				assert_eq!($name::from_float(1.0), $name::from_parts($max));
				assert_eq!($name::from_float(2.0), $name::from_parts($max));
				assert_eq!($name::from_float(-1.0), $name::from_parts(Zero::zero()));
			}

			#[test]
			fn percent_trait_impl_works() {
				assert_eq!(<$name as PerThing>::from_percent(0), $name::from_parts(Zero::zero()));
				assert_eq!(<$name as PerThing>::from_percent(10), $name::from_parts($max / 10));
				assert_eq!(<$name as PerThing>::from_percent(50), $name::from_parts($max / 2));
				assert_eq!(<$name as PerThing>::from_percent(100), $name::from_parts($max));
				assert_eq!(<$name as PerThing>::from_percent(200), $name::from_parts($max));
			}

			macro_rules! u256ify {
				($val:expr) => {
					Into::<U256>::into($val)
				};
			}

			macro_rules! per_thing_mul_test {
				($num_type:tt) => {
					// multiplication from all sort of from_percent
					assert_eq!(
						$name::from_float(1.0) * $num_type::max_value(),
						$num_type::max_value()
					);
					if $max % 100 == 0 {
						assert_eq_error_rate!(
							$name::from_percent(99) * $num_type::max_value(),
							((Into::<U256>::into($num_type::max_value()) * 99u32) / 100u32).as_u128() as $num_type,
							1,
						);
						assert_eq!(
							$name::from_float(0.5) * $num_type::max_value(),
							$num_type::max_value() / 2,
						);
						assert_eq_error_rate!(
							$name::from_percent(1) * $num_type::max_value(),
							$num_type::max_value() / 100,
							1,
						);
					} else {
						assert_eq!(
							$name::from_float(0.99) * <$num_type>::max_value(),
							(
								(
									u256ify!($name::from_float(0.99).0) *
									u256ify!(<$num_type>::max_value()) /
									u256ify!($max)
								).as_u128()
							) as $num_type,
						);
						assert_eq!(
							$name::from_float(0.50) * <$num_type>::max_value(),
							(
								(
									u256ify!($name::from_float(0.50).0) *
									u256ify!(<$num_type>::max_value()) /
									u256ify!($max)
								).as_u128()
							) as $num_type,
						);
						assert_eq!(
							$name::from_float(0.01) * <$num_type>::max_value(),
							(
								(
									u256ify!($name::from_float(0.01).0) *
									u256ify!(<$num_type>::max_value()) /
									u256ify!($max)
								).as_u128()
							) as $num_type,
						);
					}

					assert_eq!($name::from_float(0.0) * $num_type::max_value(), 0);

					// // multiplication with bounds
					assert_eq!($name::one() * $num_type::max_value(), $num_type::max_value());
					assert_eq!($name::zero() * $num_type::max_value(), 0);
				}
			}

			#[test]
			fn per_thing_mul_works() {
				use primitive_types::U256;

				// accuracy test
				assert_eq!(
					$name::from_rational(1 as $type, 3) * 30 as $type,
					10,
				);

				$(per_thing_mul_test!($test_units);)*
			}

			#[test]
			fn per_thing_mul_rounds_to_nearest_number() {
				assert_eq!($name::from_percent(33) * 10u64, 3);
				assert_eq!($name::from_percent(34) * 10u64, 3);
				assert_eq!($name::from_percent(35) * 10u64, 3);
				assert_eq!($name::from_percent(36) * 10u64, 4);
			}

			#[test]
			fn per_thing_multiplication_with_large_number() {
				use primitive_types::U256;
				let max_minus_one = $max - 1;
				assert_eq_error_rate!(
					$name::from_parts(max_minus_one) * std::u128::MAX,
					((Into::<U256>::into(std::u128::MAX) * max_minus_one) / $max).as_u128(),
					1,
				);
			}

			macro_rules! per_thing_from_rationale_approx_test {
				($num_type:tt) => {
					// within accuracy boundary
					assert_eq!(
						$name::from_rational(1 as $num_type, 0),
						$name::one(),
					);
					assert_eq!(
						$name::from_rational(1 as $num_type, 1),
						$name::one(),
					);
					assert_eq_error_rate!(
						$name::from_rational(1 as $num_type, 3).0,
						$name::from_parts($max / 3).0,
						2
					);
					assert_eq!(
						$name::from_rational(1 as $num_type, 10),
						$name::from_float(0.10),
					);
					assert_eq!(
						$name::from_rational(1 as $num_type, 4),
						$name::from_float(0.25),
					);
					assert_eq!(
						$name::from_rational(1 as $num_type, 4),
						$name::from_rational(2 as $num_type, 8),
					);
					// no accurate anymore but won't overflow.
					assert_eq_error_rate!(
						$name::from_rational(
							$num_type::max_value() - 1,
							$num_type::max_value()
						).0 as $upper_type,
						$name::one().0 as $upper_type,
						2,
					);
					assert_eq_error_rate!(
						$name::from_rational(
							$num_type::max_value() / 3,
							$num_type::max_value()
						).0 as $upper_type,
						$name::from_parts($max / 3).0 as $upper_type,
						2,
					);
					assert_eq!(
						$name::from_rational(1, $num_type::max_value()),
						$name::zero(),
					);
				};
			}

			#[test]
			fn per_thing_from_rationale_approx_works() {
				// This is just to make sure something like Percent which _might_ get built from a
				// u8 does not overflow in the context of this test.
				let max_value = <$upper_type>::from($max);

				// almost at the edge
				assert_eq!(
					$name::from_rational(max_value - 1, max_value + 1),
					$name::from_parts($max - 2),
				);
				assert_eq!(
					$name::from_rational(1, $max - 1),
					$name::from_parts(1),
				);
				assert_eq!(
					$name::from_rational(1, $max),
					$name::from_parts(1),
				);
				assert_eq!(
					$name::from_rational(2, 2 * max_value - 1),
					$name::from_parts(1),
				);
				assert_eq!(
					$name::from_rational(1, max_value + 1),
					$name::zero(),
				);
				assert_eq!(
					$name::from_rational(3 * max_value / 2, 3 * max_value),
					$name::from_float(0.5),
				);

				$(per_thing_from_rationale_approx_test!($test_units);)*
			}

			#[test]
			fn per_things_mul_operates_in_output_type() {
				// assert_eq!($name::from_float(0.5) * 100u32, 50u32);
				assert_eq!($name::from_float(0.5) * 100u64, 50u64);
				assert_eq!($name::from_float(0.5) * 100u128, 50u128);
			}

			#[test]
			fn per_thing_saturating_op_works() {
				assert_eq_error_rate!(
					$name::from_float(0.5).saturating_add($name::from_float(0.4)).0 as $upper_type,
					$name::from_float(0.9).0 as $upper_type,
					2,
				);
				assert_eq_error_rate!(
					$name::from_float(0.5).saturating_add($name::from_float(0.5)).0 as $upper_type,
					$name::one().0 as $upper_type,
					2,
				);
				assert_eq!(
					$name::from_float(0.6).saturating_add($name::from_float(0.5)),
					$name::one(),
				);

				assert_eq_error_rate!(
					$name::from_float(0.6).saturating_sub($name::from_float(0.5)).0 as $upper_type,
					$name::from_float(0.1).0 as $upper_type,
					2,
				);
				assert_eq!(
					$name::from_float(0.6).saturating_sub($name::from_float(0.6)),
					$name::from_float(0.0),
				);
				assert_eq!(
					$name::from_float(0.6).saturating_sub($name::from_float(0.7)),
					$name::from_float(0.0),
				);

				assert_eq_error_rate!(
					$name::from_float(0.5).saturating_mul($name::from_float(0.5)).0 as $upper_type,
					$name::from_float(0.25).0 as $upper_type,
					2,
				);
				assert_eq_error_rate!(
					$name::from_float(0.2).saturating_mul($name::from_float(0.2)).0 as $upper_type,
					$name::from_float(0.04).0 as $upper_type,
					2,
				);
				assert_eq_error_rate!(
					$name::from_float(0.1).saturating_mul($name::from_float(0.1)).0 as $upper_type,
					$name::from_float(0.01).0 as $upper_type,
					1,
				);
			}

			#[test]
			fn per_thing_square_works() {
				assert_eq!($name::from_float(1.0).square(), $name::from_float(1.0));
				assert_eq!($name::from_float(0.5).square(), $name::from_float(0.25));
				assert_eq!($name::from_float(0.1).square(), $name::from_float(0.01));
				assert_eq!(
					$name::from_float(0.02).square(),
					$name::from_parts((4 * <$upper_type>::from($max) / 100 / 100) as $type)
				);
			}

			#[test]
			fn per_things_div_works() {
				// normal
				assert_eq_error_rate!(
					($name::from_float(0.1) / $name::from_float(0.20)).0 as $upper_type,
					$name::from_float(0.50).0 as $upper_type,
					2,
				);
				assert_eq_error_rate!(
					($name::from_float(0.1) / $name::from_float(0.10)).0 as $upper_type,
					$name::from_float(1.0).0 as $upper_type,
					2,
				);
				assert_eq_error_rate!(
					($name::from_float(0.1) / $name::from_float(0.0)).0 as $upper_type,
					$name::from_float(1.0).0 as $upper_type,
					2,
				);

				// will not overflow
				assert_eq_error_rate!(
					($name::from_float(0.10) / $name::from_float(0.05)).0 as $upper_type,
					$name::from_float(1.0).0 as $upper_type,
					2,
				);
				assert_eq_error_rate!(
					($name::from_float(1.0) / $name::from_float(0.5)).0 as $upper_type,
					$name::from_float(1.0).0 as $upper_type,
					2,
				);
			}

			#[test]
			fn saturating_pow_works() {
				// x^0 == 1
				assert_eq!(
					$name::from_parts($max / 2).saturating_pow(0),
					$name::from_parts($max),
				);

				// x^1 == x
				assert_eq!(
					$name::from_parts($max / 2).saturating_pow(1),
					$name::from_parts($max / 2),
				);

				// x^2
				assert_eq!(
					$name::from_parts($max / 2).saturating_pow(2),
					$name::from_parts($max / 2).square(),
				);

				// x^2 .. x^16
				for n in 1..=16 {
					assert_eq!(
						$name::from_parts($max / 2).saturating_pow(n),
						$name::from_parts(($max as u128 / 2u128.pow(n as u32)) as $type),
					);
				}

				// 0^n == 0
				assert_eq!(
					$name::from_parts(0).saturating_pow(3),
					$name::from_parts(0),
				);

				// 1^n == 1
				assert_eq!(
					$name::from_parts($max).saturating_pow(3),
					$name::from_parts($max),
				);

				// (x < 1)^inf == 0 (where 2.pow(31) ~ inf)
				assert_eq!(
					$name::from_parts($max / 2).saturating_pow(2usize.pow(31)),
					$name::from_parts(0),
				);
			}

			#[test]
			fn saturating_reciprocal_mul_works() {
				// divide by 1
				assert_eq!(
					$name::from_parts($max).saturating_reciprocal_mul(<$type>::from(10u8)),
					10,
				);
				// divide by 1/2
				assert_eq!(
					$name::from_parts($max / 2).saturating_reciprocal_mul(<$type>::from(10u8)),
					20,
				);
				// saturate
				assert_eq!(
					$name::from_parts(1).saturating_reciprocal_mul($max),
					<$type>::max_value(),
				);
				// round to nearest
				assert_eq!(
					$name::from_percent(60).saturating_reciprocal_mul(<$type>::from(10u8)),
					17,
				);
				// round down
				assert_eq!(
					$name::from_percent(60).saturating_reciprocal_mul_floor(<$type>::from(10u8)),
					16,
				);
				// round to nearest
				assert_eq!(
					$name::from_percent(61).saturating_reciprocal_mul(<$type>::from(10u8)),
					16,
				);
				// round up
				assert_eq!(
					$name::from_percent(61).saturating_reciprocal_mul_ceil(<$type>::from(10u8)),
					17,
				);
			}

			#[test]
			fn saturating_truncating_mul_works() {
				assert_eq!(
					$name::from_percent(49).mul_floor(10 as $type),
					4,
				);
				let a: $upper_type = $name::from_percent(50).mul_floor(($max as $upper_type).pow(2));
				let b: $upper_type = ($max as $upper_type).pow(2) / 2;
				if $max % 2 == 0 {
					assert_eq!(a, b);
				} else {
					// difference should be less that 1%, IE less than the error in `from_percent`
					assert!(b - a < ($max as $upper_type).pow(2) / 100 as $upper_type);
				}
			}

			#[test]
			fn rational_mul_correction_works() {
				assert_eq!(
					super::rational_mul_correction::<$type, $name>(
						<$type>::max_value(),
						<$type>::max_value(),
						<$type>::max_value(),
						super::Rounding::NearestPrefDown,
					),
					0,
				);
				assert_eq!(
					super::rational_mul_correction::<$type, $name>(
						<$type>::max_value() - 1,
						<$type>::max_value(),
						<$type>::max_value(),
						super::Rounding::NearestPrefDown,
					),
					<$type>::max_value() - 1,
				);
				assert_eq!(
					super::rational_mul_correction::<$upper_type, $name>(
						((<$type>::max_value() - 1) as $upper_type).pow(2),
						<$type>::max_value(),
						<$type>::max_value(),
						super::Rounding::NearestPrefDown,
					),
					1,
				);
				// ((max^2 - 1) % max) * max / max == max - 1
				assert_eq!(
					super::rational_mul_correction::<$upper_type, $name>(
						(<$type>::max_value() as $upper_type).pow(2) - 1,
						<$type>::max_value(),
						<$type>::max_value(),
						super::Rounding::NearestPrefDown,
					),
					<$upper_type>::from((<$type>::max_value() - 1)),
				);
				// (max % 2) * max / 2 == max / 2
				assert_eq!(
					super::rational_mul_correction::<$upper_type, $name>(
						(<$type>::max_value() as $upper_type).pow(2),
						<$type>::max_value(),
						2 as $type,
						super::Rounding::NearestPrefDown,
					),
					<$type>::max_value() as $upper_type / 2,
				);
				// ((max^2 - 1) % max) * 2 / max == 2 (rounded up)
				assert_eq!(
					super::rational_mul_correction::<$upper_type, $name>(
						(<$type>::max_value() as $upper_type).pow(2) - 1,
						2 as $type,
						<$type>::max_value(),
						super::Rounding::NearestPrefDown,
					),
					2,
				);
				// ((max^2 - 1) % max) * 2 / max == 1 (rounded down)
				assert_eq!(
					super::rational_mul_correction::<$upper_type, $name>(
						(<$type>::max_value() as $upper_type).pow(2) - 1,
						2 as $type,
						<$type>::max_value(),
						super::Rounding::Down,
					),
					1,
				);
			}

			#[test]
			#[allow(unused)]
			fn const_fns_work() {
				const C1: $name = $name::from_percent(50);
				const C2: $name = $name::one();
				const C3: $name = $name::zero();
				const C4: $name = $name::from_parts(1);

				// deconstruct is also const, hence it can be called in const rhs.
				const C5: bool = C1.deconstruct() == 0;
			}

			#[test]
			fn compact_decoding_saturate_when_beyond_accuracy() {
				use num_traits::Bounded;
				use codec::Compact;

				let p = Compact::<$name>::decode(&mut &Compact(<$type>::max_value()).encode()[..])
					.unwrap();
				assert_eq!((p.0).0, $max);
				assert_eq!($name::from(p), $name::max_value());
			}

			#[allow(unused_imports)]
			use super::*;

			#[test]
			fn test_add_basic() {
				assert_eq!($name::from_parts(1) + $name::from_parts(1), $name::from_parts(2));
				assert_eq!($name::from_parts(10) + $name::from_parts(10), $name::from_parts(20));
			}

			#[test]
			fn test_basic_checked_add() {
				assert_eq!(
					$name::from_parts(1).checked_add(&$name::from_parts(1)),
					Some($name::from_parts(2))
				);
				assert_eq!(
					$name::from_parts(10).checked_add(&$name::from_parts(10)),
					Some($name::from_parts(20))
				);
				assert_eq!(
					$name::from_parts(<$type>::MAX).checked_add(&$name::from_parts(<$type>::MAX)),
					None
				);
				assert_eq!(
					$name::from_parts($max).checked_add(&$name::from_parts(1)),
					None
				);
			}

			#[test]
			fn test_basic_saturating_add() {
				assert_eq!(
					$name::from_parts(1).saturating_add($name::from_parts(1)),
					$name::from_parts(2)
				);
				assert_eq!(
					$name::from_parts(10).saturating_add($name::from_parts(10)),
					$name::from_parts(20)
				);
				assert_eq!(
					$name::from_parts(<$type>::MAX).saturating_add($name::from_parts(<$type>::MAX)),
					$name::from_parts(<$type>::MAX)
				);
			}

			#[test]
			fn test_basic_sub() {
				assert_eq!($name::from_parts(2) - $name::from_parts(1), $name::from_parts(1));
				assert_eq!($name::from_parts(20) - $name::from_parts(10), $name::from_parts(10));
			}

			#[test]
			fn test_basic_checked_sub() {
				assert_eq!(
					$name::from_parts(2).checked_sub(&$name::from_parts(1)),
					Some($name::from_parts(1))
				);
				assert_eq!(
					$name::from_parts(20).checked_sub(&$name::from_parts(10)),
					Some($name::from_parts(10))
				);
				assert_eq!($name::from_parts(0).checked_sub(&$name::from_parts(1)), None);
			}

			#[test]
			fn test_basic_saturating_sub() {
				assert_eq!(
					$name::from_parts(2).saturating_sub($name::from_parts(1)),
					$name::from_parts(1)
				);
				assert_eq!(
					$name::from_parts(20).saturating_sub($name::from_parts(10)),
					$name::from_parts(10)
				);
				assert_eq!(
					$name::from_parts(0).saturating_sub($name::from_parts(1)),
					$name::from_parts(0)
				);
			}

			#[test]
			fn test_basic_checked_mul() {
				assert_eq!(
					$name::from_parts($max).checked_mul(&$name::from_parts($max)),
					Some($name::from_percent(100))
				);
				assert_eq!(
					$name::from_percent(100).checked_mul(&$name::from_percent(100)),
					Some($name::from_percent(100))
				);
				assert_eq!(
					$name::from_percent(50).checked_mul(&$name::from_percent(26)),
					Some($name::from_percent(13))
				);
				assert_eq!(
					$name::from_percent(0).checked_mul(&$name::from_percent(0)),
					Some($name::from_percent(0))
				);
			}
		}
	};
}

macro_rules! implement_per_thing_with_perthousand {
	(
		$name:ident,
		$test_mod:ident,
		$pt_test_mod:ident,
		[$($test_units:tt),+],
		$max:tt,
		$type:ty,
		$upper_type:ty,
		$title:expr $(,)?
	) => {
		implement_per_thing! {
			$name, $test_mod, [ $( $test_units ),+ ], $max, $type, $upper_type, $title,
		}
		impl $name {
			/// Converts a percent into `Self`. Equal to `x / 1000`.
			///
			/// This can be created at compile time.
			pub const fn from_perthousand(x: $type) -> Self {
				Self(([x, 1000][(x > 1000) as usize] as $upper_type * $max as $upper_type / 1000) as $type)
			}
		}
		#[cfg(test)]
		mod $pt_test_mod {
			use super::$name;
			use crate::traits::Zero;

			#[test]
			fn from_perthousand_works() {
				// some really basic stuff
				assert_eq!($name::from_perthousand(00), $name::from_parts(Zero::zero()));
				assert_eq!($name::from_perthousand(100), $name::from_parts($max / 10));
				assert_eq!($name::from_perthousand(1000), $name::from_parts($max));
				assert_eq!($name::from_perthousand(2000), $name::from_parts($max));
			}

			#[test]
			#[allow(unused)]
			fn const_fns_work() {
				const C1: $name = $name::from_perthousand(500);
			}
		}
	}
}

#[test]
fn from_rational_with_rounding_works_in_extreme_case() {
	use Rounding::*;
	for &r in [Down, NearestPrefDown, NearestPrefUp, Up].iter() {
		Percent::from_rational_with_rounding(1, u64::max_value(), r).unwrap();
		Percent::from_rational_with_rounding(1, u32::max_value(), r).unwrap();
		Percent::from_rational_with_rounding(1, u16::max_value(), r).unwrap();
		Percent::from_rational_with_rounding(u64::max_value() - 1, u64::max_value(), r).unwrap();
		Percent::from_rational_with_rounding(u32::max_value() - 1, u32::max_value(), r).unwrap();
		Percent::from_rational_with_rounding(u16::max_value() - 1, u16::max_value(), r).unwrap();
		PerU16::from_rational_with_rounding(1, u64::max_value(), r).unwrap();
		PerU16::from_rational_with_rounding(1, u32::max_value(), r).unwrap();
		PerU16::from_rational_with_rounding(1, u16::max_value(), r).unwrap();
		PerU16::from_rational_with_rounding(u64::max_value() - 1, u64::max_value(), r).unwrap();
		PerU16::from_rational_with_rounding(u32::max_value() - 1, u32::max_value(), r).unwrap();
		PerU16::from_rational_with_rounding(u16::max_value() - 1, u16::max_value(), r).unwrap();
		Permill::from_rational_with_rounding(1, u64::max_value(), r).unwrap();
		Permill::from_rational_with_rounding(1, u32::max_value(), r).unwrap();
		Permill::from_rational_with_rounding(u64::max_value() - 1, u64::max_value(), r).unwrap();
		Permill::from_rational_with_rounding(u32::max_value() - 1, u32::max_value(), r).unwrap();
		Perbill::from_rational_with_rounding(1, u64::max_value(), r).unwrap();
		Perbill::from_rational_with_rounding(1, u32::max_value(), r).unwrap();
		Perbill::from_rational_with_rounding(u64::max_value() - 1, u64::max_value(), r).unwrap();
		Perbill::from_rational_with_rounding(u32::max_value() - 1, u32::max_value(), r).unwrap();
	}
}

#[test]
fn from_rational_with_rounding_breakage() {
	let n = 372633774963620730670986667244911905u128;
	let d = 512593663333074177468745541591173060u128;
	let q = Perquintill::from_rational_with_rounding(n, d, Rounding::Down).unwrap();
	assert!(q * d <= n);
}

#[test]
fn from_rational_with_rounding_breakage_2() {
	let n = 36893488147419103230u128;
	let d = 36893488147419103630u128;
	let q = Perquintill::from_rational_with_rounding(n, d, Rounding::Up).unwrap();
	assert!(q * d >= n);
}

implement_per_thing!(Percent, test_per_cent, [u32, u64, u128], 100u8, u8, u16, "_Percent_",);
implement_per_thing_with_perthousand!(
	PerU16,
	test_peru16,
	test_peru16_extra,
	[u32, u64, u128],
	65535_u16,
	u16,
	u32,
	"_Parts per 65535_",
);
implement_per_thing_with_perthousand!(
	Permill,
	test_permill,
	test_permill_extra,
	[u32, u64, u128],
	1_000_000u32,
	u32,
	u64,
	"_Parts per Million_",
);
implement_per_thing_with_perthousand!(
	Perbill,
	test_perbill,
	test_perbill_extra,
	[u32, u64, u128],
	1_000_000_000u32,
	u32,
	u64,
	"_Parts per Billion_",
);
implement_per_thing_with_perthousand!(
	Perquintill,
	test_perquintill,
	test_perquintill_extra,
	[u64, u128],
	1_000_000_000_000_000_000u64,
	u64,
	u128,
	"_Parts per Quintillion_",
);