Introduction

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

And thank you for the wide variety of Tribonatch and comments! Among them, I would like to introduce an article by @torifukukaiou who solved Tribonatch with the Elixir version. [I tried to solve the tribonacci sequence problem with Elixir (time limit 10 minutes)] (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 the Tribonacci sequence, and I was looking for a way to speed up the implementation in order to solve even more intense math problems in the future. Meanwhile, I learned that C language can be used with gem'RubyInline', and when I implemented it to touch C language a little, an arithmetic overflow error occurred.

About the flow until the occurrence of arithmetic overflow

I created a directory and rb file with the following command, opened VS Code and edited it.

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

Write 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
``````

Kakikaki the code ... (How do you write it in C ... ´ · ௰ · `) ← What did you do? (ﾟ Д ﾟ) I wrote it because it can be easy for the time being! !!

`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

``````

The above code is the same as the code implemented to solve the tribonatch sequence in Ruby. However, if implemented with this code, ** arithmetic overflow will occur and the calculation result will be negative **.

Commentary

You can write external code (C language) in Ruby code by writing require'inline' on the first line. The int type data type is defined for each int description on the 6th line. However, this gives an outlier with a negative display from the 36th answer in the Tribonacci sequence.

This phenomenon is called ** Arithmetic Overflow **. In C language, the processing system that becomes the minimum value LONG_MIN (-2147483648) when the maximum value of int INT_MAX (2147483647) is exceeded by 1 is being implemented. In this implementation, it seems that the 36th item exceeded INT_MAX (2147483647) and started from -2147483648.

To prevent arithmetic overflow, change from int to long this time. long maximum value: 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