CPython / Decimal and bit length of value.

jak nospam at please.ty
Sun Sep 5 07:41:01 EDT 2021


Il 03/09/2021 22:09, Nacnud Nac ha scritto:
> Hi,
> Is there a quick way to get the number of bits required to store the value in a Decimal class?
> What obvious thing am I missing? I'm working with really large integers, say, in the order of 5_000_000 of ASCII base 10 digits.
> It seems the function mpd_sizeinbase would be a nice thing to be able to call.....
> It'd be nice to just be able to tell the "size of data", or something like that, that was stored in the *data? I note there is len "field" in the structure mpd_t
> Sorry for the dumb question....
> Thanks,Duncan
> 
to semplfy the example I'll use the value 1000000:

value="1000000"

exponent in base 10 is len(value) - 1 # (1 * 10^6)

now need change from base 10 to base 2: newexp = 6 / log(2) # 19 (more 
or less)

you will need newexp + 1 bits to represent the number: 2^20 = 1.048.576

hope helps




More information about the Python-list mailing list