Good evening I go to a certain school I just graduated last month, but I am outputting what I learned. Last time, I made a method that can be used with [Ruby] strings, so this time I would like to touch on operators!
Operators are symbols used for programming calculations and comparisons. Please remember this because it's a mess
I said, but most of them were done in elementary school + or something, so you don't have to be prepared
+ | addition |
- | subtraction |
* | multiplication |
/ | division |
% | Surplus |
** | Exponentiation |
Math::PI | Constant to match pi |
Math.sqrt | Find the square root |
I've been learning programming for half a year, but I didn't use it, so please keep it in the corner of your head.
=> nil
irb(main):115:0> 1 + 1
=> 2
irb(main):116:0> 1 - 1
=> 0
irb(main):117:0> 1 * 2
=> 2
irb(main):118:0> 4 / 2
=> 2
Needless to say, the function of four arithmetic operations
irb(main):119:0> 3 / 4
=> 0
By the way, the decimal point is not displayed Because this number is an Integer class, and Integer represents a number as an integer.
irb(main):122:0> n = 3 / 4
=> 0
irb(main):123:0> n.class
=> Integer
When I checked the assigned n with class, I was able to confirm that it was "Integer".
It was described as a remainder in the above figure, but what is the story? The surplus is the remainder of the division.
irb(main):128:0> 9 % 7
=> 2
9/7 = 1 ・ ・ ・ Because it is a remainder of 2, 2 is displayed.
Exponentiation is simply exponentiation.
irb(main):131:0> 4 ** 2
=> 16
If you look at this, you can see 4 * 4 = 16
Math::PI
Math :: PI is pi But you can do finer calculations than 3.14
irb(main):138:0> 3 * 3 *3.14
=> 28.26
irb(main):139:0> 3 * 3 * Math::PI
=> 28.274333882308138
As you can see, the second decimal place is different
Math.sqrt This is a method to find the square root
Can be used with Math.sqrt (numbers)
irb(main):140:0> Math.sqrt(4)
=> 2.0
I asked for 4 but 2 for √4
irb(main):143:0> Math.sqrt(2)
=> 1.4142135623730951
People, people, people, nostalgic
I'll use operators a lot in the future so let's remember But it's enough to keep in mind the power below the power
Recommended Posts