[Ruby] A story of encountering an arithmetic overflow that you should never encounter in Ruby

3 minute read

Introduction

The author who mainly uses Ruby encountered an arithmetic overflow and learned a lesson, so I will share the situation that occurred and countermeasures. ** ※ There is no math problem this time… **

And thank you for all the various Tribonacci comments! Among them, I will introduce the article of @torifukukaiou who solved Tribonacci with Elixir version. [Solving Tribonacci sequence problem with Elixir (10 minutes time limit)] (https://qiita.com/torifukukaiou/items/d5a6639edf541539ac3a?utm_campaign=email&utm_content=link&utm_medium=email&utm_source=public_mention)

What were you trying to do?

A few days have passed since the last battle with Tribonacci sequences, and I’ve been looking for ways to speed up implementation to solve even more intense mathematical problems. In the meantime, I found that I could use C language with gem’RubyInline’, and when I implemented it so that I could touch C language for a while, an arithmetic overflow error occurred.

About the flow until the occurrence of arithmetic overflow

Created a directory and rb file with the following command and opened VS Code for editing.

$ cd Desktop
$ mkdir Ruby_with_c
$ cd Ruby_with_c
$ touch tribonacci.rb
$ bundle init
$ code.

Describe gem’RubyInline’,’~> 3.12’,’>= 3.12.4’ in Gemfile and bundle install

Gemfile


# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}"}

# gem "rails"
gem'RubyInline','~> 3.12','>= 3.12.4'
$ bundle install

The code is shimmering… (How do you write C language…‘·௰·`) ← What did you do? (゚Д゚me) Anyway, it’s easy to write, so I wrote it! !!

tribonacci.rb


require'inline'

class Tribonacci
  inline do |builder|
    builder.c "
      int add(int a, int b, int c){
        int d,i = 3;
        while (i<50){
          d = a + b + c;
          a = b;
          b = c;
          c = d;
          i++;
        }
        return c;
      }
    "
  end
end

puts Tribonacci.new.add(1,3,7)

It is the same as the code implemented in the above code to solve the Tribonacci sequence in Ruby. However, if this code is used, arithmetic overflow will occur and the calculation result will be minus.

Comment

External code (C language) can be written in Ruby code by writing require’inline’ in the first line. The 6th line int description defines the int data type for each. However, in this case, the 36th answer of the Tribonacci sequence gives an abnormal value with a negative display.

35th answer1831177831 36th answer-926906165

This phenomenon is called arithmetic overflow. When the value exceeds INT_MAX (2147483647), which is the maximum value of int, becomes the minimum value LONG_MIN (-2147483648), the processing system will be implemented in C language. In this implementation, it seems that it started from -2147483648, exceeding INT_MAX (2147483647) at the 36th.

To prevent arithmetic overflow, change int to long this time. long maximum: 9223372036854775807

tribonacci.rb


require'inline'

class Tribonacci
  inline do |builder|
    builder.c "
      long add(long a, long b, long c){
        long d,i = 3;
        while (i<50){
          d = a + b + c;
          a = b;
          b = c;
          c = d;
          i++;
        }
        return c;
      }
    "
  end
end

puts Tribonacci.new.add(1,3,7)

By changing to long, 363368061131 comes out. By the way, the number of digits of Ruby integer is automatically expanded, so there is no arithmetic overflow. So when I was just touching Ruby, I never encountered arithmetic overflow. Thanks for the encounter! !!

in conclusion

I was just learning a little about the C language, and came across an error that I wouldn’t encounter in Ruby. Currently I could only introduce gem’RubyInline’, but in the future I would like to create an extension library of C language and implement high-speed processing to tackle intense mathematical problems.

bonus

If you have an interesting math problem, please leave a comment below! !! I learned a lot about the process of solving, and I got into math problems. Based on the spirit of Connecting the dots, we believe that our immersive days will one day shine and we will continue to do our best!

Tags:

Updated: