Skip to content

Commit c3e4083

Browse files
bogglebrson
authored andcommitted
---
yaml --- r: 6462 b: refs/heads/master c: a611496 h: refs/heads/master v: v3
1 parent 42674a4 commit c3e4083

File tree

3 files changed

+337
-32
lines changed

3 files changed

+337
-32
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: bd405fb457a42a02e85fc116192ba663ac791146
2+
refs/heads/master: a611496ddfb3f510635d9921dd1463cc3512d84d

trunk/src/lib/math.rs

Lines changed: 111 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ export min, max;
55

66
export f32, f64;
77

8-
// Currently this module supports from -lmath
9-
// C95 - frexp - ldexp - fmod - modf + log2 + log1p
8+
// Currently this module supports from -lmath:
9+
// C95 + log2 + log1p + trunc + round + rint
1010

1111
export
12-
acos, asin, atan, atan2, ceil, cos, cosh, exp, abs, floor,
13-
ln, ln1p, log10, log2, pow, sin, sinh, sqrt, tan, tanh;
12+
acos, asin, atan, atan2, ceil, cos, cosh, exp, abs, floor, fmod, frexp,
13+
ldexp, ln, ln1p, log10, log2, modf, rint, round, pow, sin, sinh, sqrt,
14+
tan, tanh, trunc;
1415

1516
// These two must match in width according to architecture
1617

1718
import ctypes::c_float;
19+
import ctypes::c_int;
1820
import c_float = f64;
1921

2022

@@ -34,16 +36,23 @@ native mod f64 {
3436
pure fn exp(n: f64) -> f64;
3537
#[link_name="fabs"] pure fn abs(n: f64) -> f64;
3638
pure fn floor(n: f64) -> f64;
39+
pure fn fmod(x: f64, y: f64) -> f64;
40+
pure fn frexp(n: f64, &value: c_int) -> f64;
41+
pure fn ldexp(x: f64, n: c_int) -> f64;
3742
#[link_name="log"] pure fn ln(n: f64) -> f64;
3843
#[link_name="log1p"] pure fn ln1p(n: f64) -> f64;
3944
pure fn log10(n: f64) -> f64;
4045
pure fn log2(n: f64) -> f64;
46+
pure fn modf(n: f64, &iptr: f64) -> f64;
4147
pure fn pow(n: f64, e: f64) -> f64;
48+
pure fn rint(n: f64) -> f64;
49+
pure fn round(n: f64) -> f64;
4250
pure fn sin(n: f64) -> f64;
4351
pure fn sinh(n: f64) -> f64;
4452
pure fn sqrt(n: f64) -> f64;
4553
pure fn tan(n: f64) -> f64;
4654
pure fn tanh(n: f64) -> f64;
55+
pure fn trunc(n: f64) -> f64;
4756
}
4857

4958
#[link_name = "m"]
@@ -62,19 +71,25 @@ native mod f32 {
6271
#[link_name="expf"] pure fn exp(n: f32) -> f32;
6372
#[link_name="fabsf"] pure fn abs(n: f32) -> f32;
6473
#[link_name="floorf"] pure fn floor(n: f32) -> f32;
74+
#[link_name="frexpf"] pure fn frexp(n: f64, &value: c_int) -> f32;
75+
#[link_name="fmodf"] pure fn fmod(x: f32, y: f32) -> f32;
76+
#[link_name="ldexpf"] pure fn ldexp(x: f32, n: c_int) -> f32;
77+
#[link_name="logf"] pure fn ln(n: f32) -> f32;
78+
#[link_name="log1p"] pure fn ln1p(n: f64) -> f64;
79+
#[link_name="log2f"] pure fn log2(n: f32) -> f32;
80+
#[link_name="log10f"] pure fn log10(n: f32) -> f32;
81+
#[link_name="modff"] pure fn modf(n: f32, &iptr: f32) -> f32;
6582
#[link_name="powf"] pure fn pow(n: f32, e: f32) -> f32;
83+
#[link_name="rintf"] pure fn rint(n: f32) -> f32;
84+
#[link_name="roundf"] pure fn round(n: f32) -> f32;
6685
#[link_name="sinf"] pure fn sin(n: f32) -> f32;
6786
#[link_name="sinhf"] pure fn sinh(n: f32) -> f32;
6887
#[link_name="sqrtf"] pure fn sqrt(n: f32) -> f32;
6988
#[link_name="tanf"] pure fn tan(n: f32) -> f32;
7089
#[link_name="tanhf"] pure fn tanh(n: f32) -> f32;
71-
#[link_name="logf"] pure fn ln(n: f32) -> f32;
72-
#[link_name="log1p"] pure fn ln1p(n: f64) -> f64;
73-
#[link_name="log2f"] pure fn log2(n: f32) -> f32;
74-
#[link_name="log10f"] pure fn log10(n: f32) -> f32;
90+
#[link_name="truncf"] pure fn trunc(n: f32) -> f32;
7591
}
7692

77-
7893
mod consts {
7994
/*
8095
Const: pi
@@ -219,9 +234,7 @@ pure fn atan2(y: float, x: float) -> float
219234
/*
220235
Function: ceil
221236
222-
Returns:
223-
224-
The smallest integral value less than or equal to `n`
237+
Returns the smallest integral value less than or equal to `n`
225238
*/
226239
pure fn ceil(n: float) -> float
227240
{ c_float::ceil(n as c_float) as float }
@@ -247,34 +260,35 @@ pure fn cosh(x: float) -> float
247260
/*
248261
Function: exp
249262
250-
Returns:
251-
252-
e to the power of `n*
263+
Returns `consts::e` to the power of `n*
253264
*/
254265
pure fn exp(n: float) -> float
255266
{ c_float::exp(n as c_float) as float }
256267

257268
/*
258269
Function: abs
259270
260-
Returns:
261-
262-
The absolute value of `n`
263-
271+
Returns the absolute value of `n`
264272
*/
265273
pure fn abs(n: float) -> float
266274
{ c_float::abs(n as c_float) as float }
267275

268276
/*
269277
Function: floor
270278
271-
Returns:
272-
273-
The largest integral value less than or equal to `n`
279+
Returns the largest integral value less than or equal to `n`
274280
*/
275281
pure fn floor(n: float) -> float
276282
{ c_float::floor(n as c_float) as float }
277283

284+
/*
285+
Function: fmod
286+
287+
Returns the floating-point remainder of `x/y`
288+
*/
289+
pure fn fmod(x: float, y: float) -> float
290+
{ c_float::fmod(x as c_float, y as c_float) as float }
291+
278292
/*
279293
Function: ln
280294
@@ -283,6 +297,14 @@ Returns the natural logaritm of `n`
283297
pure fn ln(n: float) -> float
284298
{ c_float::ln(n as c_float) as float }
285299

300+
/*
301+
Function: ldexp
302+
303+
Returns `x` multiplied by 2 to the power of `n`
304+
*/
305+
pure fn ldexp(n: float, i: int) -> float
306+
{ c_float::ldexp(n as c_float, i as c_int) as float }
307+
286308
/*
287309
Function: ln1p
288310
@@ -308,13 +330,71 @@ Returns the logarithm to base 2 of `n`
308330
pure fn log2(n: float) -> float
309331
{ c_float::log2(n as c_float) as float }
310332

333+
334+
/*
335+
Function: modf
336+
337+
Breaks `n` into integral and fractional parts such that both
338+
have the same sign as `n`
339+
340+
The integral part is stored in `iptr`.
341+
342+
Returns:
343+
344+
The fractional part of `n`
345+
*/
346+
pure fn modf(n: float, &iptr: float) -> float {
347+
unchecked {
348+
let f = iptr as c_float;
349+
let r = c_float::modf(n as c_float, f) as float;
350+
iptr = f as float;
351+
ret r;
352+
}
353+
}
354+
355+
/*
356+
Function: frexp
357+
358+
Breaks `n` into a normalized fraction and an integral power of 2
359+
360+
The inegral part is stored in iptr.
361+
362+
The functions return a number x such that x has a magnitude in the interval
363+
[1/2, 1) or 0, and `n == x*(2 to the power of exp)`.
364+
365+
Returns:
366+
367+
The fractional part of `n`
368+
*/
369+
pure fn frexp(n: float, &exp: c_int) -> float
370+
{ c_float::frexp(n as c_float, exp) as float }
371+
311372
/*
312373
Function: pow
313374
*/
314375
pure fn pow(v: float, e: float) -> float
315376
{ c_float::pow(v as c_float, e as c_float) as float }
316377

317378

379+
/*
380+
Function: rint
381+
382+
Returns the integral value nearest to `x` (according to the
383+
prevailing rounding mode) in floating-point format
384+
*/
385+
pure fn rint(x: float) -> float
386+
{ c_float::rint(x as c_float) as float }
387+
388+
/*
389+
Function: round
390+
391+
392+
Return the integral value nearest to `x` rounding half-way
393+
cases away from zero, regardless of the current rounding direction.
394+
*/
395+
pure fn round(x: float) -> float
396+
{ c_float::round(x as c_float) as float }
397+
318398
/*
319399
Function: sin
320400
@@ -357,6 +437,15 @@ Returns the hyperbolic tangent of an angle `x` (measured in rad)
357437
pure fn tanh(x: float) -> float
358438
{ c_float::tanh(x as c_float) as float }
359439

440+
/*
441+
Function: trunc
442+
443+
Returns the integral value nearest to but no larger in magnitude than `x`
444+
445+
*/
446+
pure fn trunc(x: float) -> float
447+
{ c_float::trunc(x as c_float) as float }
448+
360449

361450

362451

0 commit comments

Comments
 (0)