Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
620f8b27
Commit
620f8b27
authored
Oct 22, 2013
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement some Numeric methods in Ruby
parent
3ae06167
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
58 deletions
+35
-58
mrblib/numeric.rb
mrblib/numeric.rb
+34
-0
src/numeric.c
src/numeric.c
+1
-58
No files found.
mrblib/numeric.rb
View file @
620f8b27
##
# Numeric
#
# ISO 15.2.7
class
Numeric
##
# Returns the receiver simply.
#
# ISO 15.2.7.4.1
def
+@
self
end
##
# Returns the receiver's value, negated.
#
# ISO 15.2.7.4.2
def
-@
0
-
self
end
##
# Returns the absolute value of the receiver.
#
# ISO 15.2.7.4.3
def
abs
if
self
<
0
-
self
else
self
end
end
end
##
##
# Integer
# Integer
#
#
...
...
src/numeric.c
View file @
620f8b27
...
@@ -41,38 +41,6 @@ mrb_to_flo(mrb_state *mrb, mrb_value val)
...
@@ -41,38 +41,6 @@ mrb_to_flo(mrb_state *mrb, mrb_value val)
return
mrb_float
(
val
);
return
mrb_float
(
val
);
}
}
/*
* call-seq:
* +num -> num
*
* Unary Plus---Returns the receiver's value.
*/
static
mrb_value
num_uplus
(
mrb_state
*
mrb
,
mrb_value
num
)
{
return
num
;
}
/*
* call-seq:
* -num -> numeric
*
* Unary Minus---Returns the receiver's value, negated.
*/
static
mrb_value
num_uminus
(
mrb_state
*
mrb
,
mrb_value
num
)
{
return
mrb_float_value
(
mrb
,
(
mrb_float
)
0
-
mrb_to_flo
(
mrb
,
num
));
}
static
mrb_value
fix_uminus
(
mrb_state
*
mrb
,
mrb_value
num
)
{
return
mrb_fixnum_value
(
0
-
mrb_fixnum
(
num
));
}
/*
/*
* call-seq:
* call-seq:
*
*
...
@@ -131,27 +99,6 @@ num_div(mrb_state *mrb, mrb_value x)
...
@@ -131,27 +99,6 @@ num_div(mrb_state *mrb, mrb_value x)
return
mrb_float_value
(
mrb
,
mrb_to_flo
(
mrb
,
x
)
/
y
);
return
mrb_float_value
(
mrb
,
mrb_to_flo
(
mrb
,
x
)
/
y
);
}
}
/*
* call-seq:
* num.abs -> numeric
* num.magnitude -> numeric
*
* Returns the absolute value of <i>num</i>.
*
* 12.abs #=> 12
* (-34.56).abs #=> 34.56
* -34.56.abs #=> 34.56
*/
static
mrb_value
num_abs
(
mrb_state
*
mrb
,
mrb_value
num
)
{
if
(
mrb_to_flo
(
mrb
,
num
)
<
0
)
{
return
num_uminus
(
mrb
,
num
);
}
return
num
;
}
/********************************************************************
/********************************************************************
*
*
* Document-class: Float
* Document-class: Float
...
@@ -1325,12 +1272,9 @@ mrb_init_numeric(mrb_state *mrb)
...
@@ -1325,12 +1272,9 @@ mrb_init_numeric(mrb_state *mrb)
numeric
=
mrb_define_class
(
mrb
,
"Numeric"
,
mrb
->
object_class
);
numeric
=
mrb_define_class
(
mrb
,
"Numeric"
,
mrb
->
object_class
);
mrb_include_module
(
mrb
,
numeric
,
mrb_class_get
(
mrb
,
"Comparable"
));
mrb_include_module
(
mrb
,
numeric
,
mrb_class_get
(
mrb
,
"Comparable"
));
mrb_define_method
(
mrb
,
numeric
,
"+@"
,
num_uplus
,
MRB_ARGS_REQ
(
1
));
/* 15.2.7.4.1 */
mrb_define_method
(
mrb
,
numeric
,
"-@"
,
num_uminus
,
MRB_ARGS_REQ
(
1
));
/* 15.2.7.4.2 */
mrb_define_method
(
mrb
,
numeric
,
"**"
,
num_pow
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
numeric
,
"**"
,
num_pow
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
numeric
,
"/"
,
num_div
,
MRB_ARGS_REQ
(
1
));
/* 15.2.8.3.4 */
mrb_define_method
(
mrb
,
numeric
,
"/"
,
num_div
,
MRB_ARGS_REQ
(
1
));
/* 15.2.8.3.4 */
mrb_define_method
(
mrb
,
numeric
,
"quo"
,
num_div
,
MRB_ARGS_REQ
(
1
));
/* 15.2.7.4.5 (x) */
mrb_define_method
(
mrb
,
numeric
,
"quo"
,
num_div
,
MRB_ARGS_REQ
(
1
));
/* 15.2.7.4.5 (x) */
mrb_define_method
(
mrb
,
numeric
,
"abs"
,
num_abs
,
MRB_ARGS_NONE
());
/* 15.2.7.4.3 */
mrb_define_method
(
mrb
,
numeric
,
"<=>"
,
num_cmp
,
MRB_ARGS_REQ
(
1
));
/* 15.2.9.3.6 */
mrb_define_method
(
mrb
,
numeric
,
"<=>"
,
num_cmp
,
MRB_ARGS_REQ
(
1
));
/* 15.2.9.3.6 */
/* Integer Class */
/* Integer Class */
...
@@ -1338,11 +1282,10 @@ mrb_init_numeric(mrb_state *mrb)
...
@@ -1338,11 +1282,10 @@ mrb_init_numeric(mrb_state *mrb)
mrb_undef_class_method
(
mrb
,
integer
,
"new"
);
mrb_undef_class_method
(
mrb
,
integer
,
"new"
);
mrb_define_method
(
mrb
,
integer
,
"to_i"
,
int_to_i
,
MRB_ARGS_NONE
());
/* 15.2.8.3.24 */
mrb_define_method
(
mrb
,
integer
,
"to_i"
,
int_to_i
,
MRB_ARGS_NONE
());
/* 15.2.8.3.24 */
mrb_define_method
(
mrb
,
integer
,
"to_int"
,
int_to_i
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
integer
,
"to_int"
,
int_to_i
,
MRB_ARGS_NONE
());
fixnum
=
mrb
->
fixnum_class
=
mrb_define_class
(
mrb
,
"Fixnum"
,
integer
);
fixnum
=
mrb
->
fixnum_class
=
mrb_define_class
(
mrb
,
"Fixnum"
,
integer
);
mrb_define_method
(
mrb
,
fixnum
,
"+"
,
fix_plus
,
MRB_ARGS_REQ
(
1
));
/* 15.2.8.3.1 */
mrb_define_method
(
mrb
,
fixnum
,
"+"
,
fix_plus
,
MRB_ARGS_REQ
(
1
));
/* 15.2.8.3.1 */
mrb_define_method
(
mrb
,
fixnum
,
"-"
,
fix_minus
,
MRB_ARGS_REQ
(
1
));
/* 15.2.8.3.2 */
mrb_define_method
(
mrb
,
fixnum
,
"-"
,
fix_minus
,
MRB_ARGS_REQ
(
1
));
/* 15.2.8.3.2 */
mrb_define_method
(
mrb
,
fixnum
,
"-@"
,
fix_uminus
,
MRB_ARGS_REQ
(
1
));
/* 15.2.7.4.2 */
mrb_define_method
(
mrb
,
fixnum
,
"*"
,
fix_mul
,
MRB_ARGS_REQ
(
1
));
/* 15.2.8.3.3 */
mrb_define_method
(
mrb
,
fixnum
,
"*"
,
fix_mul
,
MRB_ARGS_REQ
(
1
));
/* 15.2.8.3.3 */
mrb_define_method
(
mrb
,
fixnum
,
"%"
,
fix_mod
,
MRB_ARGS_REQ
(
1
));
/* 15.2.8.3.5 */
mrb_define_method
(
mrb
,
fixnum
,
"%"
,
fix_mod
,
MRB_ARGS_REQ
(
1
));
/* 15.2.8.3.5 */
mrb_define_method
(
mrb
,
fixnum
,
"=="
,
fix_equal
,
MRB_ARGS_REQ
(
1
));
/* 15.2.8.3.7 */
mrb_define_method
(
mrb
,
fixnum
,
"=="
,
fix_equal
,
MRB_ARGS_REQ
(
1
));
/* 15.2.8.3.7 */
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment