Skip to content

Commit 48cbe95

Browse files
committed
Auto-generated commit
1 parent 7a03310 commit 48cbe95

File tree

10 files changed

+69
-10
lines changed

10 files changed

+69
-10
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
<section class="release" id="unreleased">
66

7-
## Unreleased (2025-03-22)
7+
## Unreleased (2025-03-23)
88

99
<section class="features">
1010

@@ -32,6 +32,7 @@
3232

3333
<details>
3434

35+
- [`5cab853`](https://github.com/stdlib-js/stdlib/commit/5cab853c4f710e60d7dc1639e0cb5e9f00ad2134) - **refactor:** update to enforce mostly safe casts _(by Athan Reines)_
3536
- [`40fc3a2`](https://github.com/stdlib-js/stdlib/commit/40fc3a218445c34a667237cadb74ae9417cc0392) - **chore:** add TODO _(by Athan Reines)_
3637
- [`1aca9d3`](https://github.com/stdlib-js/stdlib/commit/1aca9d37e47e33c03b94bb5b128647c7387172e2) - **fix:** update type defn _(by Athan Reines)_
3738
- [`266a064`](https://github.com/stdlib-js/stdlib/commit/266a064a8cc55b100a00d2ad98c84820d8f17653) - **style:** fix spacing _(by Athan Reines)_

CONTRIBUTORS

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Contributors listed in alphabetical order.
44

55
Aadish Jain <[email protected]>
6+
Aarya Balwadkar <[email protected]>
67
Aayush Khanna <[email protected]>
78
Abdelrahman Samir <[email protected]>
89
Abdul Kaium <[email protected]>
@@ -104,6 +105,7 @@ Ognjen Jevremović <[email protected]>
104105
Oneday12323 <[email protected]>
105106
Ori Miles <[email protected]>
106107
Philipp Burckhardt <[email protected]>
108+
Prajjwal Bajpai <[email protected]>
107109
Prajwal Kulkarni <[email protected]>
108110
Pranav Goswami <[email protected]>
109111
Pranjal Jha <[email protected]>

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ A provided ndarray should be an object with the following properties:
129129
## Notes
130130

131131
- If `value` is a number and `x` has a complex [data type][@stdlib/ndarray/dtypes], the function fills an input ndarray with a complex number whose real component equals the provided scalar `value` and whose imaginary component is zero.
132+
- A `value` must be able to safely cast to the input ndarray [data type][@stdlib/ndarray/dtypes]. Scalar values having floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the same kind (e.g., a scalar double-precision floating-point number can be used to fill a 'float32' input ndarray).
132133
- The function **mutates** the input ndarray.
133134

134135
</section>

dist/index.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/repl.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
Input ndarray-like object.
1515

1616
value: any
17-
Scalar value.
17+
Scalar value. Must be able to safely cast to the input ndarray data
18+
type. Scalar values having floating-point data types (both real and
19+
complex) are allowed to downcast to a lower precision data type of the
20+
same kind (e.g., a scalar double-precision floating-point number can be
21+
used to fill a 'float32' input ndarray).
1822

1923
Examples
2024
--------

docs/types/index.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { ComplexLike } from '@stdlib/types/complex';
2929
* ## Notes
3030
*
3131
* - If `value` is a number, the function fills an input ndarray with a complex number whose real component equals the provided scalar value and whose imaginary component is zero.
32+
* - A `value` must be able to safely cast to the input ndarray data type. Scalar values having floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the same kind (e.g., a scalar double-precision floating-point number can be used to fill a 'float32' input ndarray).
3233
*
3334
* @param x - input ndarray
3435
* @param value - scalar value
@@ -68,6 +69,10 @@ declare function fill( x: complexndarray, value: number | ComplexLike ): void;
6869
/**
6970
* Fills an input ndarray with a specified value.
7071
*
72+
* ## Notes
73+
*
74+
* - A `value` must be able to safely cast to the input ndarray data type. Scalar values having floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the same kind (e.g., a scalar double-precision floating-point number can be used to fill a 'float32' input ndarray).
75+
*
7176
* @param x - input ndarray
7277
* @param value - scalar value
7378
*

lib/main.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020

2121
// MODULES //
2222

23+
var isScalarMostlySafeCompatible = require( '@stdlib/ndarray-base-assert-is-scalar-mostly-safe-compatible' ); // eslint-disable-line id-length
2324
var broadcastScalar = require( '@stdlib/ndarray-base-broadcast-scalar' );
2425
var getDtype = require( '@stdlib/ndarray-base-dtype' );
2526
var getShape = require( '@stdlib/ndarray-base-shape' );
2627
var getOrder = require( '@stdlib/ndarray-base-order' );
2728
var assign = require( '@stdlib/ndarray-base-assign' );
29+
var format = require( '@stdlib/string-format' );
2830

2931

3032
// MAIN //
@@ -40,6 +42,7 @@ var assign = require( '@stdlib/ndarray-base-assign' );
4042
* @param {NonNegativeInteger} x.offset - index offset
4143
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
4244
* @param {*} value - scalar value
45+
* @throws {TypeError} second argument cannot be safely cast to the input array data type
4346
* @returns {void}
4447
*
4548
* @example
@@ -73,10 +76,17 @@ var assign = require( '@stdlib/ndarray-base-assign' );
7376
* // => <Float64Array>[ 10.0, 10.0, 10.0, 10.0, 10.0, 10.0 ]
7477
*/
7578
function fill( x, value ) {
79+
var dt;
7680
var v;
7781

82+
dt = getDtype( x );
83+
84+
// Safe casts are always allowed and allow same kind casts (i.e., downcasts) only when the output data type is floating-point...
85+
if ( !isScalarMostlySafeCompatible( value, dt ) ) {
86+
throw new TypeError( format( 'invalid argument. The second argument cannot be safely cast to the input array data type. Data type: %s. Value: `%s`.', dt, value ) );
87+
}
7888
// Broadcast the fill value to an ndarray of same shape and data type as the input ndarray:
79-
v = broadcastScalar( value, getDtype( x ), getShape( x ), getOrder( x ) );
89+
v = broadcastScalar( value, dt, getShape( x ), getOrder( x ) );
8090

8191
// Assign the fill value to each element of the input ndarray:
8292
assign( [ v, x ] ); // TODO: consider replacing with ndarray/base/assign-scalar in order to avoid zero-dimensional ndarray creation and subsequent broadcasting

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@
3737
"url": "https://github.com/stdlib-js/stdlib/issues"
3838
},
3939
"dependencies": {
40+
"@stdlib/ndarray-base-assert-is-scalar-mostly-safe-compatible": "github:stdlib-js/ndarray-base-assert-is-scalar-mostly-safe-compatible#main",
4041
"@stdlib/ndarray-base-assign": "^0.1.1",
4142
"@stdlib/ndarray-base-broadcast-scalar": "^0.2.2",
4243
"@stdlib/ndarray-base-dtype": "^0.2.2",
4344
"@stdlib/ndarray-base-order": "^0.2.2",
4445
"@stdlib/ndarray-base-shape": "^0.2.2",
45-
"@stdlib/types": "^0.4.3"
46+
"@stdlib/string-format": "^0.2.2",
47+
"@stdlib/types": "^0.4.3",
48+
"@stdlib/error-tools-fmtprodmsg": "^0.2.2"
4649
},
4750
"devDependencies": {
4851
"@stdlib/array-complex128": "^0.3.0",

test/test.js

+33
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,39 @@ tape( 'main export is a function', function test( t ) {
4343
t.end();
4444
});
4545

46+
tape( 'the function throws an error if provided a second argument which cannot be safely cast to the input ndarray data type', function test( t ) {
47+
var values;
48+
var x;
49+
var i;
50+
51+
x = scalar2ndarray( 0.0, {
52+
'dtype': 'int32'
53+
});
54+
55+
values = [
56+
'5',
57+
3.14,
58+
NaN,
59+
true,
60+
false,
61+
null,
62+
void 0,
63+
[],
64+
{},
65+
function noop() {}
66+
];
67+
for ( i = 0; i < values.length; i++ ) {
68+
t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] );
69+
}
70+
t.end();
71+
72+
function badValue( value ) {
73+
return function badValue() {
74+
fill( x, value );
75+
};
76+
}
77+
});
78+
4679
tape( 'the function fills a 0-dimensional input ndarray with a specified value', function test( t ) {
4780
var expected;
4881
var x;

0 commit comments

Comments
 (0)