Matemātika#
Ievads#
Math ietver gan iebūvētas Python funkcijas, gan pilnu math moduli, kas ir automātiski pieejams VEXcode VR. Šie rīki ļauj veikt visu, sākot no pamata aritmētikas līdz pat sarežģītākām trigonometrijas, noapaļošanas un logaritmiskām darbībām.
Izmantojiet šīs funkcijas un konstantes, lai aprēķinātu robota pozīcijas, leņķus, attālumus un citas skaitliskās vērtības. Varat arī konvertēt starp grādiem un radiāniem, novērtēt izteiksmes un strādāt ar īpašām vērtībām, piemēram, bezgalību un NaN.
Zemāk ir pieejamo matemātisko funkciju, konstantu un utilītu saraksts:
Iebūvētās funkcijas — izplatīti matemātikas rīki, kas iekļauti Python valodā.
abs– Atgriež skaitļa absolūto vērtību.round– Noapaļo skaitli līdz norādītajam decimālzīmju skaitam.min– Atgriež mazāko no ievades vērtībām.max– Atgriež lielāko no ievades vērtībām.sum– Saskaita visas iterējamā objekta vērtības.divmod– Atgriež dalījumu un atlikumu kā kortežu.pow– Kāpina skaitli noteiktā pakāpē, pēc izvēles ar moduli.int– Pārvērš vērtību par veselu skaitli.float– Pārvērš vērtību par peldošā komata skaitli.
Konstantes – iepriekš definētas vērtības no math moduļa.
math.pi– Konstante π (pi).math.tau– Konstante tau (2π).math.e– Eilera skaitlis, naturālā logaritma bāze.
Trigonometrija — aprēķiniet leņķus un attiecības starp malām.
math.sin– Leņķa sinuss radiānos.math.cos– Leņķa kosinuss radiānos.math.tan– Leņķa pieskare radiānos.math.atan– Radiānos izteiktas vērtības arktangenss.math.atan2– y/x arktangens radiānos, ņemot vērā kvadrantu.math.asin– Radiānos norādītas vērtības arksinuss.math.acos– Radiānos norādītas vērtības arkkosinuss.math.degrees– Pārvērš radiānus grādos.math.radians– Pārvērš grādus radiānos.
Noapaļošana un absolūtā vērtība — pielāgojiet precizitāti vai virzienu.
math.ceil– Noapaļo uz augšu līdz tuvākajam veselajam skaitlim.math.floor– Noapaļo uz leju līdz tuvākajam veselajam skaitlim.math.trunc– Noņem decimāldaļu.math.fabs– Atgriež absolūto vērtību kā peldošo skaitli.
Pakāpes rādītāji un logaritmi – pakāpes, saknes un logaritma aprēķini.
math.pow– Kāpina skaitli noteiktā pakāpē.math.sqrt– Atgriež kvadrātsakni.math.exp– Aprēķina e kāpināšanu x pakāpē.math.log– Naturālais logaritms (bāze e).
Peldošā komata operācijas — pārbaudiet vai sadaliet peldošā komata vērtības.
math.modf– Atgriež peldošā skaitļa daļskaitļa un veselo skaitli.math.frexp– Sadala skaitli mantisā un eksponentē.math.fmod– Atlikums ar dalāmā zīmi.math.copysign– Atgriež vērtību ar citas vērtības zīmi.math.ldexp– Computesx * (2 ** exp).
Iebūvētās funkcijas#
Python nodrošina vairākas iebūvētas funkcijas, kas ļauj veikt matemātiskas darbības projekta ietvaros.
abs#
abs atgriež skaitļa absolūto vērtību, noņemot jebkuru negatīvo zīmi.
Usage:
abs(x)
Parametrs |
Apraksts |
|---|---|
|
Vesels skaitlis vai peldošs skaitlis. |
def main():
# Get the absolute value of -10
abs_result = abs(-10)
brain.print(abs_result)
# abs_result = 10
# VR threads — Do not delete
vr_thread(main)
round#
round noapaļo skaitli līdz norādītajam decimālzīmju skaitam.
Usage:
round(x, ndigits)
Parametrs |
Apraksts |
|---|---|
|
Vesels skaitlis vai peldošs skaitlis. |
|
Neobligāti. Decimālzīmju skaits, līdz kuram jānoapaļo. Noklusējuma vērtība ir 0. |
def main():
# Round 5.7 to the nearest integer
round_int_result = round(5.7)
brain.print(round_int_result)
# round_int_result = 6
# VR threads — Do not delete
vr_thread(main)
def main():
# Round 3.14159 to 2 decimal places
round_result = round(3.14159, 2)
brain.print(round_result, precision=2)
# round_result = 3.14
# VR threads — Do not delete
vr_thread(main)
min#
min atgriež mazāko vērtību no vairākiem argumentiem vai iterējama.
Usage:
min(arg1, arg2, …) or min(sequence)
Parametrs |
Apraksts |
|---|---|
|
Skaitļi, ar ko salīdzināt. |
|
Saraksts, kortežs vai cita secība, kas satur skaitļus. |
def main():
# Get the smallest number from 3, 7, and 1
min_result = min(3, 7, 1)
brain.print(min_result)
# min_result = 1
# VR threads — Do not delete
vr_thread(main)
def main():
# Get the smallest value from a list
min_list_result = min([10, 4, 25, 1])
brain.print(min_list_result)
# min_list_result = 1
# VR threads — Do not delete
vr_thread(main)
max#
max atgriež lielāko vērtību no vairākiem argumentiem vai iterējamas vērtības.
Usage:
max(arg1, arg2, …) or max(sequence)
Parametrs |
Apraksts |
|---|---|
|
Skaitļi, ar ko salīdzināt. |
|
Saraksts, kortežs vai cita secība, kas satur skaitļus. |
def main():
# Get the largest number from 3, 7, and 1
max_result = max(3, 7, 1)
brain.print(max_result)
# max_result = 7
# VR threads — Do not delete
vr_thread(main)
def main():
# Get the largest value from a list
max_list_result = max([10, 4, 25, 1])
brain.print(max_list_result)
# max_list_result = 25
# VR threads — Do not delete
vr_thread(main)
sum#
sum saskaita visas iterējamā objekta vērtības ar izvēles sākuma vērtību.
Usage:
sum(sequence, start)
Parametrs |
Apraksts |
|---|---|
|
Saraksts, kortežs vai cita secība, kas satur skaitļus. |
|
Neobligāti. Vērtība, kas jāpieskaita summai. Noklusējuma vērtība ir 0. |
def main():
# Calculate the sum of a list of numbers
sum_result = sum([1, 2, 3, 4, 5])
brain.print(sum_result)
# sum_result = 15
# VR threads — Do not delete
vr_thread(main)
def main():
# Calculate the sum of a list with a starting value of 10
sum_with_start = sum([1, 2, 3], 10)
brain.print(sum_with_start)
# sum_with_start = 16
# VR threads — Do not delete
vr_thread(main)
divmod#
divmod atgriež kortežu, kas satur dalīšanas operācijas dalījumu un atlikumu.
Usage:
divmod(a, b)
Parametrs |
Apraksts |
|---|---|
|
Dividende. |
|
Dalītājs. |
def main():
# Perform integer division and remainder of 10 / 3
divmod_result = divmod(10, 3)
brain.print(divmod_result)
# divmod_result = (3, 1)
# VR threads — Do not delete
vr_thread(main)
pow#
pow kāpina skaitli pakāpē un pēc izvēles veic moduļa darbību.
Usage:
pow(x, y, mod)
Parametrs |
Apraksts |
|---|---|
|
Bāzes skaitlis. |
|
Eksponents. |
|
Neobligāti. Moduļa vērtība. Ja norādīta, atgriež |
def main():
# Calculate 2 raised to the power of 3
pow_result = pow(2, 3)
brain.print(pow_result)
# pow_result = 8
# VR threads — Do not delete
vr_thread(main)
int#
int konvertē skaitli vai virkni par veselu skaitli. Tā atbalsta arī bāzes konvertēšanu, konvertējot no virknes.
Usage:
int(x, base)
Parametrs |
Apraksts |
|---|---|
|
Skaitlis, virkne vai cits objekts, kas jāpārveido. |
|
Neobligāti. Skaitļu bāze, kas jāizmanto konvertēšanai. Noklusējuma vērtība ir 10. |
def main():
# Convert a float to an integer to get rid of decimals
price = 19.99
price_int = int(price)
brain.print("{} coins".format(price_int))
# Output: 19 coins
# VR threads — Do not delete
vr_thread(main)
def main():
# Convert a string into an integer to use in calculations
user_input = "55"
user_number = int(user_input)
brain.print(user_number * 2)
# Output: 110
# VR threads — Do not delete
vr_thread(main)
float#
float pārveido skaitli vai virkni par peldošā komata skaitli.
Usage:
float(x)
Parametrs |
Apraksts |
|---|---|
|
Skaitlis, virkne vai cits objekts, kas jāpārveido. |
def main():
# Convert division result to a float
num_apples = 6
num_people = 2
apples_per_person = float(num_apples) / num_people
brain.print(apples_per_person)
# Output: 3.00
# VR threads — Do not delete
vr_thread(main)
def main():
# Convert a string into a float to use in calculations
user_input = "23.4"
user_number = float(user_input)
brain.print(user_number * 3)
# Output: 70.20
# VR threads — Do not delete
vr_thread(main)
Matemātikas modulis#
MicroPython math modulis nodrošina papildu metodes bieži sastopamu matemātisko aprēķinu veikšanai. Šīs metodes ietver trigonometriskās, logaritmiskās un citas skaitliskās darbības.
Modulis math pēc noklusējuma tiek importēts VEXcode.
Constants#
Konstantes ir iepriekš definētas vērtības, kas projekta laikā paliek nemainīgas. Tās var izmantot aprēķinos bez nepieciešamības pēc definīcijas vai piešķiršanas.
pī#
pi norāda matemātisko konstanti π, apļa apkārtmēra attiecību pret tā diametru.
Usage:
math.pi
def main():
# Calculate the area of a circle with radius 5 using pi
circle_area = math.pi * 5 * 5
brain.print(circle_area, precision=2)
# circle_area = 78.54
# VR threads — Do not delete
vr_thread(main)
tau#
tau dod 2π vērtību.
Usage:
math.tau
def main():
# Calculate the circumference of a circle with radius
circumference = math.tau * 5
brain.print(circumference, precision=2)
# circumference = 31.42
# VR threads — Do not delete
vr_thread(main)
e#
e dod naturālā logaritma bāzi.
Usage:
math.e
def main():
# Calculate e raised to the power of 2
e_power = math.pow(math.e, 2)
brain.print(e_power, precision=2)
# e_power = 7.39
# VR threads — Do not delete
vr_thread(main)
Trigonometry#
grēks#
sin aprēķina leņķa sinusu radiānos un atgriež peldošo skaitli.
Usage:
math.sin(x)
Parametrs |
Apraksts |
|---|---|
|
Pludiņskaitlis vai vesels skaitlis, kas attēlo leņķi radiānos. |
def main():
# Calculate the sine of
# 30 degrees in radians
result = math.sin(math.radians(30))
brain.print(result, precision=2)
# result = 0.50
# VR threads — Do not delete
vr_thread(main)
jo#
cos aprēķina leņķa kosinusu radiānos un atgriež peldošo skaitli.
Usage:
math.cos(x)
Parametrs |
Apraksts |
|---|---|
|
Pludiņskaitlis vai vesels skaitlis, kas attēlo leņķi radiānos. |
def main():
# Calculate the cosine of
# 60 degrees in radians
result = math.cos(math.radians(60))
brain.print(result, precision=2)
# result = 0.50
# VR threads — Do not delete
vr_thread(main)
iedegums#
tan aprēķina leņķa tangensu radiānos un atgriež peldošo skaitli.
Usage:
math.tan(x)
Parametrs |
Apraksts |
|---|---|
|
Pludiņskaitlis vai vesels skaitlis, kas attēlo leņķi radiānos. |
def main():
# Calculate the tangent of
# 45 degrees in radians
result = math.tan(math.radians(45))
brain.print(result, precision=2)
# result = 1.00
# VR threads — Do not delete
vr_thread(main)
Atans#
atan aprēķina skaitļa apgriezto tangensu (arktangensu) un atgriež peldošo skaitli, kas attēlo leņķi radiānos.
Usage:
math.atan(x)
Parametrs |
Apraksts |
|---|---|
|
Peldskaitlis vai vesels skaitlis. |
def main():
# Calculate the arc tangent
# of 1 in degrees
arc_tan = math.atan(1.0)
result = math.degrees(arc_tan)
brain.print(result, precision=2)
# result = 45.00
# VR threads — Do not delete
vr_thread(main)
atan2#
atan2 aprēķina y/x apgrieztā tangensa galveno vērtību un atgriež peldošo skaitli, kas attēlo leņķi radiānos.
Usage:
math.atan2(y, x)
Parametrs |
Apraksts |
|---|---|
|
Pludiņskaitlis vai vesels skaitlis, kas attēlo y koordinātu. |
|
Pludiņskaitlis vai vesels skaitlis, kas attēlo x koordinātu. |
def main():
# Calculate the inverse tangent
# of 1/1 in degrees
atan2 = math.atan2(1.0, 1.0)
result = math.degrees(atan2)
brain.print(result, precision=2)
# result = 45.00
# VR threads — Do not delete
vr_thread(main)
muļķis#
asin aprēķina skaitļa apgriezto sinusu (arksinusu) un atgriež peldošo skaitli, kas attēlo leņķi radiānos.
Usage:
math.asin(x)
Parametrs |
Apraksts |
|---|---|
|
Pludiņskaitlis vai vesels skaitlis no -1 līdz 1. |
def main():
# Calculate the arc sine
# of 0.5 in degrees
result = math.degrees(math.asin(0.5))
brain.print(result, precision=2)
# result = 30.00
# VR threads — Do not delete
vr_thread(main)
akos#
acos aprēķina skaitļa apgriezto kosinusu (arkkosinusu) un atgriež peldošo skaitli, kas attēlo leņķi radiānos.
Usage:
math.acos(x)
Parametrs |
Apraksts |
|---|---|
|
Pludiņskaitlis vai vesels skaitlis no -1 līdz 1. |
def main():
# Calculate the arc cosine
# of 0.5 in degrees
result = math.degrees(math.acos(0.5))
brain.print(result, precision=2)
# result = 60.00
# VR threads — Do not delete
vr_thread(main)
grādi#
degrees konvertē leņķi no radiāniem uz grādiem.
Usage:
math.degrees(x)
Parametrs |
Apraksts |
|---|---|
|
Pludiņskaitlis vai vesels skaitlis, kas attēlo leņķi radiānos. |
def main():
# Convert pi radians to degrees
degrees_result = math.degrees(math.pi)
brain.print(degrees_result, precision=2)
# degrees_result = 180.00
# VR threads — Do not delete
vr_thread(main)
radiāni#
radians konvertē leņķi no grādiem uz radiāniem.
Usage:
math.radians(x)
Parametrs |
Apraksts |
|---|---|
|
Pludiņskaitlis vai vesels skaitlis, kas attēlo leņķi grādos. |
def main():
# Convert 180 degrees to radians
radians_result = math.radians(180)
brain.print(radians_result, precision=2)
# radians_result = 3.14
# VR threads — Do not delete
vr_thread(main)
Rounding & Absolute Values#
griesti#
ceil noapaļo skaitli uz augšu līdz tuvākajam veselajam skaitlim.
Usage:
math.ceil(x)
Parametrs |
Apraksts |
|---|---|
|
Pludiņskaitlis vai vesels skaitlis, kas jānoapaļo uz augšu. |
def main():
# Round 3.7 up to the nearest integer
ceil_result = math.ceil(3.7)
brain.print(ceil_result)
# ceil_result = 4
# VR threads — Do not delete
vr_thread(main)
grīda#
floor noapaļo skaitli uz leju līdz tuvākajam veselajam skaitlim.
Usage:
math.floor(x)
Parametrs |
Apraksts |
|---|---|
|
Pludiņskaitlis vai vesels skaitlis, kas jānoapaļo uz leju. |
def main():
# Round 3.7 down to the nearest integer
floor_result = math.floor(3.7)
brain.print(floor_result)
# floor_result = 3
# VR threads — Do not delete
vr_thread(main)
saīsinājums#
trunc noņem skaitļa decimāldaļu bez noapaļošanas.
Usage:
math.trunc(x)
Parametrs |
Apraksts |
|---|---|
|
Apcirpšanai paredzēts pludiņskaitlis. |
def main():
# Remove the decimal part of 3.7
trunc_result = math.trunc(3.7)
brain.print(trunc_result)
# trunc_result = 3
# VR threads — Do not delete
vr_thread(main)
fabs#
fabs atgriež skaitļa absolūto vērtību kā peldošo skaitli.
Usage:
math.fabs(x)
Parametrs |
Apraksts |
|---|---|
|
Peldskaitlis vai vesels skaitlis. |
def main():
# Get the absolute value of -3.7
fabs_result = math.fabs(-3.7)
brain.print(fabs_result, precision=2)
# fabs_result = 3.70
# VR threads — Do not delete
vr_thread(main)
Exponents & Logarithms#
pulveris#
pow kāpina x y pakāpē un atgriež peldošo skaitli, pat ja abas ievades vērtības ir veseli skaitļi.
Usage:
math.pow(x, y)
Parametrs |
Apraksts |
|---|---|
|
Pludiņa vai vesela skaitļa bāze. |
|
Peldošs vai vesels skaitlis. |
def main():
# Calculate 2 raised to the power of 3
power_result = math.pow(2, 3)
brain.print(power_result, precision=2)
# power_result = 8.00
# VR threads — Do not delete
vr_thread(main)
kvadrātmetrs#
Funkcija sqrt aprēķina skaitļa kvadrātsakni un atgriež peldošo skaitli pat perfektiem kvadrātiem.
Usage:
math.sqrt(x)
Parametrs |
Apraksts |
|---|---|
|
Nenegatīvs peldošs skaitlis vai vesels skaitlis. |
def main():
# Calculate the square root of 16
sqrt_result = math.sqrt(16)
brain.print(sqrt_result, precision=2)
# sqrt_result = 4.00
# VR threads — Do not delete
vr_thread(main)
pieredze#
exp aprēķina skaitļa eksponenci un atgriež peldošo skaitli.
Usage:
math.exp(x)
Parametrs |
Apraksts |
|---|---|
|
Peldskaitlis vai vesels skaitlis. |
def main():
# Calculate e raised to the power of 1
exp_result = math.exp(1)
brain.print(exp_result, precision=2)
# exp_result = 2.72
# VR threads — Do not delete
vr_thread(main)
žurnāls#
log aprēķina skaitļa naturālo logaritmu un atgriež peldošo skaitli.
Usage:
math.log(x)
Parametrs |
Apraksts |
|---|---|
|
Pozitīvs peldošs skaitlis vai vesels skaitlis. |
def main():
# Calculate the natural logarithm
# (base e) of 7.389056
log_result = math.log(7.389056)
brain.print(log_result, precision=2)
# log_result = 2.00
# VR threads — Do not delete
vr_thread(main)
log10#
log10 aprēķina skaitļa desmitdaļlogaritmu un atgriež peldošo skaitli.
Usage:
math.log10(x)
Parametrs |
Apraksts |
|---|---|
|
Pozitīvs peldošs skaitlis vai vesels skaitlis. |
def main():
# Calculate the log10 of 100
log_result = math.log10(100)
brain.print(log_result)
# log_result = 2
# VR threads — Do not delete
vr_thread(main)
Floating Point Operations#
modf#
modf sadala skaitli tā daļskaitļa un vesela skaitļa daļās un atgriež kortežu (fractional part, integer part), abus kā peldošos skaitļus.
Usage:
math.modf(x)
Parametrs |
Apraksts |
|---|---|
|
Peldskaitlis vai vesels skaitlis sadalīšanai. |
def main():
# Decompose 3.14159 into fractional and integer parts
fractional_part, integer_part = math.modf(3.14159)
brain.print(fractional_part, precision=2)
brain.new_line()
brain.print(integer_part, precision=2)
# fractional_part = 0.14
# integer_part = 3.00
# VR threads — Do not delete
vr_thread(main)
frexp#
frexp sadala skaitli tā mantisā un eksponentā un atgriež kortežu (mantissa, exponent), kur mantisa ir peldošā vērtība un eksponents ir vesels skaitlis.
Usage:
math.frexp(x)
Parametrs |
Apraksts |
|---|---|
|
Peldskaitlis vai vesels skaitlis sadalīšanai. |
def main():
# Decompose 16 into its mantissa and exponent
mantissa, exponent = math.frexp(16)
brain.print(mantissa, precision=2)
brain.new_line()
brain.print(exponent)
# mantissa = 0.50
# exponent = 5
# VR threads — Do not delete
vr_thread(main)
fmod#
fmod atgriež dalīšanas atlikumu, saglabājot dalāmā zīmi (x).
Usage:
math.fmod(x, y)
Parametrs |
Apraksts |
|---|---|
|
Dividende. |
|
Dalītājs. |
def main():
# Calculate remainder of 10 / 3
# that preserves the sign of 10
fmod_result = math.fmod(10, 3)
brain.print(fmod_result, precision=2)
# fmod_result = 1.00
# VR threads — Do not delete
vr_thread(main)
autortiesību paraksts#
copysign atgriež x ar y zīmi.
Usage:
math.copysign(x, y)
Parametrs |
Apraksts |
|---|---|
|
Vērtība, kas jāmodificē. |
|
Vērtība, kuras zīme tiks kopēta. |
def main():
# Return -10 with the sign of 3 (positive)
copysign_result = math.copysign(-10, 3)
brain.print(copysign_result, precision=2)
# copysign_result = 10.00
# VR threads — Do not delete
vr_thread(main)
ldexp#
ldexp aprēķina x * (2 ** exp), kas ir līdzvērtīgs x * 2exp{code}.
Usage:
math.ldexp(x, exp)
Parametrs |
Apraksts |
|---|---|
|
Bāzes vērtība. |
|
Eksponents. |
def main():
# Compute 3 * (2 ** 4)
ldexp_result = math.ldexp(3, 4)
brain.print(ldexp_result, precision=2)
# ldexp_result = 48.00
# VR threads — Do not delete
vr_thread(main)