LONG2NUM vs LONG2FIX

H

Haoqi Haoqi

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);
}
 
J

Joel VanderWerf

Haoqi said:
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
 
H

Haoqi Haoqi

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


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

Thank you~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,241
Messages
2,571,223
Members
47,856
Latest member
mmorais

Latest Threads

Top