Skip to content

Commit 498a324

Browse files
authored
Numbers/Types Q and A added to User Guide FAQ (#543)
1 parent ca64f0c commit 498a324

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

user-guide/modules/ROOT/pages/faq.adoc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,38 @@ cpp_int prime = 1659187003930582880291185165038566829283520340642102923205105260
11251125
11261126
----
11271127

1128+
. *I need efficient memory storage for a real-time simulation. The use case is small counts, for example the number of carrier task forces operating in one ocean at any one time - a number which will never exceed 10 let alone 256?*
1129+
+
1130+
A full sized int or even a byte is comically oversized for this use case. You could use the standard: `std::uint8_t num_groups;`, however you might like to add some range-safety. Consider the following code as it's memory footprint is exacty 3 bytes:
1131+
+
1132+
[source,cpp]
1133+
----
1134+
#include <cstdint>
1135+
#include <boost/endian/arithmetic.hpp>
1136+
#include <boost/numeric/conversion/cast.hpp>
1137+
1138+
struct OceanStatus {
1139+
boost::endian::little_uint8_t carrier_groups; // 1 byte on disk
1140+
boost::endian::little_uint8_t submarine_groups;
1141+
boost::endian::little_uint8_t air_wings;
1142+
1143+
void set_carrier_groups(int x) {
1144+
1145+
// Guarantee 0–10 range at runtime
1146+
if (x < 0 || x > 10)
1147+
throw std::out_of_range("carrier group count must be 0-10");
1148+
1149+
// Safe cast to uint8_t
1150+
carrier_groups = boost::numeric_cast<std::uint8_t>(x);
1151+
}
1152+
};
1153+
1154+
----
1155+
1156+
. *What library should I look at to help pack really small integers (say 2 to 4 bits each) into the minimum number of bytes possible?*
1157+
+
1158+
The libary to evaluate is boost:dynamic-bitset[], it gives you stable bit indexing, easy clearing/writing slices, guaranteed predictable ordering, and portability across CPU architectures. In particular, check out the function `to_block_range` for exporting packed blocks. This should be easier - and safer - than hand-rolling your own masks and shifts.
1159+
11281160
== Other Languages
11291161

11301162
. *Have developers written applications in languages such as Python that have successfully used the Boost libraries?*
@@ -1908,6 +1940,9 @@ boost:multiprecision[] is designed to seamlessly extend the built-in numeric typ
19081940
+
19091941
You should conside using boost:nowide[], a library that makes Windows Unicode handling sane! This library provides UTF-8 versions of `fopen`, `std::cout`, as well as file I/O and environmental variables. It also provides the crucial automatic conversion to UTF-16 when calling Windows APIs. You might also find boost:locale[] useful if internationalization is required, boost:static-string[], and the `small_vector` type of boost:container[] for efficient short-string handling.
19101942

1943+
. *Storage space is of the essence in the real-time app I am building, are there Boost libraries that can provide _small_ integers, specifying 8 or 16 bits and no more storage than that is allocated?*
1944+
+
1945+
For byte level efficiency look at boost:endian[], this library gives you precise control over both integer size and alignment. It also provides cross-platfrom compatibility, if that is important. For example, `boost::endian::little_int8_t smallCount;` will always be 8 bit. boost:multiprecision[] does provide for declaring small integers as low as 8 bits, but is not so memory efficient. If your goal is to save overall memory, not just per-number bytes, check out boost:container[], as it supports small-vector optimization and no heap allocations.
19111946

19121947
== See Also
19131948

0 commit comments

Comments
 (0)