Skip to content

Commit fa493b4

Browse files
Pranavchikukgryte
andauthored
Add C implementation for @stdlib/math/base/assert/is-integer (#779)
* add c-api documentation in readme * add include.gypi and binding.gyp * add is_integer.h * Add Makefile, addon.c and main.c in src/ * Add manifest.json * add native.js in lib/ * add c examples * add native tests, benchmarks for c and js * Apply suggestions from code review Co-authored-by: Athan <kgryte@gmail.com>
1 parent d52cda0 commit fa493b4

File tree

15 files changed

+1319
-1
lines changed

15 files changed

+1319
-1
lines changed

lib/node_modules/@stdlib/math/base/assert/is-integer/README.md

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2018 The Stdlib Authors.
5+
Copyright (c) 2022 The Stdlib Authors.
66
77
Licensed under the Apache License, Version 2.0 (the "License");
88
you may not use this file except in compliance with the License.
@@ -92,6 +92,97 @@ bool = isInteger( NaN );
9292

9393
<!-- /.examples -->
9494

95+
<!-- C interface documentation. -->
96+
97+
* * *
98+
99+
<section class="c">
100+
101+
## C APIs
102+
103+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
104+
105+
<section class="intro">
106+
107+
</section>
108+
109+
<!-- /.intro -->
110+
111+
<!-- C usage documentation. -->
112+
113+
<section class="usage">
114+
115+
### Usage
116+
117+
```c
118+
#include "stdlib/math/base/assert/is_integer.h"
119+
```
120+
121+
#### stdlib_base_is_integer( x )
122+
123+
Tests if a finite [double-precision floating-point number][ieee754] is an `integer`.
124+
125+
```c
126+
bool out = stdlib_base_is_integer( 1.0 );
127+
// returns true
128+
129+
out = stdlib_base_is_integer( 3.14 );
130+
// returns false
131+
```
132+
133+
The function accepts the following arguments:
134+
135+
- **x**: `[in] double` input value.
136+
137+
```c
138+
bool stdlib_base_is_integer( const double x );
139+
```
140+
141+
</section>
142+
143+
<!-- /.usage -->
144+
145+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
146+
147+
<section class="notes">
148+
149+
</section>
150+
151+
<!-- /.notes -->
152+
153+
<!-- C API usage examples. -->
154+
155+
<section class="examples">
156+
157+
### Examples
158+
159+
```c
160+
#include "stdlib/math/base/assert/is_integer.h"
161+
#include <stdio.h>
162+
#include <stdlib.h>
163+
#include <stdbool.h>
164+
165+
int main() {
166+
double x;
167+
bool v;
168+
int i;
169+
170+
for ( i = 0; i < 100; i++ ) {
171+
x = ( ( (double)rand() / (double)RAND_MAX ) * 100.0 ) - 50.0;
172+
v = stdlib_base_is_integer( x );
173+
printf( "x = %lf, is_integer(x) = %s\n", x, ( v ) ? "true" : "false" );
174+
}
175+
}
176+
```
177+
178+
</section>
179+
180+
<!-- /.examples -->
181+
182+
</section>
183+
184+
<!-- /.c -->
185+
95186
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
96187
97188
<section class="related">
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
26+
var tryRequire = require( '@stdlib/utils/try-require' );
27+
var pkg = require( './../package.json' ).name;
28+
29+
30+
// VARIABLES //
31+
32+
var isInteger = tryRequire( resolve( __dirname, './../lib/native.js' ) );
33+
var opts = {
34+
'skip': ( isInteger instanceof Error )
35+
};
36+
37+
38+
// MAIN //
39+
40+
bench( pkg+'::native,true', opts, function benchmark( b ) {
41+
var values;
42+
var bool;
43+
var i;
44+
45+
values = [
46+
0,
47+
1,
48+
2,
49+
3,
50+
4,
51+
5,
52+
6,
53+
7,
54+
8,
55+
9,
56+
1.0e308,
57+
10,
58+
-0,
59+
-1,
60+
-2,
61+
-3,
62+
-4,
63+
-5,
64+
-6,
65+
-7,
66+
-8,
67+
-9,
68+
-10,
69+
1.0e308
70+
];
71+
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
bool = isInteger( values[ i%values.length ] );
75+
if ( typeof bool !== 'boolean' ) {
76+
b.fail( 'should return a boolean' );
77+
}
78+
}
79+
b.toc();
80+
if ( !isBoolean( bool ) && bool !== true ) {
81+
b.fail( 'should return true' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
});
86+
87+
bench( pkg+'::native,false', opts, function benchmark( b ) {
88+
var values;
89+
var bool;
90+
var i;
91+
92+
values = [
93+
0.1,
94+
1.1,
95+
2.1,
96+
3.1,
97+
4.1,
98+
5.1,
99+
6.1,
100+
7.1,
101+
8.1,
102+
9.1,
103+
10.1,
104+
-0.1,
105+
-1.1,
106+
-2.1,
107+
-3.1,
108+
-4.1,
109+
-5.1,
110+
-6.1,
111+
-7.1,
112+
-8.1,
113+
-9.1,
114+
-10.1
115+
];
116+
117+
b.tic();
118+
for ( i = 0; i < b.iterations; i++ ) {
119+
bool = isInteger( values[ i%values.length ] );
120+
if ( typeof bool !== 'boolean' ) {
121+
b.fail( 'should return a boolean' );
122+
}
123+
}
124+
b.toc();
125+
if ( !isBoolean( bool ) && bool !== false ) {
126+
b.fail( 'should return false' );
127+
}
128+
b.pass( 'benchmark finished' );
129+
b.end();
130+
});
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#/
2+
# @license Apache-2.0
3+
#
4+
# Copyright (c) 2022 The Stdlib Authors.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#/
18+
19+
# VARIABLES #
20+
21+
ifndef VERBOSE
22+
QUIET := @
23+
else
24+
QUIET :=
25+
endif
26+
27+
# Determine the OS ([1][1], [2][2]).
28+
#
29+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
30+
# [2]: http://stackoverflow.com/a/27776822/2225624
31+
OS ?= $(shell uname)
32+
ifneq (, $(findstring MINGW,$(OS)))
33+
OS := WINNT
34+
else
35+
ifneq (, $(findstring MSYS,$(OS)))
36+
OS := WINNT
37+
else
38+
ifneq (, $(findstring CYGWIN,$(OS)))
39+
OS := WINNT
40+
else
41+
ifneq (, $(findstring Windows_NT,$(OS)))
42+
OS := WINNT
43+
endif
44+
endif
45+
endif
46+
endif
47+
48+
# Define the program used for compiling C source files:
49+
ifdef C_COMPILER
50+
CC := $(C_COMPILER)
51+
else
52+
CC := gcc
53+
endif
54+
55+
# Define the command-line options when compiling C files:
56+
CFLAGS ?= \
57+
-std=c99 \
58+
-O3 \
59+
-Wall \
60+
-pedantic
61+
62+
# Determine whether to generate position independent code ([1][1], [2][2]).
63+
#
64+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
65+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
66+
ifeq ($(OS), WINNT)
67+
fPIC ?=
68+
else
69+
fPIC ?= -fPIC
70+
endif
71+
72+
# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`):
73+
INCLUDE ?=
74+
75+
# List of source files:
76+
SOURCE_FILES ?=
77+
78+
# List of libraries (e.g., `-lopenblas -lpthread`):
79+
LIBRARIES ?=
80+
81+
# List of library paths (e.g., `-L /foo/bar -L /beep/boop`):
82+
LIBPATH ?=
83+
84+
# List of C targets:
85+
c_targets := benchmark.out
86+
87+
88+
# RULES #
89+
90+
#/
91+
# Compiles source files.
92+
#
93+
# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`)
94+
# @param {string} [CFLAGS] - C compiler options
95+
# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
96+
# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`)
97+
# @param {string} [SOURCE_FILES] - list of source files
98+
# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`)
99+
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`)
100+
#
101+
# @example
102+
# make
103+
#
104+
# @example
105+
# make all
106+
#/
107+
all: $(c_targets)
108+
109+
.PHONY: all
110+
111+
#/
112+
# Compiles C source files.
113+
#
114+
# @private
115+
# @param {string} CC - C compiler (e.g., `gcc`)
116+
# @param {string} CFLAGS - C compiler options
117+
# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
118+
# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`)
119+
# @param {string} SOURCE_FILES - list of source files
120+
# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`)
121+
# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`)
122+
#/
123+
$(c_targets): %.out: %.c
124+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES)
125+
126+
#/
127+
# Runs compiled benchmarks.
128+
#
129+
# @example
130+
# make run
131+
#/
132+
run: $(c_targets)
133+
$(QUIET) ./$<
134+
135+
.PHONY: run
136+
137+
#/
138+
# Removes generated files.
139+
#
140+
# @example
141+
# make clean
142+
#/
143+
clean:
144+
$(QUIET) -rm -f *.o *.out
145+
146+
.PHONY: clean

0 commit comments

Comments
 (0)