คณิตศาสตร์#

การแนะนำ#

ส่วนของคณิตศาสตร์นั้นประกอบด้วยฟังก์ชันในตัวของ Python และโมดูล math แบบเต็มรูปแบบ ซึ่งมีให้ใช้งานโดยอัตโนมัติใน VEXcode AIM เครื่องมือเหล่านี้ช่วยให้คุณสามารถดำเนินการได้ทุกอย่าง ตั้งแต่การคำนวณเลขคณิตพื้นฐานไปจนถึงตรีโกณมิติขั้นสูง การปัดเศษ และการคำนวณลอการิทึม

ใช้ฟังก์ชันและค่าคงที่เหล่านี้ในการคำนวณตำแหน่ง มุม ระยะทาง และค่าตัวเลขอื่นๆ สำหรับหุ่นยนต์ของคุณ นอกจากนี้ คุณยังสามารถแปลงระหว่างองศาและเรเดียน ประเมินนิพจน์ และทำงานกับค่าพิเศษ เช่น อนันต์และ NaN ได้อีกด้วย

ด้านล่างนี้คือรายการฟังก์ชันทางคณิตศาสตร์ ค่าคงที่ และเครื่องมืออำนวยความสะดวกที่มีให้ใช้งาน:

ฟังก์ชันในตัว – เครื่องมือทางคณิตศาสตร์พื้นฐานที่มีให้ใช้งานใน Python

  • abs – ส่งคืนค่าสัมบูรณ์ของตัวเลข

  • round – ปัดเศษตัวเลขให้เหลือจำนวนทศนิยมตามที่กำหนด

  • min – ส่งคืนค่าที่น้อยที่สุดของค่าอินพุต

  • max – ส่งคืนค่าที่มากที่สุดของค่าที่ป้อนเข้ามา

  • sum – บวกค่าทั้งหมดใน iterable

  • divmod – คืนค่าผลหารและเศษเหลือเป็นทูเปิล

  • pow – ยกกำลังจำนวนหนึ่ง โดยอาจมีค่าสัมบูรณ์ด้วย

  • int – แปลงค่าเป็นจำนวนเต็ม

  • float – แปลงค่าเป็นเลขทศนิยม

ค่าคงที่ – ค่าที่กำหนดไว้ล่วงหน้าจากโมดูล math

  • math.pi – ค่าคงที่ π (พาย)

  • math.tau – ค่าคงที่เทา (2π)

  • math.e – จำนวนของออยเลอร์ ฐานของลอการิทึมธรรมชาติ

  • math.inf – อนันต์บวก

  • math.nan – ไม่ใช่ตัวเลข (NaN)

ตรีโกณมิติ – การคำนวณมุมและความสัมพันธ์ระหว่างด้านต่างๆ

  • math.sin – ค่าไซน์ของมุมในหน่วยเรเดียน

  • math.cos – โคไซน์ของมุมในหน่วยเรเดียน

  • math.tan – ค่าแทนเจนต์ของมุมในหน่วยเรเดียน

  • math.atan – อาร์คแทงเจนต์ของค่าในหน่วยเรเดียน

  • math.atan2 – อาร์คแทงเจนต์ของ y/x ในหน่วยเรเดียน โดยพิจารณาควอดแรนต์

  • math.asin – ค่าอาร์คไซน์ของค่าในหน่วยเรเดียน

  • math.acos – อาร์คโคไซน์ของค่าในหน่วยเรเดียน

  • math.degrees – แปลงเรเดียนเป็นองศา

  • math.radians – แปลงองศาเป็นเรเดียน

ไฮเปอร์โบลิก – ฟังก์ชันขั้นสูงที่เกี่ยวข้องกับตรีโกณมิติ

  • math.sinh – ไซน์ไฮเปอร์โบลิกของค่า

  • math.cosh – โคไซน์ไฮเปอร์โบลิกของค่า

  • math.tanh – ค่าแทนเจนต์ไฮเปอร์โบลิกของค่าหนึ่ง

  • math.asinh – ไซน์ไฮเปอร์โบลิกผกผัน

  • math.acosh – โคไซน์ไฮเปอร์โบลิกผกผัน

  • math.atanh – แทนเจนต์ไฮเปอร์โบลิกผกผัน

การปัดเศษและค่าสัมบูรณ์ – ปรับความแม่นยำหรือทิศทาง

  • math.ceil – ปัดเศษขึ้นเป็นจำนวนเต็มที่ใกล้ที่สุด

  • math.floor – ปัดเศษลงเป็นจำนวนเต็มที่ใกล้ที่สุด

  • math.trunc – ลบส่วนทศนิยมออก

  • math.fabs – ส่งคืนค่าสัมบูรณ์เป็น float

เลขยกกำลังและลอการิทึม – การยกกำลัง ราก และการคำนวณลอการิทึม

  • math.pow – ยกกำลังจำนวนหนึ่ง

  • math.sqrt – ส่งคืนค่ารากที่สอง

  • math.exp – คำนวณ e ยกกำลัง x

  • math.log – ลอการิทึมธรรมชาติ (ฐาน e)

  • math.log10 – ลอการิทึมฐาน 10

  • math.log2 – ลอการิทึมฐาน 2

  • math.factorial – แฟกทอเรียลของจำนวนเต็ม

  • math.expm1 – ผลลัพธ์ที่แม่นยำยิ่งขึ้นสำหรับ e^x - 1.

การคำนวณเลขทศนิยม – ตรวจสอบหรือแยกส่วนค่าเลขทศนิยม

  • math.modf – คืนค่าส่วนที่เป็นเศษส่วนและส่วนจำนวนเต็มของ float

  • math.frexp – แยกตัวเลขออกเป็นส่วนแมนทิสซาและเลขชี้กำลัง

  • math.fmod – เศษเหลือที่มีเครื่องหมายของตัวตั้งหาร

  • math.copysign – ส่งคืนค่าที่มีเครื่องหมายของค่าอื่น

  • math.ldexp – คำนวณ x * (2 ** exp).

การเปรียบเทียบและการประมาณค่า – ตรวจสอบค่าต่างๆ กับค่าความคลาดเคลื่อนหรือหมวดหมู่

  • math.isclose – ทดสอบว่าค่าสองค่าใกล้เคียงกันหรือไม่

  • math.isfinite – ตรวจสอบว่าจำนวนนั้นเป็นจำนวนจำกัดหรือไม่

  • math.isinf – ตรวจสอบว่าตัวเลขเป็นอนันต์หรือไม่

  • math.isnan – ตรวจสอบว่าค่าเป็น NaN หรือไม่

ค่าความคลาดเคลื่อนและค่าแกมมา – ฟังก์ชันทางคณิตศาสตร์พิเศษ

  • math.gamma – ฟังก์ชันแกมมา (แฟกทอเรียลทั่วไป)

  • math.lgamma – แกมมาลอการิทึม

  • math.erf – ฟังก์ชันข้อผิดพลาด

  • math.erfc – ฟังก์ชันข้อผิดพลาดเสริม

ฟังก์ชันในตัว#

Python มีฟังก์ชันในตัวหลายฟังก์ชันที่ช่วยให้คุณสามารถดำเนินการทางคณิตศาสตร์ภายในโปรเจ็กต์ของคุณได้

abs#

abs ส่งคืนค่าสัมบูรณ์ของตัวเลข โดยตัดเครื่องหมายลบออก

Usage:
abs(x)

พารามิเตอร์

คำอธิบาย

x

จำนวนเต็มหรือจำนวนทศนิยม

# Get the absolute value of -10
abs_result = abs(-10)
robot.screen.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)
robot.screen.print(round_int_result)

# round_int_result = 6

# Round 3.14159 to 2 decimal places
round_result = round(3.14159, 2)
robot.screen.print(round_result)

# 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)
robot.screen.print(min_result)

# min_result = 1

# Get the smallest value from a list
min_list_result = min([10, 4, 25, 1])
robot.screen.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)
robot.screen.print(max_result)

# max_result = 7

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

# max_list_result = 25

sum#

sum จะบวกค่าทั้งหมดใน iterable เข้าด้วยกัน โดยสามารถกำหนดค่าเริ่มต้นได้ (ไม่บังคับ)

Usage:
sum(sequence, start)

พารามิเตอร์

คำอธิบาย

sequence

รายการ, ทูเปิล หรือลำดับอื่นๆ ที่ประกอบด้วยตัวเลข

start

ไม่บังคับ ระบุค่าที่จะเพิ่มเข้าไปในผลรวม ค่าเริ่มต้นคือ 0

# Calculate the sum of a list of numbers
sum_result = sum([1, 2, 3, 4, 5])
robot.screen.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)
robot.screen.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)
robot.screen.print(divmod_result)

# divmod_result = (3, 1)

pow#

pow ยกกำลังตัวเลข และอาจทำการหารเอาเศษด้วยตัวเศษ (modulus) ด้วย

Usage:
pow(x, y, mod)

พารามิเตอร์

คำอธิบาย

x

เลขฐาน

y

เลขชี้กำลัง

mod

ไม่บังคับ ค่าโมดูลัส หากระบุ จะคืนค่า (x ** y) % mod

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

# pow_result = 8

# Calculate (5 ** 3) % 7
pow_mod_result = pow(5, 3, 7)
robot.screen.print(pow_mod_result)

# pow_mod_result = 6

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)
robot.screen.print(f"{price_int} coins")

# Output: 19 coins

# Convert a string into an integer to use in calculations
user_input = "55"
user_number = int(user_input)
robot.screen.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
robot.screen.print(apples_per_person)

# Output: 3.00

# Convert a string into a float to use in calculations
user_input = "23.4"
user_number = float(user_input)
robot.screen.print(user_number * 3)

# Output: 70.20

โมดูลคณิตศาสตร์#

โมดูล math ใน MicroPython มีเมธอดเพิ่มเติมสำหรับการคำนวณทางคณิตศาสตร์ทั่วไป เมธอดเหล่านี้รวมถึงการคำนวณตรีโกณมิติ ลอการิทึม และการดำเนินการทางตัวเลขอื่นๆ

โมดูล math ถูกนำเข้าโดยค่าเริ่มต้นใน VEXcode

Constants#

ค่าคงที่คือค่าที่กำหนดไว้ล่วงหน้าซึ่งจะคงที่ตลอดโครงการ สามารถนำไปใช้ในการคำนวณได้โดยไม่ต้องกำหนดหรือกำหนดค่าใดๆ เพิ่มเติม

พาย#

pi ให้ค่าคงที่ทางคณิตศาสตร์ π ซึ่งเป็นอัตราส่วนของเส้นรอบวงของวงกลมต่อเส้นผ่านศูนย์กลาง

Usage:
math.pi

# Calculate the area of a circle with radius 5 using pi
circle_area = math.pi * 5 * 5
robot.screen.print(circle_area)

# circle_area = 78.54

เทา#

tau ให้ค่าเท่ากับ 2π

Usage:
math.tau

# Calculate the circumference of a circle with radius
circumference = math.tau * 5
robot.screen.print(circumference)

# circumference = 31.42

อี#

e คือฐานของลอการิทึมธรรมชาติ

Usage:
math.e

# Calculate e raised to the power of 2
e_power = math.pow(math.e, 2)
robot.screen.print(e_power)

# e_power = 7.39

ข้อมูล#

inf จะให้ค่าอนันต์บวกในรูปแบบทศนิยม

Usage:
math.inf

# Check if infinity is infinite
inf_value = math.inf

if math.isinf(inf_value):
    robot.screen.print("Infinity")

# Prints "Infinity"

นาน#

nan หมายถึงค่าทศนิยมพิเศษสำหรับ “ไม่ใช่ตัวเลข” (NaN)

Usage:
math.nan

# Check if nan is Not a Number
nan_value = math.nan

if math.isnan(nan_value):
    robot.screen.print("Not a Number")

# Prints "Not a Number"

Trigonometry#

บาป#

sin คำนวณค่าไซน์ของมุมในหน่วยเรเดียนและส่งคืนค่าทศนิยม

Usage:
math.sin(x)

พารามิเตอร์

คำอธิบาย

x

ค่าทศนิยมหรือจำนวนเต็มที่แสดงมุมในหน่วยเรเดียน

# Calculate the sine of 30 degrees in radians
sine_result = math.sin(math.radians(30))
robot.screen.print(sine_result)

# sine_result = 0.50

คอส#

cos คำนวณค่าโคไซน์ของมุมในหน่วยเรเดียนและส่งคืนค่าทศนิยม

Usage:
math.cos(x)

พารามิเตอร์

คำอธิบาย

x

ค่าทศนิยมหรือจำนวนเต็มที่แสดงมุมในหน่วยเรเดียน

# Calculate the cosine of 60 degrees in radians
cosine_result = math.cos(math.radians(60))
robot.screen.print(cosine_result)

# cosine_result = 0.50

แทน#

tan คำนวณค่าแทนเจนต์ของมุมในหน่วยเรเดียนและส่งคืนค่าทศนิยม

Usage:
math.tan(x)

พารามิเตอร์

คำอธิบาย

x

ค่าทศนิยมหรือจำนวนเต็มที่แสดงมุมในหน่วยเรเดียน

# Calculate the tangent of 45 degrees in radians
tangent_result = math.tan(math.radians(45))
robot.screen.print(tangent_result)

# tangent_result = 1.00

อาตัน#

atan คำนวณค่าแทนเจนต์ผกผัน (อาร์คแทนเจนต์) ของตัวเลข และส่งคืนค่าทศนิยมที่แสดงมุมในหน่วยเรเดียน

Usage:
math.atan(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็ม

# Calculate the arc tangent of 1 in degrees
arc_tangent_result = math.degrees(math.atan(1.0))
robot.screen.print(arc_tangent_result)

# arc_tangent_result = 45.00

อะตัน2#

atan2 คำนวณค่าหลักของแทนเจนต์ผกผันของ y/x และส่งคืนค่าทศนิยมที่แสดงมุมในหน่วยเรเดียน

Usage:
math.atan2(y, x)

พารามิเตอร์

คำอธิบาย

y

ค่าทศนิยมหรือจำนวนเต็มที่แสดงค่าพิกัดแกน y

x

ค่าทศนิยมหรือจำนวนเต็มที่แสดงพิกัด x

# Calculate the inverse tangent of 1/1 in degrees
atan2_result = math.degrees(math.atan2(1.0, 1.0))
robot.screen.print(atan2_result)

# atan2_result = 45.00

อาสิน#

asin คำนวณค่าผกผันของไซน์ (arc sine) ของตัวเลข และส่งคืนค่า float ที่แสดงมุมในหน่วยเรเดียน

Usage:
math.asin(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็มระหว่าง -1 ถึง 1

# Calculate the arc sine of 0.5 in degrees
arc_sine_result = math.degrees(math.asin(0.5))
robot.screen.print(arc_sine_result)

# arc_sine_result = 30.00

อะคอส#

acos คำนวณค่าโคไซน์ผกผัน (อาร์คโคไซน์) ของตัวเลข และส่งคืนค่าทศนิยมที่แสดงมุมในหน่วยเรเดียน

Usage:
math.acos(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็มระหว่าง -1 ถึง 1

# Calculate the arc cosine of 0.5 in degrees
arc_cosine_result = math.degrees(math.acos(0.5))
robot.screen.print(arc_cosine_result)

# arc_cosine_result = 60.00

ปริญญา#

degrees แปลงมุมจากเรเดียนเป็นองศา

Usage:
math.degrees(x)

พารามิเตอร์

คำอธิบาย

x

ค่าทศนิยมหรือจำนวนเต็มที่แสดงมุมในหน่วยเรเดียน

# Convert pi radians to degrees
degrees_result = math.degrees(math.pi)
robot.screen.print(degrees_result)

# degrees_result = 180.00

เรเดียน#

radians แปลงมุมจากองศาเป็นเรเดียน

Usage:
math.radians(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็มที่แสดงค่ามุมในหน่วยองศา

# Convert 180 degrees to radians
radians_result = math.radians(180)
robot.screen.print(radians_result)

# radians_result = 3.14

Hyperbolics#

สินห์#

sinh คำนวณค่าไซน์ไฮเปอร์โบลิกของ x

Usage:
math.sinh(x)

พารามิเตอร์

คำอธิบาย

x

ค่าตัวเลขทศนิยมหรือจำนวนเต็มที่แสดงค่าที่ป้อนเข้ามา

# Calculate the hyperbolic sine of 1
sinh_result = math.sinh(1)
robot.screen.print(sinh_result)

# sinh_result = 1.18

ไม้กระบอง#

cosh คำนวณค่าโคไซน์ไฮเปอร์โบลิกของ x

Usage:
math.cosh(x)

พารามิเตอร์

คำอธิบาย

x

ค่าตัวเลขทศนิยมหรือจำนวนเต็มที่แสดงค่าที่ป้อนเข้ามา

# Calculate the hyperbolic cosine of 1
cosh_result = math.cosh(1)
robot.screen.print(cosh_result)

# cosh_result = 1.54

ตันห์#

tanh คำนวณค่าแทนเจนต์ไฮเปอร์โบลิกของ x

Usage:
math.tanh(x)

พารามิเตอร์

คำอธิบาย

x

ค่าตัวเลขทศนิยมหรือจำนวนเต็มที่แสดงค่าที่ป้อนเข้ามา

# Calculate the hyperbolic tangent of 1
tanh_result = math.tanh(1)
robot.screen.print(tanh_result)

# tanh_result = 0.76

อาสินห์#

asinh คำนวณค่าผกผันของไซน์ไฮเปอร์โบลิกของ x

Usage:
math.asinh(x)

พารามิเตอร์

คำอธิบาย

x

ค่าตัวเลขทศนิยมหรือจำนวนเต็มที่แสดงค่าที่ป้อนเข้ามา

# Calculate the inverse hyperbolic sine of 1
asinh_result = math.asinh(1)
robot.screen.print(asinh_result)

# asinh_result = 0.88

อะโคช#

acosh คำนวณค่าโคไซน์ไฮเปอร์โบลิกผกผันของ x

Usage:
math.acosh(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็มที่มากกว่าหรือเท่ากับ 1

# Calculate the inverse hyperbolic cosine of 2
acosh_result = math.acosh(2)
robot.screen.print(acosh_result)

# acosh_result = 1.32

อาตันห์#

atanh คำนวณค่าแทนเจนต์ไฮเปอร์โบลิกผกผันของ x

Usage:
math.atanh(x)

พารามิเตอร์

คำอธิบาย

x

ค่าทศนิยมระหว่าง -1 และ 1 (ไม่รวม -1 และ 1)

# Calculate the inverse hyperbolic tangent of 0.5
atanh_result = math.atanh(0.5)
robot.screen.print(atanh_result)

# atanh_result = 0.55

Rounding & Absolute Values#

เพดาน#

ceil จะปัดเศษตัวเลขขึ้นเป็นจำนวนเต็มที่ใกล้ที่สุด

Usage:
math.ceil(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็มที่จะปัดขึ้น

# Round 3.7 up to the nearest integer
ceil_result = math.ceil(3.7)
robot.screen.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)
robot.screen.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)
robot.screen.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)
robot.screen.print(fabs_result)

# fabs_result = 3.70

Exponents & Logarithms#

พลัง#

pow ยกกำลัง x ด้วย y และส่งคืนค่าทศนิยม แม้ว่าอินพุตทั้งสองจะเป็นจำนวนเต็มก็ตาม

Usage:
math.pow(x, y)

พารามิเตอร์

คำอธิบาย

x

ฐานเป็นจำนวนทศนิยมหรือจำนวนเต็ม

y

เลขชี้กำลังแบบทศนิยมหรือจำนวนเต็ม

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

# power_result = 8.00

รากที่สอง#

sqrt คำนวณรากที่สองของจำนวนและส่งคืนค่าทศนิยม แม้กระทั่งสำหรับกำลังสองสมบูรณ์

Usage:
math.sqrt(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็มที่ไม่ติดลบ

# Calculate the square root of 16
sqrt_result = math.sqrt(16)
robot.screen.print(sqrt_result)

# sqrt_result = 4.00

เอ็กซ์#

exp คำนวณค่าเลขยกกำลังของตัวเลขและส่งคืนค่าทศนิยม

Usage:
math.exp(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็ม

# Calculate e raised to the power of 1
exp_result = math.exp(1)
robot.screen.print(exp_result)

# exp_result = 2.72

บันทึก#

log คำนวณลอการิทึมธรรมชาติของตัวเลขและส่งคืนค่าทศนิยม

Usage:
math.log(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็มบวก

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

# log_result = 2.00

ล็อก10#

log10 คำนวณลอการิทึมฐาน 10 ของตัวเลขและส่งคืนค่าทศนิยม

Usage:
math.log10(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็มบวก

# Calculate the base-10 logarithm of 1000
log10_result = math.log10(1000)
robot.screen.print(log10_result)

# log10_result = 3.00

ล็อก2#

log2 คำนวณลอการิทึมฐาน 2 ของตัวเลขและส่งคืนค่าทศนิยม แม้ว่า x จะเป็นกำลังของ 2 ก็ตาม

Usage:
math.log2(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็มบวก

# Calculate the base-2 logarithm of 8
log2_result = math.log2(8)
robot.screen.print(log2_result)

# log2_result = 3.00

แฟกทอเรียล#

factorial จะคืนค่าแฟกทอเรียลของจำนวนเต็ม xซึ่งเป็นผลคูณของจำนวนเต็มบวกทั้งหมดจนถึง x

Usage:
math.factorial(x)

พารามิเตอร์

คำอธิบาย

x

จำนวนเต็มที่ไม่ติดลบ

# Calculate 5 factorial (5!)
factorial_result = math.factorial(5)
robot.screen.print(factorial_result)

# factorial_result = 120

เอ็กซ์พีเอ็ม1#

expm1 คำนวณ ex - 1ซึ่งมีความแม่นยำกว่าสำหรับ x ขนาดเล็ก

Usage:
math.expm1(x)

พารามิเตอร์

คำอธิบาย

x

ค่าเลขชี้กำลัง

# Compute expm1(1) (e^1 - 1)
expm1_result = math.expm1(1)
robot.screen.print(expm1_result)

# expm1_result = 1.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)
robot.screen.print(fractional_part)
robot.screen.next_row()
robot.screen.print(integer_part)

# fractional_part = 0.14
# integer_part = 3.00

เฟร็กซ์#

frexp แยกตัวเลขออกเป็นส่วนแมนทิสซาและเลขชี้กำลัง แล้วส่งคืนทูเปิล (mantissa, exponent)โดยที่แมนทิสซาเป็นเลขทศนิยม และเลขชี้กำลังเป็นจำนวนเต็ม

Usage:
math.frexp(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็มที่จะใช้ในการแยกวิเคราะห์

# Decompose 16 into its mantissa and exponent
mantissa, exponent = math.frexp(16)
robot.screen.print(mantissa)
robot.screen.next_row()
robot.screen.print(exponent)

# mantissa = 0.50
# exponent = 5

เอฟมอด#

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)
robot.screen.print(fmod_result)

# fmod_result = 1.00

ลายเซ็นสำเนา#

copysign ส่งคืน x ด้วยเครื่องหมายของ y.

Usage:
math.copysign(x, y)

พารามิเตอร์

คำอธิบาย

x

ค่าที่ต้องการแก้ไข

y

ค่าที่เครื่องหมายจะถูกคัดลอก

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

# copysign_result = 10.00

ldexp#

ldexp คำนวณ x * (2 ** exp)ซึ่งเทียบเท่ากับ x * 2exp{code}

Usage:
math.ldexp(x, exp)

พารามิเตอร์

คำอธิบาย

x

ค่าพื้นฐาน

exp

เลขชี้กำลัง

# Compute 3 * (2 ** 4)
ldexp_result = math.ldexp(3, 4)
robot.screen.print(ldexp_result)

# ldexp_result = 48.00

Comparison & Approximation#

ปิด#

isclose ตรวจสอบว่าตัวเลขสองตัวนั้นใกล้เคียงกันภายในค่าความคลาดเคลื่อนที่กำหนดหรือไม่

Usage:
math.isclose(a, b, rel_tol, abs_tol)

พารามิเตอร์

คำอธิบาย

a

ตัวเลขแรกที่จะนำมาเปรียบเทียบ

b

ตัวเลขที่สองที่จะนำมาเปรียบเทียบ

rel_tol

ตัวเลือกเสริม: ค่าความแตกต่างสูงสุดที่อนุญาตระหว่าง a และ b โดยสัมพันธ์กับขนาดของทั้งสองค่า ค่าเริ่มต้นคือ 1e-09 (น้อยมาก)

abs_tol

ตัวเลือกเสริม ค่าความคลาดเคลื่อนคงที่ มีประโยชน์เมื่อเปรียบเทียบตัวเลขที่ใกล้เคียงกับศูนย์ ค่าเริ่มต้นคือ 0.0

หมายเหตุ: หากระบุทั้ง rel_tol และ abs_tol ผลลัพธ์จะขึ้นอยู่กับเงื่อนไขที่ตรงตามเงื่อนไขก่อนเป็นอันดับแรก

# Check if 1.000000001 and 1.0 are close
# within the default tolerance
isclose_result = math.isclose(1.000000001, 1.0)
robot.screen.print(isclose_result)

# isclose_result = True

# Check if 0.0000001 and 0.0 are close
# using absolute tolerance
isclose_result = math.isclose(0.0000001, 0.0, abs_tol=1e-07)
robot.screen.print(isclose_result)

# isclose_result = True

# Check if 1000000.0 and 1000000.1 are close
# with a stricter tolerance
isclose_result = math.isclose(1000000.0, 1000000.1, rel_tol=1e-10)
robot.screen.print(isclose_result)

# isclose_result = False

ไม่มีที่สิ้นสุด#

isfinite ตรวจสอบว่าตัวเลขนั้นเป็นจำนวนจำกัดหรือไม่ เมธอดนี้ส่งคืนค่าบูลีน:

  • True – จำนวนนั้นมีค่าจำกัด

  • False – ตัวเลขนั้นเป็นอนันต์

Usage:
math.isfinite(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็มที่จะใช้ตรวจสอบ

# Check if 42 is a finite number (returns True)
is_finite_true = math.isfinite(42)
robot.screen.print(is_finite_true)

# is_finite_true = True

# Check if infinity is a finite number (returns False)
is_finite_false = math.isfinite(math.inf)
robot.screen.print(is_finite_false)

# is_finite_false = False

ไอซินฟ์#

isinf ตรวจสอบว่าตัวเลขนั้นเป็นอนันต์หรือไม่ เมธอดนี้ส่งคืนค่าบูลีน:

  • True – ตัวเลขนั้นเป็นอนันต์

  • False – จำนวนนั้นมีจำกัด

Usage:
math.isinf(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็มที่จะใช้ตรวจสอบ

# Check if infinity is an infinite number (returns True)
is_inf_true = math.isinf(math.inf)
robot.screen.print(is_inf_true)

# is_inf_true = True

# Check if 42 is an infinite number (returns False)
is_inf_false = math.isinf(42)
robot.screen.print(is_inf_false)

# is_inf_false = False

อิสนาน#

isnan ตรวจสอบว่าตัวเลขเป็น NaN (ไม่ใช่ตัวเลข) หรือไม่ เมธอดนี้จะส่งคืนค่าบูลีน:

  • True – ตัวเลขคือ NaN

  • False – ตัวเลขนี้เป็นตัวเลขที่ถูกต้อง

Usage:
math.isnan(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็มที่จะใช้ตรวจสอบ

# Check if NaN (Not a Number) is NaN (returns True)
is_nan_true = math.isnan(math.nan)
robot.screen.print(is_nan_true)

# is_nan_true = True

# Check if 42 is NaN (returns False)
is_nan_false = math.isnan(42)
robot.screen.print(is_nan_false)

# is_nan_false = False

Error and Gamma Calculations#

แกมมา#

gamma คำนวณฟังก์ชันแกมมาของ xซึ่งเป็นการขยายฟังก์ชันแฟกทอเรียลสำหรับจำนวนจริงและจำนวนเชิงซ้อน สำหรับจำนวนเต็ม n, gamma(n) = (n-1)!.

Usage:
math.gamma(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็มบวก

# Calculate the gamma function of 5 (equivalent to 4!)
gamma_result = math.gamma(5)
robot.screen.print(gamma_result)

# gamma_result = 24.00

lgamma#

lgamma คำนวณลอการิทึมธรรมชาติของฟังก์ชัน gamma

Usage:
math.lgamma(x)

พารามิเตอร์

คำอธิบาย

x

ตัวเลขทศนิยมหรือจำนวนเต็มบวก

# Calculate the natural logarithm of the
# gamma function of 5
lgamma_result = math.lgamma(5)
robot.screen.print(lgamma_result)

# lgamma_result = 3.18

เอิร์ฟ#

erf คำนวณฟังก์ชันข้อผิดพลาดของ x

Usage:
math.erf(x)

พารามิเตอร์

คำอธิบาย

x

ค่าตัวเลขทศนิยมหรือจำนวนเต็มที่แสดงค่าที่ป้อนเข้ามา

# Calculate the error function of 1
erf_result = math.erf(1)
robot.screen.print(erf_result)

# erf_result = 0.84

erfc#

erfc คำนวณฟังก์ชันข้อผิดพลาดเสริมของ xซึ่งกำหนดเป็น 1 - erf(x)

Usage:
math.erfc(x)

พารามิเตอร์

คำอธิบาย

x

ค่าตัวเลขทศนิยมหรือจำนวนเต็มที่แสดงค่าที่ป้อนเข้ามา

# Calculate the complementary error function of 1
erfc_result = math.erfc(1)
robot.screen.print(erfc_result)

# erfc_result = 0.16