Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
02b7089e
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
02b7089e
编写于
6月 07, 2012
作者:
P
Patrick Walton
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
libcore: Add a num typeclass
上级
3d7400f3
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
73 addition
and
0 deletion
+73
-0
src/libcore/f32.rs
src/libcore/f32.rs
+13
-0
src/libcore/f64.rs
src/libcore/f64.rs
+13
-0
src/libcore/float.rs
src/libcore/float.rs
+32
-0
src/libcore/num.rs
src/libcore/num.rs
+15
-0
未找到文件。
src/libcore/f32.rs
浏览文件 @
02b7089e
...
...
@@ -4,6 +4,7 @@
import
cmath
::
c_float
::
*
;
import
cmath
::
c_float_targ_consts
::
*
;
import
num
::
num
;
// FIXME find out why these have to be exported explicitly
...
...
@@ -19,6 +20,7 @@
export
lgamma
,
ln
,
log_radix
,
ln1p
,
log10
,
log2
,
ilog_radix
;
export
modf
,
pow
,
round
,
sin
,
sinh
,
sqrt
,
tan
,
tanh
,
tgamma
,
trunc
;
export
signbit
;
export
num
;
// These are not defined inside consts:: for consistency with
// the integer types
...
...
@@ -176,6 +178,17 @@ mod consts {
ret
ln
(
n
)
/
consts
::
ln_2
;
}
impl
num
of
num
for
f32
{
fn
add
(
&&
other
:
f32
)
->
f32
{
ret
self
+
other
;
}
fn
sub
(
&&
other
:
f32
)
->
f32
{
ret
self
-
other
;
}
fn
mul
(
&&
other
:
f32
)
->
f32
{
ret
self
*
other
;
}
fn
div
(
&&
other
:
f32
)
->
f32
{
ret
self
/
other
;
}
fn
modulo
(
&&
other
:
f32
)
->
f32
{
ret
self
%
other
;
}
fn
to_int
()
->
int
{
ret
self
as
int
;
}
fn
from_int
(
n
:
int
)
->
f32
{
ret
n
as
f32
;
}
}
//
// Local Variables:
// mode: rust
...
...
src/libcore/f64.rs
浏览文件 @
02b7089e
...
...
@@ -4,6 +4,7 @@
import
cmath
::
c_double
::
*
;
import
cmath
::
c_double_targ_consts
::
*
;
import
num
::
num
;
// Even though this module exports everything defined in it,
// because it contains re-exports, we also have to explicitly
...
...
@@ -21,6 +22,7 @@
export
modf
,
pow
,
round
,
sin
,
sinh
,
sqrt
,
tan
,
tanh
,
tgamma
,
trunc
;
export
signbit
;
export
epsilon
;
export
num
;
// These are not defined inside consts:: for consistency with
// the integer types
...
...
@@ -197,6 +199,17 @@ mod consts {
ret
ln
(
n
)
/
consts
::
ln_2
;
}
impl
num
of
num
for
f64
{
fn
add
(
&&
other
:
f64
)
->
f64
{
ret
self
+
other
;
}
fn
sub
(
&&
other
:
f64
)
->
f64
{
ret
self
-
other
;
}
fn
mul
(
&&
other
:
f64
)
->
f64
{
ret
self
*
other
;
}
fn
div
(
&&
other
:
f64
)
->
f64
{
ret
self
/
other
;
}
fn
modulo
(
&&
other
:
f64
)
->
f64
{
ret
self
%
other
;
}
fn
to_int
()
->
int
{
ret
self
as
int
;
}
fn
from_int
(
n
:
int
)
->
f64
{
ret
n
as
f64
;
}
}
//
// Local Variables:
// mode: rust
...
...
src/libcore/float.rs
浏览文件 @
02b7089e
...
...
@@ -17,6 +17,7 @@
export
modf
,
pow
,
round
,
sin
,
sinh
,
sqrt
,
tan
,
tanh
,
tgamma
,
trunc
;
export
signbit
;
export
pow_with_uint
;
export
num
;
// export when m_float == c_double
...
...
@@ -26,6 +27,7 @@
import
m_float
=
f64
;
import
f64
::
*
;
import
num
::
num
;
const
NaN
:
float
=
0.0
/
0.0
;
...
...
@@ -408,6 +410,17 @@ fn sin(x: float) -> float { f64::sin(x as f64) as float }
fn
cos
(
x
:
float
)
->
float
{
f64
::
cos
(
x
as
f64
)
as
float
}
fn
tan
(
x
:
float
)
->
float
{
f64
::
tan
(
x
as
f64
)
as
float
}
impl
num
of
num
for
float
{
fn
add
(
&&
other
:
float
)
->
float
{
ret
self
+
other
;
}
fn
sub
(
&&
other
:
float
)
->
float
{
ret
self
-
other
;
}
fn
mul
(
&&
other
:
float
)
->
float
{
ret
self
*
other
;
}
fn
div
(
&&
other
:
float
)
->
float
{
ret
self
/
other
;
}
fn
modulo
(
&&
other
:
float
)
->
float
{
ret
self
%
other
;
}
fn
to_int
()
->
int
{
ret
self
as
int
;
}
fn
from_int
(
n
:
int
)
->
float
{
ret
n
as
float
;
}
}
#[test]
fn
test_from_str
()
{
assert
from_str
(
"3"
)
==
some
(
3
.
);
...
...
@@ -501,6 +514,25 @@ fn test_to_str_inf() {
assert
to_str
(
-
infinity
,
10u
)
==
"-inf"
;
}
#[test]
fn
test_ifaces
()
{
fn
test
<
U
:
num
>
(
ten
:
U
)
{
assert
(
ten
.to_int
()
==
10
);
let
two
=
ten
.from_int
(
2
);
assert
(
two
.to_int
()
==
2
);
assert
(
ten
.add
(
two
)
==
ten
.from_int
(
12
));
assert
(
ten
.sub
(
two
)
==
ten
.from_int
(
8
));
assert
(
ten
.mul
(
two
)
==
ten
.from_int
(
20
));
assert
(
ten
.div
(
two
)
==
ten
.from_int
(
5
));
assert
(
ten
.modulo
(
two
)
==
ten
.from_int
(
0
));
}
test
(
10.0
);
}
//
// Local Variables:
// mode: rust
...
...
src/libcore/num.rs
0 → 100644
浏览文件 @
02b7089e
#[doc=
"An interface for numbers."
]
iface
num
{
// FIXME: Cross-crate overloading doesn't work yet.
// FIXME: Interface inheritance.
fn
add
(
&&
other
:
self
)
->
self
;
fn
sub
(
&&
other
:
self
)
->
self
;
fn
mul
(
&&
other
:
self
)
->
self
;
fn
div
(
&&
other
:
self
)
->
self
;
fn
modulo
(
&&
other
:
self
)
->
self
;
fn
to_int
()
->
int
;
fn
from_int
(
n
:
int
)
->
self
;
// TODO: Static functions.
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录