# LONG2NUM vs LONG2FIX

**URL:** <https://rubytalk.org/t/long2num-vs-long2fix/37274>\
**Category:** ruby-talk\
**Created:** [30 April 2007 05:46 UTC](https://rubytalk.org/t/long2num-vs-long2fix/37274 "2007-04-30T05:46:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haoqi\_Haoqi](https://avatars.discourse-cdn.com/v4/letter/h/2acd7d/32.png) [@Haoqi\_Haoqi](https://rubytalk.org/u/Haoqi_Haoqi)\
**Post date:** [30 April 2007 05:46 UTC](https://rubytalk.org/t/long2num-vs-long2fix/37274/1 "2007-04-30T05:46:08Z")

</div>

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)  
> &nbsp;&nbsp;&nbsp;&nbsp;VALUE fix;  
> {  
> &nbsp;&nbsp;&nbsp;&nbsp;long i = FIX2LONG(fix);
> 
> &nbsp;&nbsp;&nbsp;&nbsp;if (i \< 0) i = -i;
> 
> &nbsp;&nbsp;&nbsp;&nbsp;return LONG2NUM(i);  
> }
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [30 April 2007 05:59 UTC](https://rubytalk.org/t/long2num-vs-long2fix/37274/2 "2007-04-30T05:59:32Z")

</div>

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)  
> > &nbsp;&nbsp;&nbsp;&nbsp;VALUE fix;  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;long i = FIX2LONG(fix);
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;if (i \< 0) i = -i;
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;return LONG2NUM(i);  
> > }
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Haoqi\_Haoqi](https://avatars.discourse-cdn.com/v4/letter/h/2acd7d/32.png) [@Haoqi\_Haoqi](https://rubytalk.org/u/Haoqi_Haoqi)\
**Post date:** [1 May 2007 05:47 UTC](https://rubytalk.org/t/long2num-vs-long2fix/37274/3 "2007-05-01T05:47:20Z")

</div>

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  
> &nbsp;&nbsp;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-forum.com/\](http://www.ruby-forum.com/%5C).
