VBA users tried using Python / R: basic arithmetic operations

Introduction

I am a VBA user who started studying machine learning. As a memorandum, I would like to summarize the Python / R grammar while comparing it with VBA.

table of contents

-Basic Arithmetic -Four arithmetic operations -Exponentiation and integer division -Sign Inversion and Absolute Value -Summary -List -Whole program

Basic arithmetic

Four arithmetic operations

First, there are four arithmetic operations (addition, subtraction, multiplication, division). The four arithmetic operators +, -,*, and/are common to all languages.

Python

Python3


print( 1 + 2 )   #Addition
print( 3 - 4 )   #Subtraction
print( 5 * 6 )   #Multiply
print( 7 / 8 )   #division

In Python2, if the divisor (divided number) and the divisor (divided number) are integers, dividing by / results in rounded division.

R

R


print( 1 + 2 )   #Addition
print( 3 - 4 )   #Subtraction
print( 5 * 6 )   #Multiply
print( 7 / 8 )   #division

VBA

VBA


Debug.Print 1 + 2   'Addition
Debug.Print 3 - 4   'Subtraction
Debug.Print 5 * 6   'Multiply
Debug.Print 7 / 8   'division

Exponentiation and integer division

Next is the calculation of exponentiation (power) and integer division (quotient (integer quotient) and remainder (remainder)). Operators differ depending on the language.

Python

Python3


print( 2 ** 3 )   #Exponentiation
print( 5 // 3 )   #Integer quotient
print( 5 % 3 )    #Surplus
print( divmod(5, 3) ) #Integer quotient and remainder

The Python divmod function returns tuples. The result of divmod (5, 3) is tuple(1, 2).

R

R


print( 2 ^ 3 )   #Exponentiation
print( 5 %/% 3 ) #Integer quotient
print( 5 %% 3 )  #Surplus

VBA

VBA


Debug.Print 2 ^ 3   'Exponentiation
Debug.Print 5 \ 3   'Integer quotient
Debug.Print 5 Mod 3 'Surplus
Debug.Print Int(5 / 3) 'Integer quotient (get the integer part with the Int function)

The backslash "" in the VBA integer quotient calculation "5 \ 3" is a half-width yen mark.

Sign inversion and absolute value

Finally, sign inversion and absolute value calculation.

Python

Python3


print( -2 )       #Sign inversion
print( abs(-2) )  #Absolute value

R

R


print( -2 )      #Sign inversion
print( abs(-2) ) #Absolute value

VBA

VBA


Debug.Print -2      'Sign inversion
Debug.Print Abs(-1) 'Absolute value

Summary

List

List the operators and functions used in each language. For comparison, the calculation in EXCEL is also shown.

Calculation Example Python R VBA EXCEL result
Addition 1+2 1 + 2 1 + 2 1 + 2 =1+2 3
Subtraction 3-4 3 - 4 3 - 4 3 - 4 =3-4 -1
Multiply 5*6 5 * 6 5 * 6 5 * 6 =5*6 30
division 7/8 7 / 8 7 / 8 7 / 8 =7/8 0.875
Exponentiation 2^3 2 ** 3 2 ^ 3 2 ^ 3 =2^3 8
Integer quotient 5/3 5 // 3 5 %/% 3 5 ¥ 3 =QUOTIENT(5,3) 1
Surplus 5/3 5 % 3 5 %% 3 5 Mod 3 =MOD(5,3) 2
divmod(5, 3) (1, 2)
Sign inversion -2 -2 -2 -2 =-2 -2
Absolute value |-2| abs(-2) abs(-2) Abs(-2) =ABS(-2) 2

The "¥" in VBA is a circle mark (a full-width circle mark is included, but it is actually a half-width circle mark).

Whole program

The whole program used for reference is shown.

Python

Python3


#Four arithmetic operations
print( 1 + 2 )   #Addition
print( 3 - 4 )   #Subtraction
print( 5 * 6 )   #Multiply
print( 7 / 8 )   #division

#Exponentiation and integer division
print( 2 ** 3 )  #Exponentiation
print( 5 // 3 )  #Integer quotient
print( 5 % 3 )   #Surplus
print( divmod(5, 3) ) #Integer quotient and remainder

#Sign inversion and absolute value
print( -2 )      #Sign inversion
print( abs(-2) ) #Absolute value

R

R


#Four arithmetic operations
print( 1 + 2 )   #Addition
print( 3 - 4 )   #Subtraction
print( 5 * 6 )   #Multiply
print( 7 / 8 )   #division

#Exponentiation and integer division
print( 2 ^ 3 )   #Exponentiation
print( 5 %/% 3 ) #Integer quotient
print( 5 %% 3 )  #Surplus

#Sign inversion and absolute value
print( -2 )      #Sign inversion
print( abs(-2) ) #Absolute value

VBA

VBA


Sub test()

'Four arithmetic operations
Debug.Print 1 + 2   'Addition
Debug.Print 3 - 4   'Subtraction
Debug.Print 5 * 6   'Multiply
Debug.Print 7 / 8   'division

'Exponentiation and integer division
Debug.Print 2 ^ 3   'Exponentiation
Debug.Print 5 \ 3   'Integer quotient
Debug.Print 5 Mod 3 'Surplus
Debug.Print Int(5 / 3) 'Integer quotient (get the integer part with the Int function)

'Sign inversion and absolute value
Debug.Print -2      'Sign inversion
Debug.Print Abs(-1) 'Absolute value

End Sub

The backslash "" in the VBA integer quotient calculation "5 \ 3" is a half-width yen mark.

reference

Recommended Posts

VBA users tried using Python / R: basic arithmetic operations
VBA users tried using Python / R: logical operations and comparison operations
VBA user tried using Python / R: basic grammar
VBA user tried using Python / R: Matrix
VBA user tried using Python / R: Iterative processing
VBA user tried using Python / R: conditional branching
VBA user tried using Python / R: string manipulation
VBA user tried using Python / R: String manipulation (continued)
Four arithmetic operations in python
[Python] Using OpenCV with Python (Basic)
[Python] I tried using OpenPose
I tried using Thonny (Python / IDE)
[Python] I tried using YOLO v3
I tried using Bayesian Optimization in Python
Setting up Basic authentication using Python @Lambda
I tried using UnityCloudBuild API from Python
I tried to touch Python (basic syntax)
R code compatible sheet for Python users
Mayungo's Python Learning Episode 5: I tried to do four arithmetic operations with numbers
[Python / DynamoDB / boto3] List of operations I tried
vprof --I tried using the profiler for Python
I tried object detection using Python and OpenCV
I tried using mecab with python2.7, ruby2.3, php7
I tried reading a CSV file using Python
I tried using the Datetime module by Python
[Python / Chrome] Basic settings and operations for scraping
Pharmaceutical company researchers summarized database operations using Python
Summary of Excel operations using OpenPyXL in Python