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
405e8fbc
Commit
405e8fbc
authored
May 02, 2014
by
cremno
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mruby-math: define and use `Math::DomainError`
Domain errors are detected as per ISO C99 and may differ from CRuby.
parent
72fe54c4
Changes
1
Hide 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 @
405e8fbc
...
@@ -9,8 +9,14 @@
...
@@ -9,8 +9,14 @@
#include <math.h>
#include <math.h>
#define domain_error(msg) \
static
void
mrb_raise(mrb, E_RANGE_ERROR, "Numerical argument is out of domain - " #msg)
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 */
/* math functions not provided by Microsoft Visual C++ 2012 or older */
#if defined _MSC_VER && _MSC_VER < 1800
#if defined _MSC_VER && _MSC_VER < 1800
...
@@ -172,6 +178,9 @@ math_asin(mrb_state *mrb, mrb_value obj)
...
@@ -172,6 +178,9 @@ math_asin(mrb_state *mrb, mrb_value obj)
mrb_float
x
;
mrb_float
x
;
mrb_get_args
(
mrb
,
"f"
,
&
x
);
mrb_get_args
(
mrb
,
"f"
,
&
x
);
if
(
x
<
-
1
.
0
||
x
>
1
.
0
)
{
domain_error
(
mrb
,
"asin"
);
}
x
=
asin
(
x
);
x
=
asin
(
x
);
return
mrb_float_value
(
mrb
,
x
);
return
mrb_float_value
(
mrb
,
x
);
...
@@ -189,6 +198,9 @@ math_acos(mrb_state *mrb, mrb_value obj)
...
@@ -189,6 +198,9 @@ math_acos(mrb_state *mrb, mrb_value obj)
mrb_float
x
;
mrb_float
x
;
mrb_get_args
(
mrb
,
"f"
,
&
x
);
mrb_get_args
(
mrb
,
"f"
,
&
x
);
if
(
x
<
-
1
.
0
||
x
>
1
.
0
)
{
domain_error
(
mrb
,
"acos"
);
}
x
=
acos
(
x
);
x
=
acos
(
x
);
return
mrb_float_value
(
mrb
,
x
);
return
mrb_float_value
(
mrb
,
x
);
...
@@ -334,6 +346,9 @@ math_acosh(mrb_state *mrb, mrb_value obj)
...
@@ -334,6 +346,9 @@ math_acosh(mrb_state *mrb, mrb_value obj)
mrb_float
x
;
mrb_float
x
;
mrb_get_args
(
mrb
,
"f"
,
&
x
);
mrb_get_args
(
mrb
,
"f"
,
&
x
);
if
(
x
<
1
.
0
)
{
domain_error
(
mrb
,
"acosh"
);
}
x
=
acosh
(
x
);
x
=
acosh
(
x
);
return
mrb_float_value
(
mrb
,
x
);
return
mrb_float_value
(
mrb
,
x
);
...
@@ -351,6 +366,9 @@ math_atanh(mrb_state *mrb, mrb_value obj)
...
@@ -351,6 +366,9 @@ math_atanh(mrb_state *mrb, mrb_value obj)
mrb_float
x
;
mrb_float
x
;
mrb_get_args
(
mrb
,
"f"
,
&
x
);
mrb_get_args
(
mrb
,
"f"
,
&
x
);
if
(
x
<
-
1
.
0
||
x
>
1
.
0
)
{
domain_error
(
mrb
,
"atanh"
);
}
x
=
atanh
(
x
);
x
=
atanh
(
x
);
return
mrb_float_value
(
mrb
,
x
);
return
mrb_float_value
(
mrb
,
x
);
...
@@ -404,8 +422,14 @@ math_log(mrb_state *mrb, mrb_value obj)
...
@@ -404,8 +422,14 @@ math_log(mrb_state *mrb, mrb_value obj)
int
argc
;
int
argc
;
argc
=
mrb_get_args
(
mrb
,
"f|f"
,
&
x
,
&
base
);
argc
=
mrb_get_args
(
mrb
,
"f|f"
,
&
x
,
&
base
);
if
(
x
<
0
.
0
)
{
domain_error
(
mrb
,
"log"
);
}
x
=
log
(
x
);
x
=
log
(
x
);
if
(
argc
==
2
)
{
if
(
argc
==
2
)
{
if
(
base
<
0
.
0
)
{
domain_error
(
mrb
,
"log"
);
}
x
/=
log
(
base
);
x
/=
log
(
base
);
}
}
return
mrb_float_value
(
mrb
,
x
);
return
mrb_float_value
(
mrb
,
x
);
...
@@ -429,6 +453,9 @@ math_log2(mrb_state *mrb, mrb_value obj)
...
@@ -429,6 +453,9 @@ math_log2(mrb_state *mrb, mrb_value obj)
mrb_float
x
;
mrb_float
x
;
mrb_get_args
(
mrb
,
"f"
,
&
x
);
mrb_get_args
(
mrb
,
"f"
,
&
x
);
if
(
x
<
0
.
0
)
{
domain_error
(
mrb
,
"log2"
);
}
x
=
log2
(
x
);
x
=
log2
(
x
);
return
mrb_float_value
(
mrb
,
x
);
return
mrb_float_value
(
mrb
,
x
);
...
@@ -451,6 +478,9 @@ math_log10(mrb_state *mrb, mrb_value obj)
...
@@ -451,6 +478,9 @@ math_log10(mrb_state *mrb, mrb_value obj)
mrb_float
x
;
mrb_float
x
;
mrb_get_args
(
mrb
,
"f"
,
&
x
);
mrb_get_args
(
mrb
,
"f"
,
&
x
);
if
(
x
<
0
.
0
)
{
domain_error
(
mrb
,
"log10"
);
}
x
=
log10
(
x
);
x
=
log10
(
x
);
return
mrb_float_value
(
mrb
,
x
);
return
mrb_float_value
(
mrb
,
x
);
...
@@ -469,6 +499,9 @@ math_sqrt(mrb_state *mrb, mrb_value obj)
...
@@ -469,6 +499,9 @@ math_sqrt(mrb_state *mrb, mrb_value obj)
mrb_float
x
;
mrb_float
x
;
mrb_get_args
(
mrb
,
"f"
,
&
x
);
mrb_get_args
(
mrb
,
"f"
,
&
x
);
if
(
x
<
0
.
0
)
{
domain_error
(
mrb
,
"sqrt"
);
}
x
=
sqrt
(
x
);
x
=
sqrt
(
x
);
return
mrb_float_value
(
mrb
,
x
);
return
mrb_float_value
(
mrb
,
x
);
...
@@ -624,6 +657,8 @@ mrb_mruby_math_gem_init(mrb_state* mrb)
...
@@ -624,6 +657,8 @@ mrb_mruby_math_gem_init(mrb_state* mrb)
struct
RClass
*
mrb_math
;
struct
RClass
*
mrb_math
;
mrb_math
=
mrb_define_module
(
mrb
,
"Math"
);
mrb_math
=
mrb_define_module
(
mrb
,
"Math"
);
mrb_define_class_under
(
mrb
,
mrb_math
,
"DomainError"
,
mrb
->
eStandardError_class
);
#ifdef M_PI
#ifdef M_PI
mrb_define_const
(
mrb
,
mrb_math
,
"PI"
,
mrb_float_value
(
mrb
,
M_PI
));
mrb_define_const
(
mrb
,
mrb_math
,
"PI"
,
mrb_float_value
(
mrb
,
M_PI
));
#else
#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