T
Tom Werner
I've been hand-translating some Ruby code into C for an extension and
have come across the need to create and return a Bignum from the
extension. I see that there is a macro INT2NUM that will produce a
Bignum from long or long long, but what is the best way to create an
arbitrarily sized Bignum from a byte array packed as:
[a, b, c] -> (a * (256 ** 0)) + (b * (256 ** 1)) + (c * (256 ** 2))
in other words, reversing the array and flatting it would produce the
correct binary representation of the number.
I tried creating a Fixnum with INT2FIX(0) and then using rb_funcalls to
incrementally construct a Bignum, but that approach 1) didn't seem to
work past WORD sized numbers and 2) seems like it would be awfully slow.
--
Tom Preston-Werner
* Libraries:
Chronic (chronic.rubyforge.org)
God (god.rubyforge.org)
Fuzed (fuzed.rubyforge.org)
* Site:
rubyisawesome.com
have come across the need to create and return a Bignum from the
extension. I see that there is a macro INT2NUM that will produce a
Bignum from long or long long, but what is the best way to create an
arbitrarily sized Bignum from a byte array packed as:
[a, b, c] -> (a * (256 ** 0)) + (b * (256 ** 1)) + (c * (256 ** 2))
in other words, reversing the array and flatting it would produce the
correct binary representation of the number.
I tried creating a Fixnum with INT2FIX(0) and then using rb_funcalls to
incrementally construct a Bignum, but that approach 1) didn't seem to
work past WORD sized numbers and 2) seems like it would be awfully slow.
--
Tom Preston-Werner
* Libraries:
Chronic (chronic.rubyforge.org)
God (god.rubyforge.org)
Fuzed (fuzed.rubyforge.org)
* Site:
rubyisawesome.com