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
9ed212ef
Unverified
Commit
9ed212ef
authored
4 years ago
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
complex.c: implement `Complex` addition and subtraction in C.
parent
fa3ac351
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
16 deletions
+44
-16
mrbgems/mruby-complex/mrblib/complex.rb
mrbgems/mruby-complex/mrblib/complex.rb
+0
-16
mrbgems/mruby-complex/src/complex.c
mrbgems/mruby-complex/src/complex.c
+44
-0
No files found.
mrbgems/mruby-complex/mrblib/complex.rb
View file @
9ed212ef
...
...
@@ -19,22 +19,6 @@ class Complex < Numeric
Complex
(
-
real
,
-
imaginary
)
end
def
+
(
rhs
)
if
rhs
.
is_a?
Complex
Complex
(
real
+
rhs
.
real
,
imaginary
+
rhs
.
imaginary
)
elsif
rhs
.
is_a?
Numeric
Complex
(
real
+
rhs
,
imaginary
)
end
end
def
-
(
rhs
)
if
rhs
.
is_a?
Complex
Complex
(
real
-
rhs
.
real
,
imaginary
-
rhs
.
imaginary
)
elsif
rhs
.
is_a?
Numeric
Complex
(
real
-
rhs
,
imaginary
)
end
end
def
abs
Math
.
hypot
imaginary
,
real
end
...
...
This diff is collapsed.
Click to expand it.
mrbgems/mruby-complex/src/complex.c
View file @
9ed212ef
...
...
@@ -171,6 +171,48 @@ complex_eq(mrb_state *mrb, mrb_value x)
return
mrb_bool_value
(
mrb_complex_eq
(
mrb
,
x
,
y
));
}
static
mrb_value
complex_add
(
mrb_state
*
mrb
,
mrb_value
x
)
{
mrb_value
y
=
mrb_get_arg1
(
mrb
);
struct
mrb_complex
*
p1
=
complex_ptr
(
mrb
,
x
);
switch
(
mrb_type
(
y
))
{
case
MRB_TT_COMPLEX
:
{
struct
mrb_complex
*
p2
=
complex_ptr
(
mrb
,
y
);
return
mrb_complex_new
(
mrb
,
p1
->
real
+
p2
->
real
,
p1
->
imaginary
+
p2
->
imaginary
);
}
default:
{
mrb_float
z
=
mrb_to_flo
(
mrb
,
y
);
return
mrb_complex_new
(
mrb
,
p1
->
real
+
z
,
p1
->
imaginary
);
}
}
}
static
mrb_value
complex_sub
(
mrb_state
*
mrb
,
mrb_value
x
)
{
mrb_value
y
=
mrb_get_arg1
(
mrb
);
struct
mrb_complex
*
p1
=
complex_ptr
(
mrb
,
x
);
switch
(
mrb_type
(
y
))
{
case
MRB_TT_COMPLEX
:
{
struct
mrb_complex
*
p2
=
complex_ptr
(
mrb
,
y
);
return
mrb_complex_new
(
mrb
,
p1
->
real
-
p2
->
real
,
p1
->
imaginary
-
p2
->
imaginary
);
}
default:
{
mrb_float
z
=
mrb_to_flo
(
mrb
,
y
);
return
mrb_complex_new
(
mrb
,
p1
->
real
-
z
,
p1
->
imaginary
);
}
}
}
static
mrb_value
complex_mul
(
mrb_state
*
mrb
,
mrb_value
x
)
{
...
...
@@ -386,6 +428,8 @@ void mrb_mruby_complex_gem_init(mrb_state *mrb)
mrb_define_method
(
mrb
,
comp
,
"to_f"
,
complex_to_f
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
comp
,
"to_i"
,
complex_to_i
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
comp
,
"to_c"
,
complex_to_c
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
comp
,
"+"
,
complex_add
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
comp
,
"-"
,
complex_sub
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
comp
,
"*"
,
complex_mul
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
comp
,
"/"
,
complex_div
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
comp
,
"quo"
,
complex_div
,
MRB_ARGS_REQ
(
1
));
...
...
This diff is collapsed.
Click to expand it.
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