libdar::infinint "power" method
Johnathan Burchill <[email protected]>
| Newsgroups | gmane.comp.sysutils.backup.dar.general |
|---|---|
| Message-ID | <[email protected]> |
Hi Denis,
I found the following method useful in KDar, and wondered whether others
might find use for it (or is it already implemented?):
libdar::infinint power(libdar::infinint base, libdar::infinint exponent)
{
libdar::infinint result(1);
//there must be a better way to handle negative exponents!!! i.e. return
//an error or an exception? For now, just return 0;
if (exponent < 0) return 0;
for (libdar::infinint i; i<exponent; ++i)
{
result = result * base;
}
return result;
}
Also wondering whether you can think of a more efficient way of taking an
infinint to some power.
I use it to convert an infinint from "Megabytes" to "bytes", for example.
e.g. I have a droplist of "B", "kB", "MB", "GB", "TB", from which the user
can select which unit they want the slice size to be measured in. The
items in the list are enumerated 0,1,2,3,4. So to pass the correct value
to libdar::op_create, I do
infinint slicesize(deci("650").computer()*power(infinint(1024),
infinint(droplist->currentItem())));
which would be 650*1024*1024 Bytes, since MB is Item 2.
That way I can store the infinint values as strings, so there is no limit
to the size of the slice (whereas my old way was to store the slize size
as integer (int) units of MB. i.e. not exactly capable of configuring
arbitrarily large slice sizes.
Cheers,
JB
--
Johnathan K. Burchill, Ph.D.
[email protected]
signature.asc
(application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (GNU/Linux) iD8DBQBABPlLrXMSOtiWrO4RAmyyAKCV/UUIn3E23Nj6HtSEfaZrwGtulACfXLAa RBBOTNA8sFNhqme+pE/Vxs4= =Mivo -----END PGP SIGNATURE-----