@stdlib/math-base-special-copysignf 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/native.js ADDED
@@ -0,0 +1,63 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2021 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 addon = require( './../src/addon.node' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Returns a single-precision floating-point number with the magnitude of `x` and the sign of `y`.
30
+ *
31
+ * @private
32
+ * @param {number} x - number from which to derive a magnitude
33
+ * @param {number} y - number from which to derive a sign
34
+ * @returns {number} a single-precision floating-point number
35
+ *
36
+ * @example
37
+ * var z = copysignf( -3.0, 10.0 );
38
+ * // returns 3.0
39
+ *
40
+ * @example
41
+ * var z = copysignf( 3.0, -1.0 );
42
+ * // returns -3.0
43
+ *
44
+ * @example
45
+ * var z = copysignf( 1.0, -0.0 );
46
+ * // returns -1.0
47
+ *
48
+ * @example
49
+ * var z = copysignf( -3.0, -0.0 );
50
+ * // returns -3.0
51
+ *
52
+ * @example
53
+ * var z = copysignf( -0.0, 1.0 );
54
+ * // returns 0.0
55
+ */
56
+ function copysignf( x, y ) {
57
+ return addon( x, y );
58
+ }
59
+
60
+
61
+ // EXPORTS //
62
+
63
+ module.exports = copysignf;
package/manifest.json ADDED
@@ -0,0 +1,81 @@
1
+ {
2
+ "options": {
3
+ "task": "build"
4
+ },
5
+ "fields": [
6
+ {
7
+ "field": "src",
8
+ "resolve": true,
9
+ "relative": true
10
+ },
11
+ {
12
+ "field": "include",
13
+ "resolve": true,
14
+ "relative": true
15
+ },
16
+ {
17
+ "field": "libraries",
18
+ "resolve": false,
19
+ "relative": false
20
+ },
21
+ {
22
+ "field": "libpath",
23
+ "resolve": true,
24
+ "relative": false
25
+ }
26
+ ],
27
+ "confs": [
28
+ {
29
+ "task": "build",
30
+ "src": [
31
+ "./src/main.c"
32
+ ],
33
+ "include": [
34
+ "./include"
35
+ ],
36
+ "libraries": [
37
+ "-lm"
38
+ ],
39
+ "libpath": [],
40
+ "dependencies": [
41
+ "@stdlib/math-base-napi-binary",
42
+ "@stdlib/number-float32-base-to-word",
43
+ "@stdlib/number-float32-base-from-word"
44
+ ]
45
+ },
46
+ {
47
+ "task": "benchmark",
48
+ "src": [
49
+ "./src/main.c"
50
+ ],
51
+ "include": [
52
+ "./include"
53
+ ],
54
+ "libraries": [
55
+ "-lm"
56
+ ],
57
+ "libpath": [],
58
+ "dependencies": [
59
+ "@stdlib/number-float32-base-to-word",
60
+ "@stdlib/number-float32-base-from-word"
61
+ ]
62
+ },
63
+ {
64
+ "task": "examples",
65
+ "src": [
66
+ "./src/main.c"
67
+ ],
68
+ "include": [
69
+ "./include"
70
+ ],
71
+ "libraries": [
72
+ "-lm"
73
+ ],
74
+ "libpath": [],
75
+ "dependencies": [
76
+ "@stdlib/number-float32-base-to-word",
77
+ "@stdlib/number-float32-base-from-word"
78
+ ]
79
+ }
80
+ ]
81
+ }
package/package.json ADDED
@@ -0,0 +1,99 @@
1
+ {
2
+ "name": "@stdlib/math-base-special-copysignf",
3
+ "version": "0.0.1",
4
+ "description": "Return a single-precision floating-point number with the magnitude of x and the sign of y.",
5
+ "license": "Apache-2.0",
6
+ "author": {
7
+ "name": "The Stdlib Authors",
8
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9
+ },
10
+ "contributors": [
11
+ {
12
+ "name": "The Stdlib Authors",
13
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14
+ }
15
+ ],
16
+ "main": "./lib",
17
+ "gypfile": false,
18
+ "directories": {
19
+ "benchmark": "./benchmark",
20
+ "doc": "./docs",
21
+ "example": "./examples",
22
+ "include": "./include",
23
+ "lib": "./lib",
24
+ "src": "./src",
25
+ "test": "./test"
26
+ },
27
+ "types": "./docs/types",
28
+ "scripts": {
29
+ "test": "make test",
30
+ "test-cov": "make test-cov",
31
+ "examples": "make examples",
32
+ "benchmark": "make benchmark"
33
+ },
34
+ "homepage": "https://stdlib.io",
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git://github.com/stdlib-js/math-base-special-copysignf.git"
38
+ },
39
+ "bugs": {
40
+ "url": "https://github.com/stdlib-js/stdlib/issues"
41
+ },
42
+ "dependencies": {
43
+ "@stdlib/math-base-napi-binary": "^0.0.x",
44
+ "@stdlib/number-float32-base-from-word": "^0.0.x",
45
+ "@stdlib/number-float32-base-to-word": "^0.0.x",
46
+ "@stdlib/number-float64-base-to-float32": "^0.0.x",
47
+ "@stdlib/utils-library-manifest": "^0.0.x"
48
+ },
49
+ "devDependencies": {
50
+ "@stdlib/bench": "^0.0.x",
51
+ "@stdlib/constants-float32-ninf": "^0.0.x",
52
+ "@stdlib/constants-float32-pinf": "^0.0.x",
53
+ "@stdlib/math-base-assert-is-nanf": "^0.0.x",
54
+ "@stdlib/math-base-assert-is-negative-zerof": "^0.0.x",
55
+ "@stdlib/math-base-assert-is-positive-zerof": "^0.0.x",
56
+ "@stdlib/random-base-randu": "^0.0.x",
57
+ "@stdlib/utils-try-require": "^0.0.x",
58
+ "tape": "git+https://github.com/kgryte/tape.git#fix/globby",
59
+ "istanbul": "^0.4.1",
60
+ "tap-spec": "5.x.x"
61
+ },
62
+ "engines": {
63
+ "node": ">=0.10.0",
64
+ "npm": ">2.7.0"
65
+ },
66
+ "os": [
67
+ "aix",
68
+ "darwin",
69
+ "freebsd",
70
+ "linux",
71
+ "macos",
72
+ "openbsd",
73
+ "sunos",
74
+ "win32",
75
+ "windows"
76
+ ],
77
+ "keywords": [
78
+ "stdlib",
79
+ "stdmath",
80
+ "math",
81
+ "mathematics",
82
+ "float32",
83
+ "float",
84
+ "floating-point",
85
+ "ieee754",
86
+ "copysignf",
87
+ "copy",
88
+ "sign",
89
+ "bits",
90
+ "number",
91
+ "flt",
92
+ "special",
93
+ "function"
94
+ ],
95
+ "funding": {
96
+ "type": "patreon",
97
+ "url": "https://www.patreon.com/athan"
98
+ }
99
+ }
package/src/addon.c ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2021 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
+ #include "stdlib/math/base/special/copysignf.h"
20
+ #include "stdlib/math/base/napi/binary.h"
21
+
22
+ // cppcheck-suppress shadowFunction
23
+ STDLIB_MATH_BASE_NAPI_MODULE_FF_F( stdlib_base_copysignf )
package/src/main.c ADDED
@@ -0,0 +1,68 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2021 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
+ #include "stdlib/math/base/special/copysignf.h"
20
+ #include "stdlib/number/float32/base/to_word.h"
21
+ #include "stdlib/number/float32/base/from_word.h"
22
+ #include <stdint.h>
23
+
24
+ // 10000000000000000000000000000000 => 2147483648 => 0x80000000
25
+ static const uint32_t SIGN_MASK = 0x80000000;
26
+
27
+ // 01111111111111111111111111111111 => 2147483647 => 0x7fffffff
28
+ static const int32_t MAGNITUDE_MASK = 0x7fffffff;
29
+
30
+ /**
31
+ * Returns a single-precision floating-point number with the magnitude of `x` and the sign of `y`.
32
+ *
33
+ * @param x number from which to derive a magnitude
34
+ * @param y number from which to derive a sign
35
+ * @return result
36
+ *
37
+ * @example
38
+ * float v = stdlib_base_copysignf( -3.0f, 10.0f );
39
+ * // returns 3.0f
40
+ *
41
+ * @example
42
+ * float v = stdlib_base_copysignf( 3.0f, -1.0f );
43
+ * // returns -3.0f
44
+ */
45
+ float stdlib_base_copysignf( const float x, const float y ) {
46
+ uint32_t wx;
47
+ uint32_t wy;
48
+ float z;
49
+
50
+ // Convert `x` to an unsigned integer:
51
+ stdlib_base_float32_to_word( x, &wx );
52
+
53
+ // Turn off the sign bit of `x`:
54
+ wx &= MAGNITUDE_MASK;
55
+
56
+ // Convert `y` to an unsigned integer:
57
+ stdlib_base_float32_to_word( y, &wy );
58
+
59
+ // Leave only the sign bit of `y` turned on:
60
+ wy &= SIGN_MASK;
61
+
62
+ // Copy the sign bit of `y` to `x`:
63
+ wx |= wy;
64
+
65
+ // Return a new value having the same magnitude as `x`, but with the sign of `y`:
66
+ stdlib_base_float32_from_word( wx, &z );
67
+ return z;
68
+ }