]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/units/example/information.cpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / libs / units / example / information.cpp
CommitLineData
7c673cae
FG
1// Boost.Units - A C++ library for zero-overhead dimensional analysis and
2// unit/quantity manipulation and conversion
3//
4// Copyright (C) 2014 Erik Erlandson
5//
6// Distributed under the Boost Software License, Version 1.0. (See
7// accompanying file LICENSE_1_0.txt or copy at
8// http://www.boost.org/LICENSE_1_0.txt)
9
10//#include <boost/units/systems/information.hpp>
11
12/**
13\file
14
15\brief information.cpp
16
17\details
18Demonstrate information unit system.
19
20Output:
21@verbatim
22bytes= 1.25e+08 B
23bits= 8e+06 b
24nats= 4605.17 nat
251024 bytes in a kibi-byte
268.38861e+06 bits in a mebi-byte
270.000434294 hartleys in a milli-nat
28entropy in bits= 1 b
29entropy in nats= 0.693147 nat
30entropy in hartleys= 0.30103 Hart
31entropy in shannons= 1 Sh
32entropy in bytes= 0.125 B
33@endverbatim
34**/
35
36#include <cmath>
37#include <iostream>
38using std::cout;
39using std::endl;
11fdf7f2 40using std::log;
7c673cae
FG
41
42#include <boost/units/quantity.hpp>
43#include <boost/units/io.hpp>
44#include <boost/units/conversion.hpp>
45namespace bu = boost::units;
46using bu::quantity;
47using bu::conversion_factor;
48
49// SI prefixes
50#include <boost/units/systems/si/prefixes.hpp>
51namespace si = boost::units::si;
52
53// information unit system
54#include <boost/units/systems/information.hpp>
55using namespace bu::information;
56
57// Define a function for the entropy of a bernoulli trial.
58// The formula is computed using natural log, so the units are in nats.
59// The user provides the desired return unit, the only restriction being that it
60// must be a unit of information. Conversion to the requested return unit is
61// accomplished automatically by the boost::units library.
62template <typename Sys>
11fdf7f2 63constexpr
7c673cae
FG
64quantity<bu::unit<bu::information_dimension, Sys> >
65bernoulli_entropy(double p, const bu::unit<bu::information_dimension, Sys>&) {
66 typedef bu::unit<bu::information_dimension, Sys> requested_unit;
67 return quantity<requested_unit>((-(p*log(p) + (1-p)*log(1-p)))*nats);
68}
69
70int main(int argc, char** argv) {
71 // a quantity of information (default in units of bytes)
72 quantity<info> nbytes(1 * si::giga * bit);
73 cout << "bytes= " << nbytes << endl;
74
75 // a quantity of information, stored as bits
76 quantity<hu::bit::info> nbits(1 * si::mega * byte);
77 cout << "bits= " << nbits << endl;
78
79 // a quantity of information, stored as nats
80 quantity<hu::nat::info> nnats(2 * si::kilo * hartleys);
81 cout << "nats= " << nnats << endl;
82
83 // how many bytes are in a kibi-byte?
84 cout << conversion_factor(kibi * byte, byte) << " bytes in a kibi-byte" << endl;
85
86 // how many bits are in a mebi-byte?
87 cout << conversion_factor(mebi * byte, bit) << " bits in a mebi-byte" << endl;
88
89 // how many hartleys are in a milli-nat?
90 cout << conversion_factor(si::milli * nat, hartley) << " hartleys in a milli-nat" << endl;
91
92 // compute the entropy of a fair coin flip, in various units of information:
93 cout << "entropy in bits= " << bernoulli_entropy(0.5, bits) << endl;
94 cout << "entropy in nats= " << bernoulli_entropy(0.5, nats) << endl;
95 cout << "entropy in hartleys= " << bernoulli_entropy(0.5, hartleys) << endl;
96 cout << "entropy in shannons= " << bernoulli_entropy(0.5, shannons) << endl;
97 cout << "entropy in bytes= " << bernoulli_entropy(0.5, bytes) << endl;
98
99 return 0;
100}