Skip to content

Commit 2b0ac64

Browse files
committed
feat: add some math functions
1 parent f7e74af commit 2b0ac64

File tree

2 files changed

+193
-9
lines changed

2 files changed

+193
-9
lines changed

planglib/std/math.pi

Lines changed: 184 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ fn set_lo(x: *i64, lx: i64) void {
1717
return;
1818
}
1919

20-
var Lg1 = &6.666666666666735130e-01;
21-
var Lg2 = &3.999999999940941908e-01;
22-
var Lg3 = &2.857142874366239149e-01;
23-
var Lg4 = &2.222219843214978396e-01;
24-
var Lg5 = &1.818357216161805012e-01;
25-
var Lg6 = &1.531383769920937332e-01;
26-
var Lg7 = &1.479819860511658591e-01;
20+
var Lg1 = 6.666666666666735130e-01;
21+
var Lg2 = 3.999999999940941908e-01;
22+
var Lg3 = 2.857142874366239149e-01;
23+
var Lg4 = 2.222219843214978396e-01;
24+
var Lg5 = 1.818357216161805012e-01;
25+
var Lg6 = 1.531383769920937332e-01;
26+
var Lg7 = 1.479819860511658591e-01;
2727

2828
/// 使用泰勒级数实现
2929
pub fn ln(x: f64) f64 {
@@ -79,8 +79,8 @@ pub fn ln(x: f64) f64 {
7979
i = hx - 0x6147a;
8080
w = z*z;
8181
j = 0x6b851-hx;
82-
t1= w*(*Lg2+w*(*Lg4+w**Lg6));
83-
t2= z*(*Lg1+w*(*Lg3+w*(*Lg5+w**Lg7)));
82+
t1= w*(Lg2+w*(Lg4+w*Lg6));
83+
t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7)));
8484
i =i | j;
8585
R = t2+t1;
8686
if i>0 {
@@ -97,4 +97,179 @@ pub fn ln(x: f64) f64 {
9797
return kf64*ln2_hi-((s*(f-R)-kf64*ln2_lo)-f);
9898
}
9999
}
100+
}
101+
102+
pub fn abs(x: f64) f64 {
103+
if x < 0.0 {
104+
return -x;
105+
} else {
106+
return x;
107+
}
108+
}
109+
110+
pub fn absi(x: i64) i64 {
111+
if x < 0 {
112+
return -x;
113+
} else {
114+
return x;
115+
}
116+
}
117+
118+
pub fn deg2rad(x: f64) f64 {
119+
return x * 0.017453292519943295769236907684886;
120+
}
121+
122+
pub fn cos(x: f64) f64 {
123+
let term = 1.0; // The current term in the Taylor series
124+
let sum = 1.0; // The sum of all terms so far
125+
let n = 0; // The current exponent
126+
let x_squared = x * x; // The square of x
127+
128+
while abs(term) > 1e-10 {
129+
n = n + 2;
130+
term = term* (-x_squared / ((n - 1) as f64 * n as f64));
131+
sum = sum + term;
132+
}
133+
134+
return sum;
135+
}
136+
137+
pub fn sin(x: f64) f64 {
138+
let term = x; // The current term in the Taylor series
139+
let sum = x; // The sum of all terms so far
140+
let n = 1; // The current exponent
141+
let x_squared = x * x; // The square of x
142+
143+
while abs(term) > 1e-10 {
144+
n = n + 2;
145+
term = term* (-x_squared / ((n - 1) as f64 * n as f64));
146+
sum = sum + term;
147+
}
148+
149+
return sum;
150+
}
151+
152+
pub fn tan(x: f64) f64 {
153+
return sin(x) / cos(x);
154+
}
155+
156+
pub fn atan(x: f64) f64 {
157+
let term = x; // The current term in the Taylor series
158+
let sum = x; // The sum of all terms so far
159+
let n = 1; // The current exponent
160+
let x_squared = x * x; // The square of x
161+
162+
while abs(term) > 1e-10 {
163+
n = n + 2;
164+
term = term* (-x_squared / n as f64);
165+
sum = sum + term;
166+
}
167+
168+
return sum;
169+
}
170+
171+
pub fn asin(x: f64) f64 {
172+
let term = x; // The current term in the Taylor series
173+
let sum = x; // The sum of all terms so far
174+
let n = 1; // The current exponent
175+
let x_squared = x * x; // The square of x
176+
177+
while abs(term) > 1e-10 {
178+
n = n + 2;
179+
term = term* (-x_squared * (n - 2) as f64 / n as f64);
180+
sum = sum + term;
181+
}
182+
183+
return sum;
184+
}
185+
186+
var PI = 3.14159265358979323846;
187+
var E = 2.71828182845904523536;
188+
189+
pub fn acos(x: f64) f64 {
190+
return PI / 2.0 - asin(x);
191+
}
192+
193+
pub trait F64Ext {
194+
fn powf(n: f64) f64;
195+
fn ln() f64;
196+
fn exp() f64;
197+
fn powf_int(n: i64) f64;
198+
fn floor() f64;
199+
fn abs() f64;
200+
}
201+
202+
pub fn exp(x: f64) f64 {
203+
let term = 1.0; // The current term in the Taylor series
204+
let sum = 1.0; // The sum of all terms so far
205+
let n = 0; // The current exponent
206+
207+
while abs(term) > 1e-10 {
208+
n = n + 1;
209+
term = term* (x / n as f64);
210+
sum = sum + term;
211+
}
212+
213+
return sum;
214+
}
215+
216+
217+
218+
219+
impl F64Ext for f64 {
220+
fn floor() f64 {
221+
return (*self) as i64 as f64;
222+
}
223+
fn ln() f64 {
224+
return ln(*self);
225+
}
226+
fn exp() f64 {
227+
return exp(*self);
228+
}
229+
fn powf(n: f64) f64 {
230+
// if n is integer, use fast powf_int
231+
if n.floor() == n {
232+
return self.powf_int(n as i64);
233+
}
234+
235+
return exp(n * ln(*self));
236+
}
237+
fn powf_int(n: i64) f64 {
238+
// fast powf_int
239+
let res = 1.0;
240+
let x = *self;
241+
while n > 0 {
242+
if n & 1 == 1 {
243+
res = res * x;
244+
}
245+
x = x * x;
246+
n = n >> 1;
247+
}
248+
return res;
249+
}
250+
fn abs() f64 {
251+
return abs(*self);
252+
}
253+
254+
255+
}
256+
257+
258+
259+
260+
pub fn max<T:Ord<T>>(a: T, b: T) T {
261+
if a.cmp(b)>0 {
262+
return a;
263+
} else {
264+
return b;
265+
}
266+
}
267+
268+
269+
pub fn min<T:Ord<T>>(a: T, b: T) T {
270+
if a.cmp(b)<0 {
271+
return a;
272+
} else {
273+
return b;
274+
}
100275
}

test/test/std_test.pi

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use core::panic;
22
use std::string;
3+
use std::math::F64Ext;
34
pub fn test_std() void {
45
let re = "abcde".index_of("cde");
56
let chars = [1,2,3];
@@ -23,5 +24,13 @@ pub fn test_std() void {
2324
panic::assert(arr2.eq(&[1,3,1]));
2425
arr1.slice(2, 1)[0] = 9;
2526
panic::assert(arr1.eq(&[1,3,9]));
27+
panic::assert( 2.0.powf(2.0)==4.0);
28+
let r = 2.0.powf(2.1);
29+
panic::assert(r==4.287093850143365);
30+
let m = math::max(1, 2);
31+
panic::assert(m==2);
32+
let mi = math::min(1.0, 2.0);
33+
panic::assert(mi==1.0);
34+
panic::assert((math::cos(math::PI) - -1.0).abs() < 0.0000001);
2635
return;
2736
}

0 commit comments

Comments
 (0)