수학#

소개#

수학 모듈에는 내장 파이썬 함수와 VEXcode 123에서 자동으로 사용 가능한 math 모듈이 모두 포함되어 있습니다. 이러한 도구를 사용하면 기본적인 산술 연산부터 고급 삼각 함수, 반올림, 로그 연산까지 모든 작업을 수행할 수 있습니다.

이 함수와 상수를 사용하여 로봇의 위치, 각도, 거리 및 기타 수치 값을 계산할 수 있습니다. 또한 도(degrees)와 라디안(radians) 간 변환, 수식 평가, 무한대(infinity) 및 NaN과 같은 특수 값 처리도 가능합니다.

다음은 사용 가능한 수학 함수, 상수 및 유틸리티 목록입니다.

내장 함수 – 파이썬에 포함된 일반적인 수학 도구입니다.

  • abs – 숫자의 절댓값을 반환합니다.

  • round – 숫자를 지정된 소수 자릿수로 반올림합니다.

  • min – 입력값 중 가장 작은 값을 반환합니다.

  • max – 입력 값 중 가장 큰 값을 반환합니다.

  • sum – 반복 가능한 모든 값을 더합니다.

  • divmod – 몫과 나머지를 튜플로 반환합니다.

  • pow – 선택적으로 모듈러스를 사용하여 숫자를 거듭제곱합니다.

  • int – 값을 정수로 변환합니다.

  • float – 값을 부동 소수점 숫자로 변환합니다.

상수 – math 모듈에서 미리 정의된 값입니다.

  • math.pi – 상수 π(파이).

  • math.tau – 상수 tau(2π).

  • math.e – 오일러 수, 자연로그의 밑.

삼각법 – 각도를 계산하고 변 사이의 관계를 파악합니다.

  • math.sin – 라디안의 각도의 사인.

  • math.cos – 라디안의 각도의 코사인.

  • math.tan – 라디안의 각도의 탄젠트.

  • math.atan – 라디안 값의 아크탄젠트.

  • math.atan2 – 사분면을 고려하여 라디안의 y/x의 아크탄젠트.

  • math.asin – 라디안 값의 아크사인.

  • math.acos – 라디안 값의 아크코사인.

  • math.degrees – 라디안을 도로 변환합니다.

  • math.radians – 도를 라디안으로 변환합니다.

반올림 및 절댓값 – 정밀도 또는 방향을 조정합니다.

  • math.ceil – 가장 가까운 정수로 반올림합니다.

  • math.floor – 가장 가까운 정수로 내림합니다.

  • math.trunc – 소수 부분을 제거합니다.

  • math.fabs – 절대값을 부동소수점으로 반환합니다.

지수와 로그 - 거듭제곱, 제곱근, 로그 계산

  • math.pow – 숫자를 거듭제곱합니다.

  • math.sqrt – 제곱근을 반환합니다.

  • math.exp – e의 x제곱을 계산합니다.

  • math.log – x의 로그를 계산합니다.

  • math.log10 – x의 밑이 10인 로그를 계산합니다.

부동 소수점 연산 – 부동 소수점 값을 검사하거나 분해합니다.

  • math.modf – 부동 소수점의 소수 부분과 정수 부분을 반환합니다.

  • math.frexp – 숫자를 가수와 지수로 분해합니다.

  • math.fmod – 피제수의 부호가 있는 나머지.

  • math.copysign – 다른 값의 부호를 가진 값을 반환합니다.

  • math.ldexp – x * (2 ** exp) 계산합니다.

내장 함수#

파이썬은 프로젝트 내에서 수학 연산을 수행할 수 있도록 여러 가지 내장 함수를 제공합니다.

abs#

abs 숫자의 절댓값을 반환하며, 음수 부호를 제거합니다.

Usage:
abs(x)

매개변수

설명

x

정수 또는 실수.

# Get the absolute value of -10
abs_result = abs(-10)
console.print(abs_result)
# abs_result = 10

round#

round 숫자를 지정된 소수점 자릿수까지 반올림합니다.

Usage:
round(x, ndigits)

매개변수

설명

x

정수 또는 실수.

ndigits

선택 사항입니다. 소수점 이하 자릿수를 지정합니다. 기본값은 0입니다.

# Round 5.7 to the nearest integer
round_int_result = round(5.7)
console.print(round_int_result)
# round_int_result = 6

# Round 3.14159 to 2 decimal places
round_result = round(3.14159, 2)
console.print(round_result, precision=2)
# round_result = 3.14

min#

min 여러 인자 또는 반복 가능한 객체 중에서 가장 작은 값을 반환합니다.

Usage:
min(arg1, arg2, …) or min(sequence)

매개변수

설명

arg1, arg2, …

비교할 숫자들.

sequence

숫자가 포함된 리스트, 튜플 또는 기타 시퀀스.

# Get the smallest number from 3, 7, and 1
min_result = min(3, 7, 1)
console.print(min_result)
# min_result = 1

# Get the smallest value from a list
min_list_result = min([10, 4, 25, 1])
console.print(min_list_result)
# min_list_result = 1

max#

max 여러 인자 또는 반복 가능한 객체 중에서 가장 큰 값을 반환합니다.

Usage:
max(arg1, arg2, …) or max(sequence)

매개변수

설명

arg1, arg2, …

비교할 숫자들.

sequence

숫자가 포함된 리스트, 튜플 또는 기타 시퀀스.

# Get the largest number from 3, 7, and 1
max_result = max(3, 7, 1)
console.print(max_result)
# max_result = 7

# Get the largest value from a list
max_list_result = max([10, 4, 25, 1])
console.print(max_list_result)
# max_list_result = 25

sum#

sum 하고, 선택적으로 시작 값을 지정하여 반복 가능한 객체의 모든 값을 더합니다.

Usage:
sum(sequence, start)

매개변수

설명

sequence

숫자가 포함된 리스트, 튜플 또는 기타 시퀀스.

start

선택 사항입니다. 합계에 더할 값입니다. 기본값은 0입니다.

# Calculate the sum of a list of numbers
sum_result = sum([1, 2, 3, 4, 5])
console.print(sum_result)
# sum_result = 15

# Calculate the sum of a list with a starting value of 10
sum_with_start = sum([1, 2, 3], 10)
console.print(sum_with_start)
# sum_with_start = 16

divmod#

divmod 나눗셈 연산의 몫과 나머지를 포함하는 튜플을 반환합니다.

Usage:
divmod(a, b)

매개변수

설명

a

배당금.

b

제수.

# Perform integer division and remainder of 10 / 3
divmod_result = divmod(10, 3)
console.print(divmod_result)
# divmod_result = (3, 1)

pow#

pow 숫자를 거듭제곱하고 선택적으로 나머지 연산을 수행합니다.

Usage:
pow(x, y, mod)

매개변수

설명

x

기본 숫자.

y

지수.

mod

선택 사항입니다. 나머지 값입니다. 제공된 경우 (x ** y) % mod 을 반환합니다.

# Calculate 2 raised to the power of 3
pow_result = pow(2, 3)
console.print(pow_result)
# pow_result = 8

int#

int 숫자 또는 문자열을 정수로 변환합니다. 문자열에서 정수로 변환할 때 진법 변환도 지원합니다.

Usage:
int(x, base)

매개변수

설명

x

변환할 숫자, 문자열 또는 기타 객체입니다.

base

선택 사항입니다. 변환에 사용할 진법입니다. 기본값은 10입니다.

# Convert a float to an integer to get rid of decimals
price = 19.99
price_int = int(price)
console.print("{} coins".format(price_int))
# Output: 19 coins

# Convert a string into an integer to use in calculations
user_input = "55"
user_number = int(user_input)
console.print(user_number * 2)
# Output: 110

float#

float 은 숫자 또는 문자열을 부동 소수점 숫자로 변환합니다.

Usage:
float(x)

매개변수

설명

x

변환할 숫자, 문자열 또는 기타 객체입니다.

# Convert division result to a float
num_apples = 6
num_people = 2
apples_per_person = float(num_apples) / num_people
console.print(apples_per_person)
# Output: 3.0

# Convert a string into a float to use in calculations
user_input = "23.4"
user_number = float(user_input)
console.print(user_number * 3)
# Output: 70.2

수학 모듈#

파이썬의 math 모듈은 일반적인 수학 계산을 수행하기 위한 추가 메서드를 제공합니다. 이러한 메서드에는 삼각 함수, 로그 함수 및 기타 수치 연산이 포함됩니다.

VEXcode에서는 math 모듈이 기본적으로 임포트됩니다.

Constants#

상수는 프로젝트 기간 동안 고정된 상태로 유지되는 미리 정의된 값입니다. 상수는 별도의 정의나 할당 없이 계산에 사용할 수 있습니다.

파이#

pi 원의 둘레와 지름의 비율인 수학 상수 π를 나타냅니다.

Usage:
math.pi

# Calculate the area of a circle with radius 5 using pi
circle_area = math.pi * 5 * 5
console.print(circle_area, precision=2)
# circle_area = 78.54

타우#

tau 2π의 값을 나타냅니다.

Usage:
math.tau

# Calculate the circumference of a circle with radius 5
circumference = math.tau * 5
console.print(circumference, precision=2)
# circumference = 31.42

이자형#

e 자연로그의 밑을 나타냅니다.

Usage:
math.e

# Calculate e raised to the power of 2
e_power = math.pow(math.e, 2)
console.print(e_power, precision=2)
# e_power = 7.39

Trigonometry#

죄#

sin 함수는 라디안 단위 각도의 사인 값을 계산하여 부동 소수점 값으로 반환합니다.

Usage:
math.sin(x)

매개변수

설명

x

라디안 단위의 각도를 나타내는 실수 또는 정수입니다.

# Calculate the sine of 
# 30 degrees in radians
result = math.sin(math.radians(30))
console.print(result, precision=2)
# result = 0.50

코사인#

cos 라디안 단위 각도의 코사인 값을 계산하여 부동 소수점 값을 반환합니다.

Usage:
math.cos(x)

매개변수

설명

x

라디안 단위의 각도를 나타내는 실수 또는 정수입니다.

# Calculate the cosine of 
# 60 degrees in radians
result = math.cos(math.radians(60))
console.print(result, precision=2)
# result = 0.50

탠 껍질#

tan 함수는 라디안 단위 각도의 탄젠트 값을 계산하여 정수를 반환합니다.

Usage:
math.tan(x)

매개변수

설명

x

라디안 단위의 각도를 나타내는 실수 또는 정수입니다.

# Calculate the tangent of 
# 45 degrees in radians
result = math.tan(math.radians(45))
console.print(result)
# result = 1

아신#

asin 숫자의 역사인(아크사인) 값을 계산하고 라디안 단위의 각도를 나타내는 정수를 반환합니다.

Usage:
math.asin(x)

매개변수

설명

x

-1과 1 사이의 부동 소수점 또는 정수.

# Calculate the arc sine 
# of 0.5 in degrees
result = math.degrees(math.asin(0.5))
console.print(result)
# result = 30

아코스#

`acos 는 숫자의 역코사인(아크코사인) 값을 계산하고, 그 값을 라디안 단위의 각도로 나타낸 정수를 반환합니다.

Usage:
math.acos(x)

매개변수

설명

x

-1과 1 사이의 부동 소수점 또는 정수.

# Calculate the arc cosine 
# of 0.5 in degrees
result = math.degrees(math.acos(0.5))
console.print(result)
# result = 60

아탄#

atan 함수는 숫자의 역탄젠트(호탄젠트) 값을 계산하고 라디안 단위의 각도를 나타내는 정수를 반환합니다.

Usage:
math.atan(x)

매개변수

설명

x

실수 또는 정수.

# Calculate the arc tangent 
# of 1 in degrees
arc_tan = math.atan(1.0)
result = math.degrees(arc_tan)
console.print(result)
# result = 45

아탄2#

atan2 y/x의 역탄젠트의 주값을 계산하고 라디안 단위의 각도를 나타내는 부동 소수점을 반환합니다.

Usage:
math.atan2(y, x)

매개변수

설명

y

y좌표를 나타내는 실수 또는 정수입니다.

x

x좌표를 나타내는 실수 또는 정수입니다.

# Calculate the inverse tangent 
# of 1/1 in degrees
atan2 = math.atan2(1.0, 1.0)
result = math.degrees(atan2)
console.print(result)
# result = 45

학위#

degrees 은 각도를 라디안에서 도로 변환하고 정수를 반환합니다.

Usage:
math.degrees(x)

매개변수

설명

x

라디안 단위의 각도를 나타내는 실수 또는 정수입니다.

# Convert pi radians to degrees
degrees_result = math.degrees(math.pi)
console.print(degrees_result)
# degrees_result = 180

라디안#

radians 각도를 도에서 라디안으로 변환합니다.

Usage:
math.radians(x)

매개변수

설명

x

각도를 도 단위로 나타내는 실수 또는 정수입니다.

# Convert 180 degrees to radians
radians_result = math.radians(180)
console.print(radians_result, precision=2)
# radians_result = 3.14

Rounding & Absolute Values#

천장#

ceil 은 숫자를 가장 가까운 정수로 올림합니다.

Usage:
math.ceil(x)

매개변수

설명

x

반올림할 부동 소수점 또는 정수입니다.

# Round 3.7 up to the nearest integer
ceil_result = math.ceil(3.7)
console.print(ceil_result)
# ceil_result = 4

바닥#

floor 은 숫자를 가장 가까운 정수로 내림합니다.

Usage:
math.floor(x)

매개변수

설명

x

반올림할 부동 소수점 또는 정수입니다.

# Round 3.7 down to the nearest integer
floor_result = math.floor(3.7)
console.print(floor_result)
# floor_result = 3

잘린 부분#

trunc 은 반올림 없이 숫자의 소수 부분을 제거합니다.

Usage:
math.trunc(x)

매개변수

설명

x

소수점 이하 자릿수가 잘릴 수 있는 값입니다.

# Remove the decimal part of 3.7
trunc_result = math.trunc(3.7)
console.print(trunc_result)
# trunc_result = 3

팹스#

fabs 은 숫자의 절댓값을 부동 소수점으로 반환합니다.

Usage:
math.fabs(x)

매개변수

설명

x

실수 또는 정수.

# Get the absolute value of -3.7
fabs_result = math.fabs(-3.7)
console.print(fabs_result, precision=1)
# fabs_result = 3.7

Exponents & Logarithms#

통나무#

log 은 숫자의 로그 값을 계산하고 부동 소수점 값을 반환합니다.

Usage:
math.log(x, base)

매개변수

설명

x

양의 부동 소수점 또는 정수.

base

선택 사항입니다. 사용할 로그 밑입니다. 기본값은 자연 로그입니다.

# Calculate the natural logarithm
# (base e) of 7.389056
log_result = math.log(7.389056)
console.print(log_result)
# log_result = 2

# Calculate the log base 2 of 8
log_result = math.log(8, 2)
console.print(log_result)
# log_result = 3

로그10#

log10 숫자의 밑이 10인 로그 값을 계산하고 부동 소수점 값을 반환합니다.

Usage:
math.log10(x)

매개변수

설명

x

양의 부동 소수점 또는 정수.

# Calculate the base-10 logarithm of 100
log10_result = math.log10(100)
console.print(log10_result)
# log10_result = 2

파워#

pow 은 x를 y제곱하고 두 입력값이 모두 정수이더라도 부동소수점을 반환합니다.

Usage:
math.pow(x, y)

매개변수

설명

x

부동소수점 또는 정수형을 밑으로 합니다.

y

부동 소수점 또는 정수 지수.

# Calculate 2 raised to the power of 3
power_result = math.pow(2, 3)
console.print(power_result)

# power_result = 8

제곱근#

sqrt 은 숫자의 제곱근을 계산하고 완전제곱수인 경우에도 부동소수점을 반환합니다.

Usage:
math.sqrt(x)

매개변수

설명

x

음수가 아닌 부동소수점 숫자 또는 정수.

# Calculate the square root of 25
sqrt_result = math.sqrt(25)
console.print(sqrt_result)
# sqrt_result = 5.0

exp#

exp 숫자의 지수를 계산하고 부동 소수점을 반환합니다.

Usage:
math.exp(x)

매개변수

설명

x

실수 또는 정수.

# Calculate e raised to the power of 1
exp_result = math.exp(1)
console.print(exp_result, precision=2)
# exp_result = 2.72

Floating Point Operations#

모드#

modf 숫자를 소수 부분과 정수 부분으로 분해하고 튜플 (fractional part, integer part)모두 부동 소수점으로 반환합니다.

Usage:
math.modf(x)

매개변수

설명

x

분해할 부동 소수점 또는 정수입니다.

# Decompose 3.14159 into fractional and integer parts
fractional_part, integer_part = math.modf(3.14159)
console.print(fractional_part, precision=2)
console.new_line()
console.print(integer_part)
# fractional_part = 0.14
# integer_part = 3

프렉스#

frexp 숫자를 가수와 지수로 분해하고 가수가 부동 소수점이고 지수가 정수인 튜플 (mantissa, exponent)을 반환합니다.

Usage:
math.frexp(x)

매개변수

설명

x

분해할 부동 소수점 또는 정수입니다.

# Decompose 16 into its mantissa and exponent
mantissa, exponent = math.frexp(16)
console.print(mantissa, precision=2)
console.new_line()
console.print(exponent)
# mantissa = 0.50
# exponent = 5

fmod#

fmod 피제수의 부호를 유지하면서 나눗셈의 나머지를 반환합니다(x).

Usage:
math.fmod(x, y)

매개변수

설명

x

배당금.

y

제수.

# Calculate remainder of 10 / 3
# that preserves the sign of 10
fmod_result = math.fmod(10, 3)
console.print(fmod_result)
# fmod_result = 1

저작권 표시#

copysign y 의 부호를 가진 x를 반환합니다.

Usage:
math.copysign(x, y)

매개변수

설명

x

수정할 값입니다.

y

부호가 복사될 값입니다.

# Return -10 with the sign of 3 (positive)
copysign_result = math.copysign(-10, 3)
console.print(copysign_result)
# copysign_result = 10

ldexp#

ldexp x * (2 ** exp)계산하는데, 이는 x * 2exp 과 같습니다.

Usage:
math.ldexp(x, exp)

매개변수

설명

x

기본값.

exp

지수.

# Compute 3 * (2 ** 4)
ldexp_result = math.ldexp(3, 4)
console.print(ldexp_result)
# ldexp_result = 48