Μαθηματικά#

Εισαγωγή#

Math includes both built-in Python functions and the full math module, which is automatically available in VEXcode VR. These tools allow you to perform everything from basic arithmetic to advanced trigonometry, rounding, and logarithmic operations.

Χρησιμοποιήστε αυτές τις συναρτήσεις και τις σταθερές για να υπολογίσετε θέσεις, γωνίες, αποστάσεις και άλλες αριθμητικές τιμές για το ρομπότ σας. Μπορείτε επίσης να μετατρέψετε μεταξύ μοιρών και ακτινίων, να αξιολογήσετε εκφράσεις και να εργαστείτε με ειδικές τιμές όπως το άπειρο και το NaN.

Παρακάτω είναι μια λίστα με τις διαθέσιμες μαθηματικές συναρτήσεις, σταθερές και βοηθητικά προγράμματα:

Ενσωματωμένες συναρτήσεις – Κοινά μαθηματικά εργαλεία που περιλαμβάνονται στην Python.

  • abs – Returns the absolute value of a number.

  • round – Rounds a number to a specified number of decimal places.

  • min – Returns the smallest of the input values.

  • max – Returns the largest of the input values.

  • sum – Adds up all values in an iterable.

  • divmod – Returns the quotient and remainder as a tuple.

  • pow – Raises a number to a power, optionally with a modulus.

  • int – Converts a value to an integer.

  • float – Converts a value to a floating-point number.

Constants – Predefined values from the math module.

  • math.pi – The constant π (pi).

  • math.tau – The constant tau (2π).

  • math.e – Euler’s number, base of the natural log.

Τριγωνομετρία – Υπολογισμός γωνιών και σχέσεων μεταξύ πλευρών.

  • math.sin – Sine of an angle in radians.

  • math.cos – Cosine of an angle in radians.

  • math.tan – Tangent of an angle in radians.

  • math.atan – Arctangent of a value in radians.

  • math.atan2 – Arctangent of y/x in radians, considering the quadrant.

  • math.asin – Arcsine of a value in radians.

  • math.acos – Arccosine of a value in radians.

  • math.degrees – Converts radians to degrees.

  • math.radians – Converts degrees to radians.

Στρογγυλοποίηση & Απόλυτη Τιμή – Προσαρμογή ακρίβειας ή κατεύθυνσης.

  • math.ceil – Rounds up to the nearest integer.

  • math.floor – Rounds down to the nearest integer.

  • math.trunc – Removes the decimal portion.

  • math.fabs – Returns the absolute value as a float.

Εκθέτες & Λογάριθμοι – Υπολογισμοί δύναμης, ρίζας και λογαρίθμου.

  • math.pow – Raises a number to a power.

  • math.sqrt – Returns the square root.

  • math.exp – Calculates e to the power of x.

  • math.log – Natural logarithm (base e).

Πράξεις κινητής υποδιαστολής – Έλεγχος ή ανάλυση τιμών κινητής υποδιαστολής.

  • math.modf – Returns the fractional and integer parts of a float.

  • math.frexp – Decomposes a number into mantissa and exponent.

  • math.fmod – Remainder with sign of the dividend.

  • math.copysign – Returns a value with the sign of another.

  • math.ldexp – Computes x * (2 ** exp).

Ενσωματωμένες λειτουργίες#

Η Python παρέχει αρκετές ενσωματωμένες συναρτήσεις που σας επιτρέπουν να εκτελείτε μαθηματικές πράξεις μέσα στο έργο σας.

abs#

abs returns the absolute value of a number, removing any negative sign.

Usage:
abs(x)

Παράμετρος

Περιγραφή

x

Ένας ακέραιος αριθμός ή ένας αριθμός με κινητή βάση.

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 rounds a number to a specified number of decimal places.

Usage:
round(x, ndigits)

Παράμετρος

Περιγραφή

x

Ένας ακέραιος αριθμός ή ένας αριθμός με κινητή βάση.

ndigits

Προαιρετικό. Ο αριθμός των δεκαδικών ψηφίων για στρογγυλοποίηση. Η προεπιλογή είναι 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 returns the smallest value from multiple arguments or an iterable.

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

Παράμετρος

Περιγραφή

arg1, arg2, …

Οι αριθμοί προς σύγκριση.

sequence

Μια λίστα, πλειάδα ή άλλη ακολουθία που περιέχει αριθμούς.

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 returns the largest value from multiple arguments or an iterable.

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

Παράμετρος

Περιγραφή

arg1, arg2, …

Οι αριθμοί προς σύγκριση.

sequence

Μια λίστα, πλειάδα ή άλλη ακολουθία που περιέχει αριθμούς.

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 adds up all values in an iterable, with an optional starting value.

Usage:
sum(sequence, start)

Παράμετρος

Περιγραφή

sequence

Μια λίστα, πλειάδα ή άλλη ακολουθία που περιέχει αριθμούς.

start

Προαιρετικό. Μια τιμή που θα προστεθεί στο άθροισμα. Η προεπιλογή είναι 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 returns a tuple containing the quotient and remainder of a division operation.

Usage:
divmod(a, b)

Παράμετρος

Περιγραφή

a

Το μέρισμα.

b

Ο διαιρέτης.

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 raises a number to a power and optionally performs a modulus operation.

Usage:
pow(x, y, mod)

Παράμετρος

Περιγραφή

x

Ο βασικός αριθμός.

y

Ο εκθέτης.

mod

Optional. A modulus value. If provided, returns (x ** y) % mod.

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 converts a number or string into an integer. It also supports base conversion when converting from a string.

Usage:
int(x, base)

Παράμετρος

Περιγραφή

x

Ένας αριθμός, μια συμβολοσειρά ή άλλο αντικείμενο για μετατροπή.

base

Προαιρετικό. Η αριθμητική βάση που θα χρησιμοποιηθεί για τη μετατροπή. Η προεπιλογή είναι 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 converts a number or string into a floating-point number.

Usage:
float(x)

Παράμετρος

Περιγραφή

x

Ένας αριθμός, μια συμβολοσειρά ή άλλο αντικείμενο για μετατροπή.

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)

Μαθηματικά Ενότητα#

The math module in MicroPython provides additional methods for performing common mathematical calculations. These methods include trigonometric, logarithmic, and other numerical operations.

The math module is imported by default in VEXcode.

Constants#

Οι σταθερές είναι προκαθορισμένες τιμές που παραμένουν σταθερές κατά τη διάρκεια ενός έργου. Μπορούν να χρησιμοποιηθούν σε υπολογισμούς χωρίς να απαιτείται ορισμός ή ανάθεση.

πι#

pi gives the mathematical constant π, the ratio of a circle’s circumference to its diameter.

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 gives the value of 2π.

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 gives the base of the natural logarithm.

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#

αμαρτία#

sin calculates the sine of an angle in radians and returns a float.

Usage:
math.sin(x)

Παράμετρος

Περιγραφή

x

Ένας ακέραιος αριθμός ή ένας κινητήριος αριθμός που αντιπροσωπεύει μια γωνία σε ακτίνια.

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)

γιατί#

cos calculates the cosine of an angle in radians and returns a float.

Usage:
math.cos(x)

Παράμετρος

Περιγραφή

x

Ένας ακέραιος αριθμός ή ένας κινητήριος αριθμός που αντιπροσωπεύει μια γωνία σε ακτίνια.

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)

βυρσοδέψω#

tan calculates the tangent of an angle in radians and returns a float.

Usage:
math.tan(x)

Παράμετρος

Περιγραφή

x

Ένας ακέραιος αριθμός ή ένας κινητήριος αριθμός που αντιπροσωπεύει μια γωνία σε ακτίνια.

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)

ατάν#

atan calculates the inverse tangent (arc tangent) of a number and returns a float representing the angle in radians.

Usage:
math.atan(x)

Παράμετρος

Περιγραφή

x

Ένας αριθμός κινητής υποδιαστολής ή ένας ακέραιος αριθμός.

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 calculates the principal value of the inverse tangent of y/x and returns a float representing the angle in radians.

Usage:
math.atan2(y, x)

Παράμετρος

Περιγραφή

y

Ένας ακέραιος αριθμός ή ένας κινητήριος αριθμός που αντιπροσωπεύει τη συντεταγμένη y.

x

Ένας κινητής υποδιαστολής ή ένας ακέραιος αριθμός που αντιπροσωπεύει τη συντεταγμένη x.

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)

ασίν#

asin calculates the inverse sine (arc sine) of a number and returns a float representing the angle in radians.

Usage:
math.asin(x)

Παράμετρος

Περιγραφή

x

Ένας αριθμός κινητής υποδιαστολής ή ένας ακέραιος αριθμός μεταξύ -1 και 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)

acos#

acos calculates the inverse cosine (arc cosine) of a number and returns a float representing the angle in radians.

Usage:
math.acos(x)

Παράμετρος

Περιγραφή

x

Ένας αριθμός κινητής υποδιαστολής ή ένας ακέραιος αριθμός μεταξύ -1 και 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)

βαθμούς#

degrees converts an angle from radians to degrees.

Usage:
math.degrees(x)

Παράμετρος

Περιγραφή

x

Ένας ακέραιος αριθμός ή ένας κινητήριος αριθμός που αντιπροσωπεύει μια γωνία σε ακτίνια.

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)

ακτίνια#

radians converts an angle from degrees to radians.

Usage:
math.radians(x)

Παράμετρος

Περιγραφή

x

Ένας ακέραιος αριθμός ή ένας κινητήριος αριθμός που αντιπροσωπεύει μια γωνία σε μοίρες.

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#

οροφή#

ceil rounds a number up to the nearest integer.

Usage:
math.ceil(x)

Παράμετρος

Περιγραφή

x

Ένας αριθμός με κινητή βάση ή ένας ακέραιος αριθμός που θα στρογγυλοποιηθεί προς τα πάνω.

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)

πάτωμα#

floor rounds a number down to the nearest integer.

Usage:
math.floor(x)

Παράμετρος

Περιγραφή

x

Ένας αριθμός κινητής υποδιαστολής ή ένας ακέραιος αριθμός που θα στρογγυλοποιηθεί προς τα κάτω.

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)

κορμός#

trunc removes the decimal part of a number without rounding.

Usage:
math.trunc(x)

Παράμετρος

Περιγραφή

x

Μια κινητή περιοχή που θα περικοπεί.

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 returns the absolute value of a number as a float.

Usage:
math.fabs(x)

Παράμετρος

Περιγραφή

x

Ένας αριθμός κινητής υποδιαστολής ή ένας ακέραιος αριθμός.

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#

πάου#

pow raises x to the power of y and returns a float, even if both inputs are integers.

Usage:
math.pow(x, y)

Παράμετρος

Περιγραφή

x

Μια βάση με κινητή βάση ή ακέραιο αριθμό.

y

Ένας κινητής υποδιαστολής ή ένας ακέραιος εκθέτης.

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)

τετραγωνικό#

sqrt calculates the square root of a number and returns a float, even for perfect squares.

Usage:
math.sqrt(x)

Παράμετρος

Περιγραφή

x

Ένας μη αρνητικός κινητής υποδιαστολής ή ακέραιος αριθμός.

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)

εμπειρία#

exp calculates the exponential of a number and returns a float.

Usage:
math.exp(x)

Παράμετρος

Περιγραφή

x

Ένας αριθμός κινητής υποδιαστολής ή ένας ακέραιος αριθμός.

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)

κούτσουρο#

log calculates the natural logarithm of a number and returns a float.

Usage:
math.log(x)

Παράμετρος

Περιγραφή

x

Ένας θετικός αριθμός κινητής υποδιαστολής ή ακέραιος αριθμός.

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 calculates the base 10 logarithm of a number and returns a float.

Usage:
math.log10(x)

Παράμετρος

Περιγραφή

x

Ένας θετικός αριθμός κινητής υποδιαστολής ή ακέραιος αριθμός.

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 decomposes a number into its fractional and integer parts and returns a tuple (fractional part, integer part), both as floats.

Usage:
math.modf(x)

Παράμετρος

Περιγραφή

x

Ένας κινητής υποδιαστολής ή ένας ακέραιος αριθμός προς ανάλυση.

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 decomposes a number into its mantissa and exponent and returns a tuple (mantissa, exponent), where the mantissa is a float and the exponent is an integer.

Usage:
math.frexp(x)

Παράμετρος

Περιγραφή

x

Ένας κινητής υποδιαστολής ή ένας ακέραιος αριθμός προς ανάλυση.

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 returns the remainder of division while keeping the sign of the dividend (x).

Usage:
math.fmod(x, y)

Παράμετρος

Περιγραφή

x

Το μέρισμα.

y

Ο διαιρέτης.

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)

πνευματικό δικαίωμα#

copysign returns x with the sign of y.

Usage:
math.copysign(x, y)

Παράμετρος

Περιγραφή

x

Η τιμή που θα τροποποιηθεί.

y

Η τιμή της οποίας το πρόσημο θα αντιγραφεί.

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 computes x * (2 ** exp), which is equivalent to x * 2exp{code}.

Usage:
math.ldexp(x, exp)

Παράμετρος

Περιγραφή

x

Η βασική τιμή.

exp

Ο εκθέτης.

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)