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
c8b00d01
Commit
c8b00d01
authored
May 03, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2177 from cremno/math-domain-error
mruby-math: define and use `Math::DomainError`
parents
67bf3f14
405e8fbc
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
2 deletions
+37
-2
mrbgems/mruby-math/src/math.c
mrbgems/mruby-math/src/math.c
+37
-2
No files found.
mrbgems/mruby-math/src/math.c
View file @
c8b00d01
...
...
@@ -9,8 +9,14 @@
#include <math.h>
#define domain_error(msg) \
mrb_raise(mrb, E_RANGE_ERROR, "Numerical argument is out of domain - " #msg)
static
void
domain_error
(
mrb_state
*
mrb
,
const
char
*
func
)
{
struct
RClass
*
math
=
mrb_module_get
(
mrb
,
"Math"
);
struct
RClass
*
domainerror
=
mrb_class_get_under
(
mrb
,
math
,
"DomainError"
);
mrb_value
str
=
mrb_str_new_cstr
(
mrb
,
func
);
mrb_raisef
(
mrb
,
domainerror
,
"Numerical argument is out of domain - %S"
,
str
);
}
/* math functions not provided by Microsoft Visual C++ 2012 or older */
#if defined _MSC_VER && _MSC_VER < 1800
...
...
@@ -172,6 +178,9 @@ math_asin(mrb_state *mrb, mrb_value obj)
mrb_float
x
;
mrb_get_args
(
mrb
,
"f"
,
&
x
);
if
(
x
<
-
1
.
0
||
x
>
1
.
0
)
{
domain_error
(
mrb
,
"asin"
);
}
x
=
asin
(
x
);
return
mrb_float_value
(
mrb
,
x
);
...
...
@@ -189,6 +198,9 @@ math_acos(mrb_state *mrb, mrb_value obj)
mrb_float
x
;
mrb_get_args
(
mrb
,
"f"
,
&
x
);
if
(
x
<
-
1
.
0
||
x
>
1
.
0
)
{
domain_error
(
mrb
,
"acos"
);
}
x
=
acos
(
x
);
return
mrb_float_value
(
mrb
,
x
);
...
...
@@ -334,6 +346,9 @@ math_acosh(mrb_state *mrb, mrb_value obj)
mrb_float
x
;
mrb_get_args
(
mrb
,
"f"
,
&
x
);
if
(
x
<
1
.
0
)
{
domain_error
(
mrb
,
"acosh"
);
}
x
=
acosh
(
x
);
return
mrb_float_value
(
mrb
,
x
);
...
...
@@ -351,6 +366,9 @@ math_atanh(mrb_state *mrb, mrb_value obj)
mrb_float
x
;
mrb_get_args
(
mrb
,
"f"
,
&
x
);
if
(
x
<
-
1
.
0
||
x
>
1
.
0
)
{
domain_error
(
mrb
,
"atanh"
);
}
x
=
atanh
(
x
);
return
mrb_float_value
(
mrb
,
x
);
...
...
@@ -404,8 +422,14 @@ math_log(mrb_state *mrb, mrb_value obj)
int
argc
;
argc
=
mrb_get_args
(
mrb
,
"f|f"
,
&
x
,
&
base
);
if
(
x
<
0
.
0
)
{
domain_error
(
mrb
,
"log"
);
}
x
=
log
(
x
);
if
(
argc
==
2
)
{
if
(
base
<
0
.
0
)
{
domain_error
(
mrb
,
"log"
);
}
x
/=
log
(
base
);
}
return
mrb_float_value
(
mrb
,
x
);
...
...
@@ -429,6 +453,9 @@ math_log2(mrb_state *mrb, mrb_value obj)
mrb_float
x
;
mrb_get_args
(
mrb
,
"f"
,
&
x
);
if
(
x
<
0
.
0
)
{
domain_error
(
mrb
,
"log2"
);
}
x
=
log2
(
x
);
return
mrb_float_value
(
mrb
,
x
);
...
...
@@ -451,6 +478,9 @@ math_log10(mrb_state *mrb, mrb_value obj)
mrb_float
x
;
mrb_get_args
(
mrb
,
"f"
,
&
x
);
if
(
x
<
0
.
0
)
{
domain_error
(
mrb
,
"log10"
);
}
x
=
log10
(
x
);
return
mrb_float_value
(
mrb
,
x
);
...
...
@@ -469,6 +499,9 @@ math_sqrt(mrb_state *mrb, mrb_value obj)
mrb_float
x
;
mrb_get_args
(
mrb
,
"f"
,
&
x
);
if
(
x
<
0
.
0
)
{
domain_error
(
mrb
,
"sqrt"
);
}
x
=
sqrt
(
x
);
return
mrb_float_value
(
mrb
,
x
);
...
...
@@ -624,6 +657,8 @@ mrb_mruby_math_gem_init(mrb_state* mrb)
struct
RClass
*
mrb_math
;
mrb_math
=
mrb_define_module
(
mrb
,
"Math"
);
mrb_define_class_under
(
mrb
,
mrb_math
,
"DomainError"
,
mrb
->
eStandardError_class
);
#ifdef M_PI
mrb_define_const
(
mrb
,
mrb_math
,
"PI"
,
mrb_float_value
(
mrb
,
M_PI
));
#else
...
...
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