[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] u8vector to numbers bignum
From: |
Peter Bex |
Subject: |
Re: [Chicken-users] u8vector to numbers bignum |
Date: |
Thu, 28 May 2015 08:59:00 +0200 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
On Wed, May 27, 2015 at 03:53:39PM -0700, chi wrote:
> How would I convert a u8vector to a bignum?
>
> I'd like a good large random number, and there's srfi 27 for decent random
> sources, and there's 'numbers' for bignum support, but srfi-27 only produces
> fixnums or u8vectors.
I don't know anything about the srfi-27 egg, but after a quick look,
there seems to be an undocumented "srfi-27-numbers" module. Since srfi-27
depends on the "numbers" egg, this should probably do the trick.
> Logically I can't imagine a bignum isn't represented under
> the hood by a block of bytes somehow, so it should be easy to turn a random
> byte
> vector into a random bignum?
>
> numbers itself only provides (random ...) which uses rand(3), and that's not
> what I would call a good random number. Random sources don't really seem like
> something a bignum library should worry about, anyway.
Yeah, the numbers random implementation is shitty, which is why I
decided to omit it from my port to CHICKEN core (CHICKEN 5's random is
still fixnum only). Personally, I think it would be better if we simply
got rid of all "random" support in core, because there's no way this is ever
going to satisfy everyone. Besides, the standard C library functions are
deeply flawed, especially on some platforms like OS X.
> I could always do (number->string (u8vec_to_hex (random-u8vector #x20)) #x10)
> but having a double sized hexadecimal intermediate isn't terribly appealing,
> especially for a random number I might have to generate for hundreds of peers
> a
> second in a high volume scenario. (Okay now I really /am/ being optimistic.)
>
> Looking at the numbers source, it seems to be analagous to a vector whose
> first
> element is a magic number indicating it's a bignum, and the rest of the vector
> is the digits.
That's correct: it's a record object which has 'bignum as its type tag and
is followed by a string which contains the binary data. The first C_word of
that string is actually 1 or 0 indicating its sign (1 if negative).
Cheers,
Peter
signature.asc
Description: Digital signature
- [Chicken-users] u8vector to numbers bignum, chi, 2015/05/27
- Re: [Chicken-users] u8vector to numbers bignum,
Peter Bex <=
- Re: [Chicken-users] u8vector to numbers bignum, cowan, 2015/05/28
- Re: [Chicken-users] u8vector to numbers bignum, Michele La Monaca, 2015/05/28
- Re: [Chicken-users] u8vector to numbers bignum, Peter Bex, 2015/05/28
- Re: [Chicken-users] u8vector to numbers bignum, Michele La Monaca, 2015/05/28
- Re: [Chicken-users] u8vector to numbers bignum, John Cowan, 2015/05/28
- Re: [Chicken-users] u8vector to numbers bignum, chi, 2015/05/28
- Re: [Chicken-users] u8vector to numbers bignum, cowan, 2015/05/29
- Re: [Chicken-users] u8vector to numbers bignum, Stephen Eilert, 2015/05/29
Re: [Chicken-users] u8vector to numbers bignum, Alex Shinn, 2015/05/28
Re: [Chicken-users] u8vector to numbers bignum, Kon Lovett, 2015/05/28