Skip to content

Commit 6d0901c

Browse files
bogglegraydon
authored andcommitted
std: export math_f* as math::f* submods and use tailcalls in std::math
1 parent 6cdb69c commit 6d0901c

File tree

5 files changed

+45
-41
lines changed

5 files changed

+45
-41
lines changed

src/libstd/cmath.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ native mod f64 {
2323
#[link_name="log1p"] pure fn ln1p(n: f64) -> f64;
2424
pure fn log10(n: f64) -> f64;
2525
pure fn log2(n: f64) -> f64;
26-
pure fn modf(n: f64, &iptr: f64) -> f64;
26+
pure fn modf(n: f64, iptr: *f64) -> f64;
2727
pure fn pow(n: f64, e: f64) -> f64;
2828
pure fn rint(n: f64) -> f64;
2929
pure fn round(n: f64) -> f64;
@@ -58,7 +58,7 @@ native mod f32 {
5858
#[link_name="log1p"] pure fn ln1p(n: f64) -> f64;
5959
#[link_name="log2f"] pure fn log2(n: f32) -> f32;
6060
#[link_name="log10f"] pure fn log10(n: f32) -> f32;
61-
#[link_name="modff"] pure fn modf(n: f32, &iptr: f32) -> f32;
61+
#[link_name="modff"] pure fn modf(n: f32, iptr: *f32) -> f32;
6262
#[link_name="powf"] pure fn pow(n: f32, e: f32) -> f32;
6363
#[link_name="rintf"] pure fn rint(n: f32) -> f32;
6464
#[link_name="roundf"] pure fn round(n: f32) -> f32;

src/libstd/math.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ export
1616
ldexp, ln, ln1p, log10, log2, modf, rint, round, pow, sin, sinh, sqrt,
1717
tan, tanh, trunc;
1818

19+
export f64, f32;
20+
21+
import f64 = math_f64;
22+
import f32 = math_f32;
23+
1924
// These two must match in width according to architecture
2025

2126
import ctypes::m_float;
2227
import ctypes::c_int;
28+
import ptr;
2329
import m_float = math_f64;
2430

25-
// FIXME replace with redirect to m_float::consts::FOO as soon as it works
2631
/*
2732
Module: consts
2833
*/
@@ -143,23 +148,23 @@ Function: acos
143148
Returns the arccosine of an angle (measured in rad)
144149
*/
145150
pure fn acos(x: float) -> float
146-
{ m_float::acos(x as m_float) as float }
151+
{ be m_float::acos(x as m_float) as float }
147152

148153
/*
149154
Function: asin
150155
151156
Returns the arcsine of an angle (measured in rad)
152157
*/
153158
pure fn asin(x: float) -> float
154-
{ m_float::asin(x as m_float) as float }
159+
{ be m_float::asin(x as m_float) as float }
155160

156161
/*
157162
Function: atan
158163
159164
Returns the arctangents of an angle (measured in rad)
160165
*/
161166
pure fn atan(x: float) -> float
162-
{ m_float::atan(x as m_float) as float }
167+
{ be m_float::atan(x as m_float) as float }
163168

164169

165170
/*
@@ -168,23 +173,23 @@ Function: atan2
168173
Returns the arctangent of an angle (measured in rad)
169174
*/
170175
pure fn atan2(y: float, x: float) -> float
171-
{ m_float::atan2(y as m_float, x as m_float) as float }
176+
{ be m_float::atan2(y as m_float, x as m_float) as float }
172177

173178
/*
174179
Function: ceil
175180
176181
Returns the smallest integral value less than or equal to `n`
177182
*/
178183
pure fn ceil(n: float) -> float
179-
{ m_float::ceil(n as m_float) as float }
184+
{ be m_float::ceil(n as m_float) as float }
180185

181186
/*
182187
Function: cos
183188
184189
Returns the cosine of an angle `x` (measured in rad)
185190
*/
186191
pure fn cos(x: float) -> float
187-
{ m_float::cos(x as m_float) as float }
192+
{ be m_float::cos(x as m_float) as float }
188193

189194
/*
190195
Function: cosh
@@ -193,7 +198,7 @@ Returns the hyperbolic cosine of `x`
193198
194199
*/
195200
pure fn cosh(x: float) -> float
196-
{ m_float::cosh(x as m_float) as float }
201+
{ be m_float::cosh(x as m_float) as float }
197202

198203

199204
/*
@@ -202,47 +207,47 @@ Function: exp
202207
Returns `consts::e` to the power of `n*
203208
*/
204209
pure fn exp(n: float) -> float
205-
{ m_float::exp(n as m_float) as float }
210+
{ be m_float::exp(n as m_float) as float }
206211

207212
/*
208213
Function: abs
209214
210215
Returns the absolute value of `n`
211216
*/
212217
pure fn abs(n: float) -> float
213-
{ m_float::abs(n as m_float) as float }
218+
{ be m_float::abs(n as m_float) as float }
214219

215220
/*
216221
Function: floor
217222
218223
Returns the largest integral value less than or equal to `n`
219224
*/
220225
pure fn floor(n: float) -> float
221-
{ m_float::floor(n as m_float) as float }
226+
{ be m_float::floor(n as m_float) as float }
222227

223228
/*
224229
Function: fmod
225230
226231
Returns the floating-point remainder of `x/y`
227232
*/
228233
pure fn fmod(x: float, y: float) -> float
229-
{ m_float::fmod(x as m_float, y as m_float) as float }
234+
{ be m_float::fmod(x as m_float, y as m_float) as float }
230235

231236
/*
232237
Function: ln
233238
234239
Returns the natural logaritm of `n`
235240
*/
236241
pure fn ln(n: float) -> float
237-
{ m_float::ln(n as m_float) as float }
242+
{ be m_float::ln(n as m_float) as float }
238243

239244
/*
240245
Function: ldexp
241246
242247
Returns `x` multiplied by 2 to the power of `n`
243248
*/
244249
pure fn ldexp(n: float, i: int) -> float
245-
{ m_float::ldexp(n as m_float, i as c_int) as float }
250+
{ be m_float::ldexp(n as m_float, i as c_int) as float }
246251

247252
/*
248253
Function: ln1p
@@ -251,24 +256,23 @@ Returns the natural logarithm of `1+n` accurately,
251256
even for very small values of `n`
252257
*/
253258
pure fn ln1p(n: float) -> float
254-
{ m_float::ln1p(n as m_float) as float }
259+
{ be m_float::ln1p(n as m_float) as float }
255260

256261
/*
257262
Function: log10
258263
259264
Returns the logarithm to base 10 of `n`
260265
*/
261266
pure fn log10(n: float) -> float
262-
{ m_float::log10(n as m_float) as float }
267+
{ be m_float::log10(n as m_float) as float }
263268

264269
/*
265270
Function: log2
266271
267272
Returns the logarithm to base 2 of `n`
268273
*/
269274
pure fn log2(n: float) -> float
270-
{ m_float::log2(n as m_float) as float }
271-
275+
{ be m_float::log2(n as m_float) as float }
272276

273277
/*
274278
Function: modf
@@ -282,14 +286,10 @@ Returns:
282286
283287
The fractional part of `n`
284288
*/
285-
pure fn modf(n: float, &iptr: float) -> float {
286-
unchecked {
287-
let f = iptr as m_float;
288-
let r = m_float::modf(n as m_float, f) as float;
289-
iptr = f as float;
290-
ret r;
291-
}
292-
}
289+
#[no(warn_trivial_casts)] // FIXME Implement
290+
pure fn modf(n: float, &iptr: float) -> float { unsafe {
291+
be m_float::modf(n as m_float, ptr::addr_of(iptr) as *m_float) as float
292+
} }
293293

294294
/*
295295
Function: frexp
@@ -306,13 +306,13 @@ Returns:
306306
The fractional part of `n`
307307
*/
308308
pure fn frexp(n: float, &exp: c_int) -> float
309-
{ m_float::frexp(n as m_float, exp) as float }
309+
{ be m_float::frexp(n as m_float, exp) as float }
310310

311311
/*
312312
Function: pow
313313
*/
314314
pure fn pow(v: float, e: float) -> float
315-
{ m_float::pow(v as m_float, e as m_float) as float }
315+
{ be m_float::pow(v as m_float, e as m_float) as float }
316316

317317

318318
/*
@@ -322,7 +322,7 @@ Returns the integral value nearest to `x` (according to the
322322
prevailing rounding mode) in floating-point format
323323
*/
324324
pure fn rint(x: float) -> float
325-
{ m_float::rint(x as m_float) as float }
325+
{ be m_float::rint(x as m_float) as float }
326326

327327
/*
328328
Function: round
@@ -332,31 +332,31 @@ Return the integral value nearest to `x` rounding half-way
332332
cases away from zero, regardless of the current rounding direction.
333333
*/
334334
pure fn round(x: float) -> float
335-
{ m_float::round(x as m_float) as float }
335+
{ be m_float::round(x as m_float) as float }
336336

337337
/*
338338
Function: sin
339339
340340
Returns the sine of an angle `x` (measured in rad)
341341
*/
342342
pure fn sin(x: float) -> float
343-
{ m_float::sin(x as m_float) as float }
343+
{ be m_float::sin(x as m_float) as float }
344344

345345
/*
346346
Function: sinh
347347
348348
Returns the hyperbolic sine of an angle `x` (measured in rad)
349349
*/
350350
pure fn sinh(x: float) -> float
351-
{ m_float::sinh(x as m_float) as float }
351+
{ be m_float::sinh(x as m_float) as float }
352352

353353
/*
354354
Function: sqrt
355355
356356
Returns the square root of `x`
357357
*/
358358
pure fn sqrt(x: float) -> float
359-
{ m_float::sqrt(x as m_float) as float }
359+
{ be m_float::sqrt(x as m_float) as float }
360360

361361
/*
362362
Function: tan
@@ -365,7 +365,7 @@ Returns the tangent of an angle `x` (measured in rad)
365365
366366
*/
367367
pure fn tan(x: float) -> float
368-
{ m_float::tan(x as m_float) as float }
368+
{ be m_float::tan(x as m_float) as float }
369369

370370
/*
371371
Function: tanh
@@ -374,7 +374,7 @@ Returns the hyperbolic tangent of an angle `x` (measured in rad)
374374
375375
*/
376376
pure fn tanh(x: float) -> float
377-
{ m_float::tanh(x as m_float) as float }
377+
{ be m_float::tanh(x as m_float) as float }
378378

379379
/*
380380
Function: trunc
@@ -383,7 +383,7 @@ Returns the integral value nearest to but no larger in magnitude than `x`
383383
384384
*/
385385
pure fn trunc(x: float) -> float
386-
{ m_float::trunc(x as m_float) as float }
386+
{ be m_float::trunc(x as m_float) as float }
387387

388388

389389

src/libstd/math_f64.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export
1717

1818
export consts;
1919

20+
2021
/* Module: consts */
2122
mod consts {
2223

src/libstd/std.rc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export comm, fs, io, net, run, sys, task, uv;
1313
export c_vec, ctypes, either, option, result, four, tri, util;
1414
export bitv, deque, fun_treemap, list, map, smallintmap, sort, treemap, ufind;
1515
export rope;
16-
export math, math_f32, math_f64;
16+
export math;
1717
export ebml, dbg, getopts, json, rand, sha1, term, time, unsafe;
1818
export extfmt, test, tempfile;
1919
// FIXME: generic_os and os_fs shouldn't be exported
@@ -54,6 +54,8 @@ mod uv;
5454
mod c_vec;
5555
mod ctypes;
5656
mod cmath; /* unexported */
57+
mod math_f32;
58+
mod math_f64;
5759
mod either;
5860
mod option;
5961
mod result;
@@ -83,8 +85,6 @@ mod dbg;
8385
mod getopts;
8486
mod json;
8587
mod math;
86-
mod math_f32;
87-
mod math_f64;
8888
mod rand;
8989
mod sha1;
9090
mod tempfile;

src/test/stdtest/math.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,11 @@ fn test_exp_and_mod() {
160160

161161
let d2: float = 1.0;
162162
assert modf(float::infinity, d2) == 0.0;
163+
assert d2 == float::infinity;
163164
assert modf(float::neg_infinity, d2) == -0.0;
165+
assert d2 == float::neg_infinity;
164166
assert float::isNaN(modf(float::NaN, d2));
167+
assert float::isNaN(d2);
165168
}
166169

167170
#[test]

0 commit comments

Comments
 (0)