Re: libdar::infinint "power" method
Denis Corbin <[email protected]>
| Newsgroups | gmane.comp.sysutils.backup.dar.general |
|---|---|
| Message-ID | <[email protected]> |
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Johnathan Burchill wrote:
| Hi Denis,
Hello Johnathan,
|
| I found the following method useful in KDar, and wondered whether others
| might find use for it (or is it already implemented?):
it is not implemented in libdar. But I will add it. Note that infinint
cannot be negative, thus the test is not necessary.
And I will add another "power" method beside the one you describe,
infinint power(infinint base, U_I exponent);
to have the "for" loop manipulating system unsigned integers, which will
be much faster than using infinint.
|
| 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 would have done this way too, maybe there are some more optimized way
of calculating (in particular when the exponent is a power of two), but
I don't think the cost to detect theses special cases where optimization
is possible worth it (there is not much power of two compared to the set
of integer numbers they are included in).
|
| 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.
I would have written it this way, that the same, but maybe more easy to
read:
infinint slicesize = deci("650").computer()
~ * power(1024, droplist->currentItem());
|
| 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
Cheers,
Denis.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFABbnEpC5CI8gYGlIRAu62AKDGVLqi47jJv1g66P+tPoYfcKVoFwCgv10n
C0fO0Duw5rJZaFY6vch6cUc=
=JCvJ
-----END PGP SIGNATURE-----
-------------------------------------------------------
This SF.net email is sponsored by: Perforce Software.
Perforce is the Fast Software Configuration Management System offering
advanced branching capabilities and atomic changes on 50+ platforms.
Free Eval! http://www.perforce.com/perforce/loadprog.html