[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

LONG2NUM vs LONG2FIX

Haoqi Haoqi

4/30/2007 5:46:00 AM

what's the difference about "LONG2NUM and LONG2FIX" in ruby c api?
why not "return LONG2FIX after FIX2LONG"??
thanks.

ruby source file(numeric.c):
/*
* call-seq:
* fix.abs -> aFixnum
*
* Returns the absolute value of <i>fix</i>.
*
* -12345.abs #=> 12345
* 12345.abs #=> 12345
*
*/

static VALUE
fix_abs(fix)
VALUE fix;
{
long i = FIX2LONG(fix);

if (i < 0) i = -i;

return LONG2NUM(i);
}

--
Posted via http://www.ruby-....

2 Answers

Joel VanderWerf

4/30/2007 6:00:00 AM

0

Haoqi Haoqi wrote:
> what's the difference about "LONG2NUM and LONG2FIX" in ruby c api?

LONG2FIX will truncate into a Fixnum (a 31 bit signed integer). LONG2NUM
will convert to Bignum (arbitrary precision integer) if necessary.

> why not "return LONG2FIX after FIX2LONG"??
> thanks.

(-2**30).class # => Fixnum
(2**30).class # => Bignum
(-2**30).abs.class # => Bignum

> ruby source file(numeric.c):
> /*
> * call-seq:
> * fix.abs -> aFixnum
> *
> * Returns the absolute value of <i>fix</i>.
> *
> * -12345.abs #=> 12345
> * 12345.abs #=> 12345
> *
> */
>
> static VALUE
> fix_abs(fix)
> VALUE fix;
> {
> long i = FIX2LONG(fix);
>
> if (i < 0) i = -i;
>
> return LONG2NUM(i);
> }
>

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Haoqi Haoqi

5/1/2007 5:47:00 AM

0

Joel VanderWerf wrote:
> Haoqi Haoqi wrote:
>> what's the difference about "LONG2NUM and LONG2FIX" in ruby c api?
>
> LONG2FIX will truncate into a Fixnum (a 31 bit signed integer). LONG2NUM
> will convert to Bignum (arbitrary precision integer) if necessary.
>
>> why not "return LONG2FIX after FIX2LONG"??
>> thanks.
>
> (-2**30).class # => Fixnum
> (2**30).class # => Bignum
> (-2**30).abs.class # => Bignum

Thank you~

--
Posted via http://www.ruby-....