@stdlib/complex-float32 0.0.6 → 0.1.0

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.
@@ -23,8 +23,9 @@
23
23
  var isNumber = require( '@stdlib/assert-is-number' ).isPrimitive;
24
24
  var defineProperty = require( '@stdlib/utils-define-property' );
25
25
  var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
26
- var Float32Array = require( '@stdlib/array-float32' );
27
- var toString = require( './tostring.js' ); // eslint-disable-line stdlib/no-redeclare
26
+ var float64ToFloat32 = require( '@stdlib/number-float64-base-to-float32' );
27
+ var format = require( '@stdlib/string-format' );
28
+ var toStr = require( './tostring.js' );
28
29
  var toJSON = require( './tojson.js' );
29
30
 
30
31
 
@@ -37,8 +38,8 @@ var toJSON = require( './tojson.js' );
37
38
  * @param {number} real - real component
38
39
  * @param {number} imag - imaginary component
39
40
  * @throws {TypeError} must invoke using the `new` keyword
40
- * @throws {TypeError} real component must be a number primitive
41
- * @throws {TypeError} imaginary component must be a number primitive
41
+ * @throws {TypeError} real component must be a number
42
+ * @throws {TypeError} imaginary component must be a number
42
43
  * @returns {Complex64} 64-bit complex number
43
44
  *
44
45
  * @example
@@ -46,52 +47,28 @@ var toJSON = require( './tojson.js' );
46
47
  * // returns <Complex64>
47
48
  */
48
49
  function Complex64( real, imag ) {
49
- var view;
50
50
  if ( !( this instanceof Complex64 ) ) {
51
51
  throw new TypeError( 'invalid invocation. Constructor must be called with the `new` keyword.' );
52
52
  }
53
53
  if ( !isNumber( real ) ) {
54
- throw new TypeError( 'invalid argument. Real component must be a number primitive. Value: `'+real+'`.' );
54
+ throw new TypeError( format( 'invalid argument. Real component must be a number. Value: `%s`.', real ) );
55
55
  }
56
56
  if ( !isNumber( imag ) ) {
57
- throw new TypeError( 'invalid argument. Imaginary component must be a number primitive. Value: `'+imag+'`.' );
57
+ throw new TypeError( format( 'invalid argument. Imaginary component must be a number. Value: `%s`.', imag ) );
58
58
  }
59
59
  defineProperty( this, 're', {
60
60
  'configurable': false,
61
61
  'enumerable': true,
62
- 'get': getReal
62
+ 'writable': false,
63
+ 'value': float64ToFloat32( real )
63
64
  });
64
-
65
65
  defineProperty( this, 'im', {
66
66
  'configurable': false,
67
67
  'enumerable': true,
68
- 'get': getImag
68
+ 'writable': false,
69
+ 'value': float64ToFloat32( imag )
69
70
  });
70
- view = new Float32Array( 2 );
71
- view[ 0 ] = real;
72
- view[ 1 ] = imag;
73
-
74
71
  return this;
75
-
76
- /**
77
- * Returns a real component.
78
- *
79
- * @private
80
- * @returns {number} real component
81
- */
82
- function getReal() {
83
- return view[ 0 ];
84
- }
85
-
86
- /**
87
- * Returns an imaginary component.
88
- *
89
- * @private
90
- * @returns {number} imaginary component
91
- */
92
- function getImag() {
93
- return view[ 1 ];
94
- }
95
72
  }
96
73
 
97
74
  /**
@@ -154,7 +131,7 @@ setReadOnly( Complex64.prototype, 'byteLength', 8 );
154
131
  * var str = z.toString();
155
132
  * // returns '5 + 3i'
156
133
  */
157
- setReadOnly( Complex64.prototype, 'toString', toString );
134
+ setReadOnly( Complex64.prototype, 'toString', toStr );
158
135
 
159
136
  /**
160
137
  * Serializes a complex number as a JSON object.
@@ -163,7 +140,6 @@ setReadOnly( Complex64.prototype, 'toString', toString );
163
140
  *
164
141
  * - `JSON.stringify()` implicitly calls this method when stringifying a `Complex64` instance.
165
142
  *
166
- *
167
143
  * @name toJSON
168
144
  * @memberof Complex64.prototype
169
145
  * @type {Function}
package/manifest.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "options": {},
3
+ "fields": [
4
+ {
5
+ "field": "src",
6
+ "resolve": true,
7
+ "relative": true
8
+ },
9
+ {
10
+ "field": "include",
11
+ "resolve": true,
12
+ "relative": true
13
+ },
14
+ {
15
+ "field": "libraries",
16
+ "resolve": false,
17
+ "relative": false
18
+ },
19
+ {
20
+ "field": "libpath",
21
+ "resolve": true,
22
+ "relative": false
23
+ }
24
+ ],
25
+ "confs": [
26
+ {
27
+ "src": [
28
+ "./src/main.c"
29
+ ],
30
+ "include": [
31
+ "./include"
32
+ ],
33
+ "libraries": [],
34
+ "libpath": [],
35
+ "dependencies": []
36
+ }
37
+ ]
38
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stdlib/complex-float32",
3
- "version": "0.0.6",
3
+ "version": "0.1.0",
4
4
  "description": "64-bit complex number.",
5
5
  "license": "Apache-2.0",
6
6
  "author": {
@@ -18,7 +18,9 @@
18
18
  "benchmark": "./benchmark",
19
19
  "doc": "./docs",
20
20
  "example": "./examples",
21
+ "include": "./include",
21
22
  "lib": "./lib",
23
+ "src": "./src",
22
24
  "test": "./test"
23
25
  },
24
26
  "types": "./docs/types",
@@ -37,19 +39,22 @@
37
39
  "url": "https://github.com/stdlib-js/stdlib/issues"
38
40
  },
39
41
  "dependencies": {
40
- "@stdlib/array-float32": "^0.0.x",
41
- "@stdlib/assert-is-number": "^0.0.x",
42
- "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x",
43
- "@stdlib/utils-define-property": "^0.0.x"
42
+ "@stdlib/assert-is-number": "^0.1.0",
43
+ "@stdlib/number-float64-base-to-float32": "^0.1.0",
44
+ "@stdlib/string-format": "^0.1.0",
45
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.0",
46
+ "@stdlib/utils-define-property": "^0.1.0",
47
+ "@stdlib/utils-library-manifest": "^0.1.0",
48
+ "@stdlib/error-tools-fmtprodmsg": "^0.1.0"
44
49
  },
45
50
  "devDependencies": {
46
- "@stdlib/assert-has-own-property": "^0.0.x",
47
- "@stdlib/bench": "^0.0.x",
48
- "@stdlib/math-base-assert-is-nan": "^0.0.x",
49
- "@stdlib/random-base-randu": "^0.0.x",
51
+ "@stdlib/assert-has-own-property": "^0.1.0",
52
+ "@stdlib/bench": "^0.1.0",
53
+ "@stdlib/math-base-assert-is-nan": "^0.1.0",
54
+ "@stdlib/random-base-randu": "^0.0.8",
50
55
  "tape": "git+https://github.com/kgryte/tape.git#fix/globby",
51
56
  "istanbul": "^0.4.1",
52
- "tap-spec": "5.x.x"
57
+ "tap-min": "git+https://github.com/Planeshifter/tap-min.git"
53
58
  },
54
59
  "engines": {
55
60
  "node": ">=0.10.0",
@@ -82,7 +87,7 @@
82
87
  "ieee754"
83
88
  ],
84
89
  "funding": {
85
- "type": "patreon",
86
- "url": "https://www.patreon.com/athan"
90
+ "type": "opencollective",
91
+ "url": "https://opencollective.com/stdlib"
87
92
  }
88
93
  }
package/src/main.c ADDED
@@ -0,0 +1,151 @@
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/complex/float32.h"
20
+ #include <stdint.h>
21
+
22
+ /**
23
+ * Returns a single-precision complex floating-point number.
24
+ *
25
+ * @param real real component
26
+ * @param imag imaginary component
27
+ * @return single-precision complex floating-point number
28
+ *
29
+ * @example
30
+ * stdlib_complex64_t z = stdlib_complex64( 5.0f, 2.0f );
31
+ */
32
+ stdlib_complex64_t stdlib_complex64( const float real, const float imag ) {
33
+ stdlib_complex64_parts_t z;
34
+ z.parts[ 0 ] = real;
35
+ z.parts[ 1 ] = imag; // cppcheck-suppress unreadVariable
36
+ return z.value;
37
+ }
38
+
39
+ /**
40
+ * Converts a single-precision floating-point number to a single-precision complex floating-point number.
41
+ *
42
+ * @param real real component
43
+ * @return single-precision complex floating-point number
44
+ *
45
+ * @example
46
+ * stdlib_complex64_t z = stdlib_complex64_from_float32( 5.0f );
47
+ */
48
+ stdlib_complex64_t stdlib_complex64_from_float32( const float real ) {
49
+ stdlib_complex64_parts_t z;
50
+ z.parts[ 0 ] = real;
51
+ z.parts[ 1 ] = 0.0f; // cppcheck-suppress unreadVariable
52
+ return z.value;
53
+ }
54
+
55
+ /**
56
+ * Converts a double-precision floating-point number to a single-precision complex floating-point number.
57
+ *
58
+ * @param real real component
59
+ * @return single-precision complex floating-point number
60
+ *
61
+ * @example
62
+ * stdlib_complex64_t z = stdlib_complex64_from_float64( 5.0 );
63
+ */
64
+ stdlib_complex64_t stdlib_complex64_from_float64( const double real ) {
65
+ stdlib_complex64_parts_t z;
66
+ z.parts[ 0 ] = (float)real;
67
+ z.parts[ 1 ] = 0.0f; // cppcheck-suppress unreadVariable
68
+ return z.value;
69
+ }
70
+
71
+ /**
72
+ * Converts (copies) a single-precision complex floating-point number to a single-precision complex floating-point number.
73
+ *
74
+ * @param z single-precision complex floating-point number
75
+ * @return single-precision complex floating-point number
76
+ *
77
+ * @example
78
+ * stdlib_complex64_t z1 = stdlib_complex64( 5.0f, 3.0f );
79
+ * stdlib_complex64_t z2 = stdlib_complex64_from_complex64( z1 );
80
+ */
81
+ stdlib_complex64_t stdlib_complex64_from_complex64( const stdlib_complex64_t z ) {
82
+ stdlib_complex64_parts_t v1 = { z };
83
+ stdlib_complex64_parts_t v2;
84
+ v2.parts[ 0 ] = v1.parts[ 0 ];
85
+ v2.parts[ 1 ] = v1.parts[ 1 ]; // cppcheck-suppress unreadVariable
86
+ return v2.value;
87
+ }
88
+
89
+ /**
90
+ * Converts a signed 8-bit integer to a single-precision complex floating-point number.
91
+ *
92
+ * @param real real component
93
+ * @return single-precision complex floating-point number
94
+ *
95
+ * @example
96
+ * stdlib_complex64_t z = stdlib_complex64_from_int8( 5 );
97
+ */
98
+ stdlib_complex64_t stdlib_complex64_from_int8( const int8_t real ) {
99
+ stdlib_complex64_parts_t z;
100
+ z.parts[ 0 ] = (float)real;
101
+ z.parts[ 1 ] = 0.0f; // cppcheck-suppress unreadVariable
102
+ return z.value;
103
+ }
104
+
105
+ /**
106
+ * Converts an unsigned 8-bit integer to a single-precision complex floating-point number.
107
+ *
108
+ * @param real real component
109
+ * @return single-precision complex floating-point number
110
+ *
111
+ * @example
112
+ * stdlib_complex64_t z = stdlib_complex64_from_uint8( 5 );
113
+ */
114
+ stdlib_complex64_t stdlib_complex64_from_uint8( const uint8_t real ) {
115
+ stdlib_complex64_parts_t z;
116
+ z.parts[ 0 ] = (float)real;
117
+ z.parts[ 1 ] = 0.0f; // cppcheck-suppress unreadVariable
118
+ return z.value;
119
+ }
120
+
121
+ /**
122
+ * Converts a signed 16-bit integer to a single-precision complex floating-point number.
123
+ *
124
+ * @param real real component
125
+ * @return single-precision complex floating-point number
126
+ *
127
+ * @example
128
+ * stdlib_complex64_t z = stdlib_complex64_from_int16( 5 );
129
+ */
130
+ stdlib_complex64_t stdlib_complex64_from_int16( const int16_t real ) {
131
+ stdlib_complex64_parts_t z;
132
+ z.parts[ 0 ] = (float)real;
133
+ z.parts[ 1 ] = 0.0f; // cppcheck-suppress unreadVariable
134
+ return z.value;
135
+ }
136
+
137
+ /**
138
+ * Converts an unsigned 16-bit integer to a single-precision complex floating-point number.
139
+ *
140
+ * @param real real component
141
+ * @return single-precision complex floating-point number
142
+ *
143
+ * @example
144
+ * stdlib_complex64_t z = stdlib_complex64_from_uint16( 5 );
145
+ */
146
+ stdlib_complex64_t stdlib_complex64_from_uint16( const uint16_t real ) {
147
+ stdlib_complex64_parts_t z;
148
+ z.parts[ 0 ] = (float)real;
149
+ z.parts[ 1 ] = 0.0f; // cppcheck-suppress unreadVariable
150
+ return z.value;
151
+ }
package/docs/repl.txt DELETED
@@ -1,44 +0,0 @@
1
-
2
- {{alias}}( real, imag )
3
- 64-bit complex number constructor.
4
-
5
- Both the real and imaginary components are stored as single-precision
6
- floating-point numbers.
7
-
8
- Parameters
9
- ----------
10
- real: number
11
- Real component.
12
-
13
- imag: number
14
- Imaginary component.
15
-
16
- Returns
17
- -------
18
- z: Complex64
19
- 64-bit complex number.
20
-
21
- z.re: number
22
- Read-only property returning the real component.
23
-
24
- z.im: number
25
- Read-only property returning the imaginary component.
26
-
27
- z.BYTES_PER_ELEMENT
28
- Size (in bytes) of each component. Value: 4.
29
-
30
- z.byteLength
31
- Length (in bytes) of a complex number. Value: 8.
32
-
33
- Examples
34
- --------
35
- > var z = new {{alias}}( 5.0, 3.0 )
36
- <Complex64>
37
- > z.re
38
- 5.0
39
- > z.im
40
- 3.0
41
-
42
- See Also
43
- --------
44
-
@@ -1,69 +0,0 @@
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
- import Complex64 = require( './index' );
20
-
21
-
22
- // TESTS //
23
-
24
- // The function returns a 64-bit complex number with the expected properties...
25
- {
26
- const x = new Complex64( 5.0, 3.0 ); // $ExpectType Complex64
27
-
28
- // tslint:disable-next-line:no-unused-expression
29
- x.im; // $ExpectType number
30
-
31
- // tslint:disable-next-line:no-unused-expression
32
- x.re; // $ExpectType number
33
-
34
- // tslint:disable-next-line:no-unused-expression
35
- x.BYTES_PER_ELEMENT; // $ExpectType number
36
-
37
- // tslint:disable-next-line:no-unused-expression
38
- x.byteLength; // $ExpectType number
39
- }
40
-
41
- // 64-bit complex number comes with a `toString` method to serialize a complex number as a string...
42
- {
43
- const x = new Complex64( 5.0, 3.0 ); // $ExpectType Complex64
44
-
45
- // tslint:disable-next-line:no-unused-expression
46
- x.toString(); // $ExpectType string
47
- }
48
-
49
- // 64-bit complex number comes with a `toJSON` method to serialize a complex number as a JSON object....
50
- {
51
- const x = new Complex64( 5.0, 3.0 ); // $ExpectType Complex64
52
-
53
- // tslint:disable-next-line:no-unused-expression
54
- x.toJSON(); // $ExpectType any
55
- }
56
-
57
- // The compiler throws an error if the constructor is invoked without the `new` keyword...
58
- {
59
- Complex64( 5.0, 3.0 ); // $ExpectError
60
- }
61
-
62
- // The compiler throws an error if the constructor is provided an unsupported number of arguments...
63
- {
64
- // tslint:disable-next-line:no-unused-expression
65
- new Complex64( 5.0 ); // $ExpectError
66
-
67
- // tslint:disable-next-line:no-unused-expression
68
- new Complex64( 5.0, 3.0, 1.0 ); // $ExpectError
69
- }