Hi, sorry by the delay and thanks for the reply,

2010/7/15 Paul A. Bristow <pbristow@hetp.u-net.com>

 

 

From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Murilo Adriano Vasconcelos
Sent: Wednesday, July 14, 2010 9:04 PM
To: boost-users@lists.boost.org
Subject: [Boost-users] About naming of integer log in base 2

 

Hi all,

 

I implemented a function [0] wich computes the logarithm in base two of integers and want to know what naming is most appropriated:

 

ilog2

or simply

log2

 

 

My instinct is for log2 so it might be specialised for floating point too.

 

Glancing at your code, I note you don’t explicitly check that type T is integer (but perhaps is_signed does that?)

 

But I feel that 
 
 *     If `value` is equal to 0, the value returned is undefined

 

may be evil in some peoples opinion.

 

Of course it is always a vexed question what to do for the corner cases like this.  I am sure that this sort of issue will raise its ugly head for other functions.

That is because I made count_leading_zeros [1] function to be conditionally compiled: if the compiler is GCC, the functions makes the use of GCC's builtin functions [2] for performance. The function used
in count_leading_zeros is __builtin_clz(l and ll) wich is undefined for the value 0.

The initial intent was log2(0) = -1, you can check if your compiler isn't GCC that count_leading_zeros(T(0)) is always sizeof(T) * 8 (all bits are leading zeros). 
But one of the intents of this implementation is performance so, using compiler optimized functions is desired.

So the function to be analyzed in this case is count_leading_zeros.

[1] - http://svn.boost.org/svn/boost/sandbox/SOC/2010/bits_and_ints/boost/integer/count_leading_zeros.hpp
[2] - http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Other-Builtins.html
 

 

You might like to consider making problem cases ‘policy controlled’.

 

In the Boost.Math package John Maddock went to the trouble of providing a way of letting users controlling what happens for cases like this.

 

(He retrofitted policies, so you don’t have to do it immediately – but doing it will focus your mind on the possible corner cases).

 

If you read the math docs, perhaps the pdf at

 

http://boost.cowic.de/rc/pdf/math.pdf

 

or the download from source,

 

and see the section on Policies you will see what it being suggested, and some examples.

 

You might return zero, or ? or throw an exception?

 

You can consult and see if there is a consensus on the default policy. 

Thanks for the link.
I have to check this so I can discuss about.
 

 

At least it may reduce the discussion (Boosters are really good at that ;-) on what *should* happen, if you can say “You *can* make it do what you want.”

 

Paul

 

 

---

Paul A. Bristow

Prizet Farmhouse

Kendal, UK   LA8 8AB

+44 1539 561830, mobile +44 7714330204

pbristow@hetp.u-net.com


Best,
--
Murilo Adriano Vasconcelos
http://murilo.wordpress.com