Ruby's "1101" .to_i (2) does not convert decimal numbers to decimal numbers.

This is a common mistake in Ruby articles. Before we get into the main subject, let's sort out some terms.

First

a = 17

Think of an ordinary program. This assigns the local variable a to the ** number ** 17. There are many articles that describe this 17 as a" number ".

"Number" and "number" are different. ("Number" and "number" are basically synonymous [^ nu], so I will not distinguish them in this article.)

A number is a symbol used to write a number (however, negative numbers and decimal points are not included in the number). In the decimal notation system, the symbols from 0 to 9 are "numbers". In the above example, 1 and 7 in the source code are numbers, but what is assigned to a is a number, not a number.

[^ nu]: I say "number system" but not so much "numerical system". It can be said that "numerical value" is used for each specific number, or is often used in the computer world.

In non-academic language use, numbers are sometimes called "numbers" in metaphorical expressions. Looking at the financial statements, he said, "You got a good number." But when we're talking about programming, we want to make a clear distinction between numbers and numbers.

Now, let's get into the main subject.

What happens to the number written "1101" in decimal notation in decimal notation? You can easily find it with the following program.

p "1101".to_i(2) # => 13

The answer is "13".

This program uses a method called String # to_i. It converts a string object to an integer object, but you can specify the radix with an argument (the radix is ​​N in N-ary notation). The default value is 10. In this case, since 2 is given as an argument, the character string of the receiver is regarded as a binary number and an integer object is created.

It's good so far, but it seems that quite a lot of people look at this " 1101 ".to_i (2) and misunderstand that they are "converting a binary number to a decimal number". Well, it's no wonder it's misunderstood. Because, when I run the above program, "13" is displayed. "13" is definitely a decimal number corresponding to the binary number "1101".

So what exactly is "wrong"? Consider again the words "N-ary" and "N-ary". The N-ary system is a scaled number system that uses N types of numbers. Numeral system is a notation method for numbers. N-ary numbers are numbers based on N-ary notation.

However, the return value of the to_i method is not a string. It's an Integer object. Therefore, it is not an "arranged number". It is neither a decimal number nor a decimal number. So to_i is not a" method to convert to decimal numbers ".

The Integer object is expressed in binary notation inside the processing system, but that is for the convenience of the processing system and is not known to the user [^ nh].

[^ nh]: In principle, it would be nice to have a Ruby processing system that handles Integer objects in an internal representation based on decimal notation (though no one would have created it), and CRuby or something based on Integer in binary notation. Even though we are dealing with it, it is not so simple that the internal representation of 13 is a bit string of 1101.

So why did the above program display the decimal number "13"? That's how it works.

" 1101 ".to_i (2) returns an Integer object corresponding to the number 13. The reason I wrote "the number 13" now is because we have a habit of expressing numbers in decimal unless otherwise specified. It sounds awkward, but in some way his Integer object isn't a side-by-side of "1" and "3".

Now, that Integer object is passed as an argument to the p method. The p method converts the given object into a character string with the inspect method and outputs it. The inspect method of Integer converts itself to a String object called a ** decimal string ** (that is, a decimal number) if no arguments are given. So, in this case, you get the string " 13 ".

In short, it wasn't converted to a decimal number by to_i, but it was converted to a decimal number when it was displayed.

Recommended Posts

Ruby's "1101" .to_i (2) does not convert decimal numbers to decimal numbers.
Do not use floats and doubles to calculate decimal numbers
Convert numbers to Roman numerals in Ruby
Convert an array of strings to numbers
[Rails] Bootstrap form-control does not apply to date_select