forked from parallaxinc/spin-standard-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.int.spin
158 lines (126 loc) · 3.94 KB
/
math.int.spin
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
{
--------------------------------------------
Filename: math.int.spin
Author: Jesse Burt
Description: Basic integer math functions
Copyright (c) 2022
Started Apr 11, 2021
Updated Sep 18, 2022
See end of file for terms of use.
--------------------------------------------
}
' Used by Atan2()
K1 = 5701
K2 = -1645
K3 = 446
VAR
long _rndseed
PUB null{}
' This is not a top-level object
PUB atan(iy, ix): iangle | iratio, itmp
' Arctangent
if (ix == 0) and (iy == 0)
return 0
if (ix == 0) and (iy <> 0)
return 90_00
if (iy =< ix)
iratio := div(iy, ix)
else
iratio := div(ix, iy)
iangle := K1 * iratio
itmp := (iratio >> 5) * (iratio >> 5) * (iratio >> 5)
iangle += (itmp >> 15) * K2
itmp := (itmp >> 20) * (iratio >> 5) * (iratio >> 5)
iangle += (itmp >> 15) * K3
iangle := iangle >> 15
if (iy > ix)
iangle := (90_00 - iangle)
if (iangle < 0)
iangle := 0
if (iangle > 90_00)
iangle := 90_00
return iangle
PUB atan2(iy, ix): iresult
' 2-argument arctangent
if (ix == -32768)
ix := -32767
if (iy == -32768)
iy := -32767
if ((ix => 0) and (iy => 0))
iresult := atan(iy, ix)
elseif ((ix =< 0) and (iy => 0))
iresult := (180_00 - atan(iy, -ix))
elseif ((ix =< 0) and (iy =< 0))
iresult := (-180_00 + atan(-iy, -ix))
else
iresult := (-atan(-iy, ix))
return iresult
PUB cos(angle): cosine
' Return the cosine of angle
return sin(angle + $800)
PUB log2(num): l2
' Return base-2 logarithm of num
return (>| num)-1
PUB pow(base, exp): r
' Return base raised to the power of exp
r := base
exp--
repeat exp
r *= base
PUB rndseed(val)
' Set seed for random number generator
_rndseed := val
PUB rnd(maxval): r
' Return random unsigned number up to maxval
return ||(? _rndseed) // maxval
PUB rnds(maxval): r
' Return random signed number up to maxval
return (? _rndseed) // maxval
PUB rndi(maxval): r
' Return random unsigned number up to maxval, inclusive
return ||(? _rndseed) // (maxval+1)
PUB rndsi(maxval): r
' Return random signed number up to maxval, inclusive
return (? _rndseed) // (maxval+1)
PUB sin(angle): sine
' Return the sine of angle
sine := angle << 1 & $FFE
if angle & $800
sine := word[$F000 - sine] ' ROM Sine table
else
sine := word[$E000 + sine]
if angle & $1000
-sine
CON
MINDELTADIV = 1
MINDELTATRIG= 1
PRI div(iy, ix): ir | itmp, idelta
ir := 0
idelta := 16384
repeat while (ix < 16384) and (iy < 16384)
ix := (ix + ix)
iy := (iy + iy)
repeat
itmp := (ir + idelta)
itmp := ((itmp * ix) >> 15)
if (itmp =< iy)
ir += idelta
idelta := (idelta >> 1)
while (idelta => MINDELTADIV)
return ir
DAT
{
Copyright 2022 Jesse Burt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}