-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmath.html
More file actions
90 lines (88 loc) · 6.04 KB
/
Copy pathmath.html
File metadata and controls
90 lines (88 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!-- InstanceBegin template="/Templates/template.dwt" codeOutsideHTMLIsLocked="false" --><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>VPython Help</title>
<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="head" -->
<link href="VisualRef.css" rel="stylesheet" type="text/css" />
<!-- InstanceEndEditable -->
</head>
<body>
<div id="wrapper">
<div id="leftmenu">
<p class="Normal"><a href="index.html"> Home</a></p>
<p class="Normal"> If you're new to Python <br />
and VPython: <a href="VisualIntro.html">Introduction</a></p>
<p class="Normal"> A VPython <a href="VPython_Intro.pdf" target="_blank">tutorial</a></p>
<p class="Normal"><a href="videos.html"> Introductory Videos</a></p>
<p class="Normal"><a href="primitives.html"> Pictures</a> of 3D objects</p>
<p> <select id="menu1" onChange="jumpMenu(this)"></select></p>
<p> <select id="menu2" onChange="jumpMenu(this)"></select></p>
<p> <select id="menu3" onChange="jumpMenu(this)"></select></p>
<p class="Normal"><a href="new_features.html"> What's new</a></p>
<p class="Normal"><a href="https://vpython.org" target="_blank"> Classic VPython web site</a><br />
<a href="license.txt" target="_blank"> VPython license</a><br />
<a href="https://www.python.org" target="_blank"> Python web site</a> <br /></p></td>
</div>
<div id="content">
<!-- InstanceBeginEditable name="content" -->
<div>
<h1 class="Heading-1"> <font color="#0000A0">Math Functions</font></h1>
</div>
<div>
<p class="Normal"> For versions of Python prior to 3.0, Python performs integer division with truncation,
so that 3/4 is 0, not 0.75. This is inconvenient when doing scientific computations,
and can lead to hard-to-find bugs in programs. In the GlowScript environment, where VPython code is compiled to JavaScript, 3/4 is always 0.75.</p>
<div> </div>
</div>
<p class="Normal">The following math functions are provided:</p>
<p class="program">abs(x)<br />
sqrt(x)<br />
sin(x)<br />
cos(x)<br />
tan(x)<br />
asin(x) # arc sine<br />
acos(x) # arc cosine<br />
atan(x) # arc tangent; -pi/2 to pi/2<br />
atan2(y,x) # angle whose tangent is y/x; -pi to pi<br />
exp(x) # e to the x<br />
log(x) # natural log, base e<br />
# log(x)/log(10) gives log base 10<br />
pow(x,y) # x to the power y<br />
pi # 3.14159.... <br />
ceil(x) # round up to nearest integer<br />
floor(x) # round down to nearest integer<br />
sign(x) # +1 if x > 0, -1 if x < 0, 0 if x == 0<br />
round(x) # round to nearest integer <br />
max(x,y,z) # the largest of x,y,z<br />
min(x,y,z) # the smallest of x,y,z<br />
random() # pseudorandom number 0 to 1<br />
factorial(x) # x! = x*(x-1)*(x-2)....(1)<br />
combin(x,y) # x!/(y!*(x-y)!) <br />
max(a,b,c,..) # maximum of these<br />
min(a,b,c,..) # minimum of these</p>
<p class="Normal">Many of the quantities above are short forms of the JavaScript functions Math.abs(x), Math.sqrt(x), etc. <strong><a href="factorial.html">Here are details</a></strong> of the factorial and combin functions.</p>
<p class="Normal"> </p>
<p class="Normal">In general, Python modules cannot be imported into VPython programs, because GlowScript VPython functions in a JavaScript environment. However, one can import the Python "random" module into a VPython program, provided by the RapydScript-NG tool that converts Python to JavaScript. One cannot use the form "from random import *" but must use "import random" or "import random as rr" (or other name) or "from random import randint, uniform" (or other list of functions). Here is <strong><a href="https://docs.python.org/3/library/random.html">documentation</a></strong> of the Python random module, however only seed_state, get_random_byte, seed, random, randrange, randint, uniform, choice, shuffle, and sample have been implemented in GlowScript. You do not need to import the random module in order to be able to use the random() function described above.</p>
<p class="Normal"> </p>
<p class="Normal">There are functions for converting between degrees and
radians, where there are 2*pi radians in 360 degrees: </p>
<p class="program">radians(360) # equal to 2*pi<br />
degrees(2*pi) # equal to 360 </p>
<p class="Normal"> </p>
<p class="Normal">See JavaScript documentation on the <strong><a href="https://www.w3schools.com/jsref/jsref_obj_date.asp" target="_blank">Date</a></strong> object which provides methods for determining the current day etc. </p>
<p class="Normal">Here is a way to determine elapsed time in seconds, with microsecond accuracy, using the clock() function that is also available in the standard Python "time" module. The displayed text is approximately 1.5:</p>
<p class="program">t1 = clock()<br />
sleep(1.5)<br />
t2 = clock()<br />
print(t2-t1) </p>
<p class="Normal">There is also a function msclock() which gives the time in milliseconds instead of seconds.</p>
<p class="Normal"> </p>
<p class="Normal">You can <a href="MathJax.html" target="_blank"><strong>use LaTeX</strong></a> to display mathematical expressions on the page.</p>
<br />
<!-- InstanceEndEditable -->
</div>
</div>
</body>
<script type="text/javascript" language="javascript" src="navigation.js"></script>
<!-- InstanceEnd --></html>