Search code examples
rubyfactorial

Why am I getting two very different results from these two functions


this is copied from http://www.zenspider.com/ZSS/Products/RubyInline/Readme.html, the "home" of rubyinline, adding/moding as indicated in the comments

require 'rubygems' #added this, doesn't run otherwise
require 'inline'
class MyTest

  def factorial(n)
    f = 1
    n.downto(2) { |x| f *= x }
    f
  end

  inline do |builder|
    builder.c "
    long factorial_c(int max) {
      int i=max, result=1;
      while (i >= 2) { result *= i--; }
      return result;
    }"
  end
end

#t = MyTest.new()  # removed this
#factorial_5 = t.factorial(5) # removed this

asdf = MyTest.new # added this
puts "ruby native factorial: " + asdf.factorial(16).to_s # added this
puts "inline factorial: " + asdf.factorial_c(16).to_s # added this

when I run it, I get

ruby testfact.rb

ruby native factorial: 20922789888000

inline factorial: 2004189184

the ruby version is correct; I don't know what the inline version is (other than incorrect).

I'm running ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]; I installed rubyinline like this: sudo gem install RubyInline

I tried changing the "int"'s to "long"'s in the C code, but it didn't make a diff. (I'm not a C programmer, if that wasn't already apparent.)

Finally, I noticed that if I run them both with 12 or less as the argument, they both yield the same answer. Things get weird at 13 and above.

TIA


Solution

  • In the version of C you have is a long 32 bits or 64 bits? A long is not always larger than an int in C.