README.md (32045B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Float32Array 22 23 > [Typed array][mdn-typed-array] constructor which returns a [typed array][mdn-typed-array] representing an array of single-precision floating-point numbers in the platform byte order. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var Float32Array = require( '@stdlib/array/float32' ); 41 ``` 42 43 #### Float32Array() 44 45 A [typed array][mdn-typed-array] constructor which returns a [typed array][mdn-typed-array] representing an array of single-precision floating-point numbers in the platform byte order. 46 47 <!-- eslint-disable stdlib/require-globals --> 48 49 ```javascript 50 var arr = new Float32Array(); 51 // returns <Float32Array> 52 ``` 53 54 #### Float32Array( length ) 55 56 Returns a [typed array][mdn-typed-array] having a specified length. 57 58 <!-- eslint-disable stdlib/require-globals --> 59 60 ```javascript 61 var arr = new Float32Array( 5 ); 62 // returns <Float32Array>[ 0.0, 0.0, 0.0, 0.0, 0.0 ] 63 ``` 64 65 #### Float32Array( typedarray ) 66 67 Creates a [typed array][mdn-typed-array] from another [typed array][mdn-typed-array]. 68 69 <!-- eslint-disable stdlib/require-globals --> 70 71 ```javascript 72 var Float64Array = require( '@stdlib/array/float64' ); 73 74 var arr1 = new Float64Array( [ 0.5, 0.5, 0.5 ] ); 75 var arr2 = new Float32Array( arr1 ); 76 // returns <Float32Array>[ 0.5, 0.5, 0.5 ] 77 ``` 78 79 #### Float32Array( obj ) 80 81 Creates a [typed array][mdn-typed-array] from an array-like `object` or iterable. 82 83 <!-- eslint-disable stdlib/require-globals --> 84 85 ```javascript 86 var arr = new Float32Array( [ 0.5, 0.5, 0.5 ] ); 87 // returns <Float32Array>[ 0.5, 0.5, 0.5 ] 88 ``` 89 90 #### Float32Array( buffer\[, byteOffset\[, length]] ) 91 92 Returns a [typed array][mdn-typed-array] view of an [`ArrayBuffer`][@stdlib/array/buffer]. 93 94 <!-- eslint-disable stdlib/require-globals --> 95 96 ```javascript 97 var ArrayBuffer = require( '@stdlib/array/buffer' ); 98 99 var buf = new ArrayBuffer( 16 ); 100 var arr = new Float32Array( buf, 0, 4 ); 101 // returns <Float32Array>[ 0.0, 0.0, 0.0, 0.0 ] 102 ``` 103 104 * * * 105 106 ### Properties 107 108 <a name="static-prop-bytes-per-element"></a> 109 110 #### Float32Array.BYTES_PER_ELEMENT 111 112 Number of bytes per view element. 113 114 <!-- eslint-disable stdlib/require-globals --> 115 116 ```javascript 117 var nbytes = Float32Array.BYTES_PER_ELEMENT; 118 // returns 4 119 ``` 120 121 <a name="static-prop-name"></a> 122 123 #### Float32Array.name 124 125 [Typed array][mdn-typed-array] constructor name. 126 127 <!-- eslint-disable stdlib/require-globals --> 128 129 ```javascript 130 var str = Float32Array.name; 131 // returns 'Float32Array' 132 ``` 133 134 <a name="prop-buffer"></a> 135 136 #### Float32Array.prototype.buffer 137 138 **Read-only** property which returns the [`ArrayBuffer`][@stdlib/array/buffer] referenced by the [typed array][mdn-typed-array]. 139 140 <!-- eslint-disable stdlib/require-globals --> 141 142 ```javascript 143 var arr = new Float32Array( 5 ); 144 var buf = arr.buffer; 145 // returns <ArrayBuffer> 146 ``` 147 148 <a name="prop-byte-length"></a> 149 150 #### Float32Array.prototype.byteLength 151 152 **Read-only** property which returns the length (in bytes) of the [typed array][mdn-typed-array]. 153 154 <!-- eslint-disable stdlib/require-globals --> 155 156 ```javascript 157 var arr = new Float32Array( 5 ); 158 var byteLength = arr.byteLength; 159 // returns 20 160 ``` 161 162 <a name="prop-byte-offset"></a> 163 164 #### Float32Array.prototype.byteOffset 165 166 **Read-only** property which returns the offset (in bytes) of the [typed array][mdn-typed-array] from the start of its [`ArrayBuffer`][@stdlib/array/buffer]. 167 168 <!-- eslint-disable stdlib/require-globals --> 169 170 ```javascript 171 var arr = new Float32Array( 5 ); 172 var byteOffset = arr.byteOffset; 173 // returns 0 174 ``` 175 176 <a name="prop-bytes-per-element"></a> 177 178 #### Float32Array.prototype.BYTES_PER_ELEMENT 179 180 Number of bytes per view element. 181 182 <!-- eslint-disable stdlib/require-globals --> 183 184 ```javascript 185 var arr = new Float32Array( 5 ); 186 var nbytes = arr.BYTES_PER_ELEMENT; 187 // returns 4 188 ``` 189 190 <a name="prop-length"></a> 191 192 #### Float32Array.prototype.length 193 194 **Read-only** property which returns the number of view elements. 195 196 <!-- eslint-disable stdlib/require-globals --> 197 198 ```javascript 199 var arr = new Float32Array( 5 ); 200 var len = arr.length; 201 // returns 5 202 ``` 203 204 * * * 205 206 ### Methods 207 208 <a name="static-method-from"></a> 209 210 #### Float32Array.from( src\[, map\[, thisArg]] ) 211 212 Creates a new typed array from an array-like `object` or an iterable. 213 214 <!-- eslint-disable stdlib/require-globals --> 215 216 ```javascript 217 var arr = Float32Array.from( [ 1.0, 2.0 ] ); 218 // returns <Float32Array>[ 1.0, 2.0 ] 219 ``` 220 221 To invoke a function for each `src` value, provide a callback function. 222 223 <!-- eslint-disable stdlib/require-globals --> 224 225 ```javascript 226 function mapFcn( v ) { 227 return v * 2.0; 228 } 229 230 var arr = Float32Array.from( [ 1.0, 2.0 ], mapFcn ); 231 // returns <Float32Array>[ 2.0, 4.0 ] 232 ``` 233 234 A callback function is provided two arguments: 235 236 - `value`: source value 237 - `index`: source index 238 239 To set the callback execution context, provide a `thisArg`. 240 241 <!-- eslint-disable stdlib/require-globals --> 242 243 ```javascript 244 function mapFcn( v ) { 245 this.count += 1; 246 return v * 2.0; 247 } 248 249 var ctx = { 250 'count': 0 251 }; 252 253 var arr = Float32Array.from( [ 1.0, 2.0 ], mapFcn, ctx ); 254 // returns <Float32Array>[ 2.0, 4.0 ] 255 256 var n = ctx.count; 257 // returns 2 258 ``` 259 260 <a name="static-method-of"></a> 261 262 #### Float32Array.of( element0\[, element1\[, ...elementN]] ) 263 264 Creates a new typed array from a variable number of arguments. 265 266 <!-- eslint-disable stdlib/require-globals --> 267 268 ```javascript 269 var arr = Float32Array.of( 1.0, 2.0 ); 270 // returns <Float32Array>[ 1.0, 2.0 ] 271 ``` 272 273 <a name="method-copy-within"></a> 274 275 #### Float32Array.prototype.copyWithin( target, start\[, end] ) 276 277 Copies a sequence of elements within an array starting at `start` and ending at `end` (non-inclusive) to the position starting at `target`. 278 279 <!-- eslint-disable stdlib/require-globals --> 280 281 ```javascript 282 var arr = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 283 284 // Copy the last two elements to the first two elements: 285 arr.copyWithin( 0, 3 ); 286 287 var v = arr[ 0 ]; 288 // returns 4.0 289 290 v = arr[ 1 ]; 291 // returns 5.0 292 ``` 293 294 By default, `end` equals the number of array elements (i.e., one more than the last array index). To limit the sequence length, provide an `end` argument. 295 296 <!-- eslint-disable stdlib/require-globals --> 297 298 ```javascript 299 var arr = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 300 301 // Copy the first two elements to the last two elements: 302 arr.copyWithin( 3, 0, 2 ); 303 304 var v = arr[ 3 ]; 305 // returns 1.0 306 307 v = arr[ 4 ]; 308 // returns 2.0 309 ``` 310 311 When a `target`, `start`, and/or `end` index is negative, the respective index is determined relative to the last array element. The following example achieves the same behavior as the previous example: 312 313 <!-- eslint-disable stdlib/require-globals --> 314 315 ```javascript 316 var arr = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 317 318 // Copy the first two elements to the last two elements: 319 arr.copyWithin( -2, -5, -3 ); 320 321 var v = arr[ 3 ]; 322 // returns 1.0 323 324 v = arr[ 4 ]; 325 // returns 2.0 326 ``` 327 328 <a name="method-entries"></a> 329 330 #### Float32Array.prototype.entries() 331 332 Returns an iterator for iterating over array key-value pairs. 333 334 <!-- eslint-disable stdlib/require-globals --> 335 336 ```javascript 337 var arr = new Float32Array( [ 1.0, 2.0 ] ); 338 339 // Create an iterator: 340 var it = arr.entries(); 341 342 // Iterate over key-value pairs... 343 var v = it.next().value; 344 // returns [ 0, 1.0 ] 345 346 v = it.next().value; 347 // returns [ 1, 2.0 ] 348 349 var bool = it.next().done; 350 // returns true 351 ``` 352 353 <a name="method-every"></a> 354 355 #### Float32Array.prototype.every( predicate\[, thisArg] ) 356 357 Tests whether all array elements pass a test implemented by a `predicate` function. 358 359 <!-- eslint-disable stdlib/require-globals --> 360 361 ```javascript 362 function predicate( v ) { 363 return ( v <= 1.0 ); 364 } 365 366 var arr = new Float32Array( [ 1.0, 2.0 ] ); 367 368 var bool = arr.every( predicate ); 369 // returns false 370 ``` 371 372 A `predicate` function is provided three arguments: 373 374 - `value`: array element 375 - `index`: array index 376 - `arr`: array on which the method is invoked 377 378 To set the callback execution context, provide a `thisArg`. 379 380 <!-- eslint-disable stdlib/require-globals --> 381 382 ```javascript 383 function predicate( v ) { 384 this.count += 1; 385 return ( v >= 1.0 ); 386 } 387 388 var ctx = { 389 'count': 0 390 }; 391 392 var arr = new Float32Array( [ 1.0, 2.0 ] ); 393 394 var bool = arr.every( predicate, ctx ); 395 // returns true 396 397 var n = ctx.count; 398 // returns 2 399 ``` 400 401 <a name="method-fill"></a> 402 403 #### Float32Array.prototype.fill( value\[, start\[, end]] ) 404 405 Fills an array from a `start` index to an `end` index (non-inclusive) with a provided `value`. 406 407 <!-- eslint-disable stdlib/require-globals --> 408 409 ```javascript 410 var arr = new Float32Array( 2 ); 411 412 // Set all array elements to the same value: 413 arr.fill( 2.0 ); 414 415 var v = arr[ 0 ]; 416 // returns 2.0 417 418 v = arr[ 1 ]; 419 // returns 2.0 420 421 // Set all array elements starting from the first index to the same value: 422 arr.fill( 3.0, 1 ); 423 424 v = arr[ 0 ]; 425 // returns 2.0 426 427 v = arr[ 1 ]; 428 // returns 3.0 429 430 // Set all array elements, except the last element, to the same value: 431 arr.fill( 4.0, 0, arr.length-1 ); 432 433 v = arr[ 0 ]; 434 // returns 4.0 435 436 v = arr[ 1 ]; 437 // returns 3.0 438 ``` 439 440 When a `start` and/or `end` index is negative, the respective index is determined relative to the last array element. 441 442 <!-- eslint-disable stdlib/require-globals --> 443 444 ```javascript 445 var arr = new Float32Array( 2 ); 446 447 // Set all array elements, except the last element, to the same value: 448 arr.fill( 2.0, -arr.length, -1 ); 449 450 var v = arr[ 0 ]; 451 // returns 2.0 452 453 v = arr[ 1 ]; 454 // returns 0.0 455 ``` 456 457 <a name="method-filter"></a> 458 459 #### Float32Array.prototype.filter( predicate\[, thisArg] ) 460 461 Creates a new array (of the same data type as the host array) which includes those elements for which a `predicate` function returns a truthy value. 462 463 <!-- eslint-disable stdlib/require-globals --> 464 465 ```javascript 466 function predicate( v ) { 467 return ( v >= 2.0 ); 468 } 469 470 var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 471 472 var arr2 = arr1.filter( predicate ); 473 // returns <Float32Array>[ 2.0, 3.0 ] 474 ``` 475 476 If a `predicate` function does not return a truthy value for any array element, the method returns an empty array. 477 478 <!-- eslint-disable stdlib/require-globals --> 479 480 ```javascript 481 function predicate( v ) { 482 return ( v >= 10.0 ); 483 } 484 485 var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 486 487 var arr2 = arr1.filter( predicate ); 488 // returns <Float32Array>[] 489 ``` 490 491 A `predicate` function is provided three arguments: 492 493 - `value`: array element 494 - `index`: array index 495 - `arr`: array on which the method is invoked 496 497 To set the callback execution context, provide a `thisArg`. 498 499 <!-- eslint-disable stdlib/require-globals --> 500 501 ```javascript 502 function predicate( v ) { 503 this.count += 1; 504 return ( v >= 2.0 ); 505 } 506 507 var ctx = { 508 'count': 0 509 }; 510 511 var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 512 513 var arr2 = arr1.filter( predicate, ctx ); 514 515 var n = ctx.count; 516 // returns 3 517 ``` 518 519 <a name="method-find"></a> 520 521 #### Float32Array.prototype.find( predicate\[, thisArg] ) 522 523 Returns the first array element for which a provided `predicate` function returns a truthy value. 524 525 <!-- eslint-disable stdlib/require-globals --> 526 527 ```javascript 528 function predicate( v ) { 529 return ( v > 2.0 ); 530 } 531 532 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 533 534 var v = arr.find( predicate ); 535 // returns 3.0 536 ``` 537 538 If a `predicate` function does not return a truthy value for any array element, the method returns `undefined`. 539 540 <!-- eslint-disable stdlib/require-globals --> 541 542 ```javascript 543 function predicate( v ) { 544 return ( v < 1.0 ); 545 } 546 547 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 548 549 var v = arr.find( predicate ); 550 // returns undefined 551 ``` 552 553 A `predicate` function is provided three arguments: 554 555 - `value`: array element 556 - `index`: array index 557 - `arr`: array on which the method is invoked 558 559 To set the callback execution context, provide a `thisArg`. 560 561 <!-- eslint-disable stdlib/require-globals --> 562 563 ```javascript 564 function predicate( v ) { 565 this.count += 1; 566 return ( v > 2.0 ); 567 } 568 569 var ctx = { 570 'count': 0 571 }; 572 573 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 574 575 var v = arr.find( predicate, ctx ); 576 // returns 3.0 577 578 var n = ctx.count; 579 // returns 3 580 ``` 581 582 <a name="method-find-index"></a> 583 584 #### Float32Array.prototype.findIndex( predicate\[, thisArg] ) 585 586 Returns the index of the first array element for which a provided `predicate` function returns a truthy value. 587 588 <!-- eslint-disable stdlib/require-globals --> 589 590 ```javascript 591 function predicate( v ) { 592 return ( v >= 3.0 ); 593 } 594 595 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 596 597 var idx = arr.findIndex( predicate ); 598 // returns 2 599 ``` 600 601 If a `predicate` function does not return a truthy value for any array element, the method returns `-1`. 602 603 <!-- eslint-disable stdlib/require-globals --> 604 605 ```javascript 606 function predicate( v ) { 607 return ( v < 1.0 ); 608 } 609 610 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 611 612 var idx = arr.findIndex( predicate ); 613 // returns -1 614 ``` 615 616 A `predicate` function is provided three arguments: 617 618 - `value`: array element 619 - `index`: array index 620 - `arr`: array on which the method is invoked 621 622 To set the callback execution context, provide a `thisArg`. 623 624 <!-- eslint-disable stdlib/require-globals --> 625 626 ```javascript 627 function predicate( v ) { 628 this.count += 1; 629 return ( v >= 3.0 ); 630 } 631 632 var ctx = { 633 'count': 0 634 }; 635 636 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 637 638 var idx = arr.findIndex( predicate, ctx ); 639 // returns 2 640 641 var n = ctx.count; 642 // returns 3 643 ``` 644 645 <a name="method-for-each"></a> 646 647 #### Float32Array.prototype.forEach( fcn\[, thisArg] ) 648 649 Invokes a callback for each array element. 650 651 <!-- eslint-disable stdlib/require-globals --> 652 653 ```javascript 654 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 655 656 var str = ''; 657 658 function fcn( v, i ) { 659 str += i + ':' + v; 660 if ( i < arr.length-1 ) { 661 str += ' '; 662 } 663 } 664 665 arr.forEach( fcn ); 666 667 console.log( str ); 668 // => '0:1 1:2 2:3' 669 ``` 670 671 The callback is provided three arguments: 672 673 - `value`: array element 674 - `index`: array index 675 - `arr`: array on which the method is invoked 676 677 To set the callback execution context, provide a `thisArg`. 678 679 <!-- eslint-disable stdlib/require-globals --> 680 681 ```javascript 682 function fcn() { 683 this.count += 1; 684 } 685 686 var ctx = { 687 'count': 0 688 }; 689 690 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 691 692 arr.forEach( fcn, ctx ); 693 694 var n = ctx.count; 695 // returns 3 696 ``` 697 698 <a name="method-includes"></a> 699 700 #### Float32Array.prototype.includes( searchElement\[, fromIndex] ) 701 702 Returns a `boolean` indicating whether an array includes a search element. 703 704 <!-- eslint-disable stdlib/require-globals --> 705 706 ```javascript 707 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 708 709 var bool = arr.includes( 3.0 ); 710 // returns true 711 712 bool = arr.includes( 0.0 ); 713 // returns false 714 ``` 715 716 By default, the method searches the entire array (`fromIndex = 0`). To begin searching from a specific array index, provide a `fromIndex`. 717 718 <!-- eslint-disable stdlib/require-globals --> 719 720 ```javascript 721 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 722 723 var bool = arr.includes( 1.0, 1 ); 724 // returns false 725 ``` 726 727 When a `fromIndex` is negative, the starting index is resolved relative to the last array element. 728 729 <!-- eslint-disable stdlib/require-globals --> 730 731 ```javascript 732 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 733 734 bool = arr.includes( 1.0, -2 ); 735 // returns false 736 ``` 737 738 The method does **not** distinguish between signed and unsigned zero. 739 740 <a name="method-index-of"></a> 741 742 #### Float32Array.prototype.indexOf( searchElement\[, fromIndex] ) 743 744 Returns the index of the first array element strictly equal to a search element. 745 746 <!-- eslint-disable stdlib/require-globals --> 747 748 ```javascript 749 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 750 751 var idx = arr.indexOf( 3.0 ); 752 // returns 2 753 754 idx = arr.indexOf( 0.0 ); 755 // returns -1 756 ``` 757 758 By default, the method searches the entire array (`fromIndex = 0`). To begin searching from a specific array index, provide a `fromIndex`. 759 760 <!-- eslint-disable stdlib/require-globals --> 761 762 ```javascript 763 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 764 765 var idx = arr.indexOf( 1.0, 1 ); 766 // returns -1 767 ``` 768 769 When a `fromIndex` is negative, the starting index is resolved relative to the last array element. 770 771 <!-- eslint-disable stdlib/require-globals --> 772 773 ```javascript 774 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 775 776 var idx = arr.indexOf( 1.0, -2 ); 777 // returns -1 778 ``` 779 780 The method does **not** distinguish between signed and unsigned zero. 781 782 <a name="method-join"></a> 783 784 #### Float32Array.prototype.join( \[separator] ) 785 786 Serializes an array by joining all array elements as a string. 787 788 <!-- eslint-disable stdlib/require-globals --> 789 790 ```javascript 791 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 792 793 var str = arr.join(); 794 // returns '1,2,3' 795 ``` 796 797 By default, the method delineates array elements using a comma `,`. To specify a custom separator, provide a `separator` string. 798 799 <!-- eslint-disable stdlib/require-globals --> 800 801 ```javascript 802 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 803 804 var str = arr.join( '|' ); 805 // returns '1|2|3' 806 ``` 807 808 <a name="method-keys"></a> 809 810 #### Float32Array.prototype.keys() 811 812 Returns an iterator for iterating over array keys. 813 814 <!-- eslint-disable stdlib/require-globals --> 815 816 ```javascript 817 var arr = new Float32Array( [ 1.0, 2.0 ] ); 818 819 // Create an iterator: 820 var it = arr.keys(); 821 822 // Iterate over keys... 823 var v = it.next().value; 824 // returns 0 825 826 v = it.next().value; 827 // returns 1 828 829 var bool = it.next().done; 830 // returns true 831 ``` 832 833 <a name="method-last-index-of"></a> 834 835 #### Float32Array.prototype.lastIndexOf( searchElement\[, fromIndex] ) 836 837 Returns the index of the last array element strictly equal to a search element, iterating from right to left. 838 839 <!-- eslint-disable stdlib/require-globals --> 840 841 ```javascript 842 var arr = new Float32Array( [ 1.0, 0.0, 2.0, 0.0, 1.0 ] ); 843 844 var idx = arr.lastIndexOf( 0.0 ); 845 // returns 3 846 847 idx = arr.lastIndexOf( 3.0 ); 848 // returns -1 849 ``` 850 851 By default, the method searches the entire array (`fromIndex = -1`). To begin searching from a specific array index, provide a `fromIndex`. 852 853 <!-- eslint-disable stdlib/require-globals --> 854 855 ```javascript 856 var arr = new Float32Array( [ 1.0, 0.0, 2.0, 0.0, 1.0 ] ); 857 858 var idx = arr.lastIndexOf( 0.0, 2 ); 859 // returns 1 860 ``` 861 862 When a `fromIndex` is negative, the starting index is resolved relative to the last array element. 863 864 <!-- eslint-disable stdlib/require-globals --> 865 866 ```javascript 867 var arr = new Float32Array( [ 1.0, 0.0, 2.0, 0.0, 1.0 ] ); 868 869 var idx = arr.lastIndexOf( 0.0, -3 ); 870 // returns 1 871 ``` 872 873 The method does **not** distinguish between signed and unsigned zero. 874 875 <a name="method-map"></a> 876 877 #### Float32Array.prototype.map( fcn\[, thisArg] ) 878 879 Maps each array element to an element in a new array having the same data type as the host array. 880 881 <!-- eslint-disable stdlib/require-globals --> 882 883 ```javascript 884 function fcn( v ) { 885 return v * 2.0; 886 } 887 888 var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 889 890 var arr2 = arr1.map( fcn ); 891 // returns <Float32Array>[ 2.0, 4.0, 6.0 ] 892 ``` 893 894 A callback is provided three arguments: 895 896 - `value`: array element 897 - `index`: array index 898 - `arr`: array on which the method is invoked 899 900 To set the callback execution context, provide a `thisArg`. 901 902 <!-- eslint-disable stdlib/require-globals --> 903 904 ```javascript 905 function fcn( v ) { 906 this.count += 1; 907 return v * 2.0; 908 } 909 910 var ctx = { 911 'count': 0 912 }; 913 914 var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 915 916 var arr2 = arr1.map( fcn, ctx ); 917 918 var n = ctx.count; 919 // returns 3 920 ``` 921 922 <a name="method-reduce"></a> 923 924 #### Float32Array.prototype.reduce( fcn\[, initialValue] ) 925 926 Applies a function against an accumulator and each element in an array and returns the accumulated result. 927 928 <!-- eslint-disable stdlib/require-globals --> 929 930 ```javascript 931 function fcn( acc, v ) { 932 return acc + ( v*v ); 933 } 934 935 var arr = new Float32Array( [ 2.0, 1.0, 3.0 ] ); 936 937 var v = arr.reduce( fcn ); 938 // returns 12.0 939 ``` 940 941 If not provided an initial value, the method invokes a provided function with the first array element as the first argument and the second array element as the second argument. 942 943 If provided an initial value, the method invokes a provided function with the initial value as the first argument and the first array element as the second argument. 944 945 <!-- eslint-disable stdlib/require-globals --> 946 947 ```javascript 948 function fcn( acc, v ) { 949 return acc + ( v*v ); 950 } 951 952 var arr = new Float32Array( [ 2.0, 1.0, 3.0 ] ); 953 954 var v = arr.reduce( fcn, 0.0 ); 955 // returns 14.0 956 ``` 957 958 A callback is provided four arguments: 959 960 - `acc`: accumulated result 961 - `value`: array element 962 - `index`: array index 963 - `arr`: array on which the method is invoked 964 965 <a name="method-reduce-right"></a> 966 967 #### Float32Array.prototype.reduceRight( fcn\[, initialValue] ) 968 969 Applies a function against an accumulator and each element in an array and returns the accumulated result, iterating from right to left. 970 971 <!-- eslint-disable stdlib/require-globals --> 972 973 ```javascript 974 function fcn( acc, v ) { 975 return acc + ( v*v ); 976 } 977 978 var arr = new Float32Array( [ 2.0, 1.0, 3.0 ] ); 979 980 var v = arr.reduceRight( fcn ); 981 // returns 8.0 982 ``` 983 984 If not provided an initial value, the method invokes a provided function with the last array element as the first argument and the second-to-last array element as the second argument. 985 986 If provided an initial value, the method invokes a provided function with the initial value as the first argument and the last array element as the second argument. 987 988 <!-- eslint-disable stdlib/require-globals --> 989 990 ```javascript 991 function fcn( acc, v ) { 992 return acc + ( v*v ); 993 } 994 995 var arr = new Float32Array( [ 2.0, 1.0, 3.0 ] ); 996 997 var v = arr.reduce( fcn, 0.0 ); 998 // returns 14.0 999 ``` 1000 1001 A callback is provided four arguments: 1002 1003 - `acc`: accumulated result 1004 - `value`: array element 1005 - `index`: array index 1006 - `arr`: array on which the method is invoked 1007 1008 <a name="method-reverse"></a> 1009 1010 #### Float32Array.prototype.reverse() 1011 1012 Reverses an array **in-place** (thus mutating the array on which the method is invoked). 1013 1014 <!-- eslint-disable stdlib/require-globals --> 1015 1016 ```javascript 1017 var arr = new Float32Array( [ 2.0, 0.0, 3.0 ] ); 1018 1019 // Reverse the array: 1020 arr.reverse(); 1021 1022 var v = arr[ 0 ]; 1023 // returns 3.0 1024 1025 v = arr[ 1 ]; 1026 // returns 0.0 1027 1028 v = arr[ 2 ]; 1029 // returns 2.0 1030 ``` 1031 1032 <a name="method-set"></a> 1033 1034 #### Float32Array.prototype.set( arr\[, offset] ) 1035 1036 Sets array elements. 1037 1038 <!-- eslint-disable stdlib/require-globals --> 1039 1040 ```javascript 1041 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 1042 // returns <Float32Array>[ 1.0, 2.0, 3.0 ] 1043 1044 // Set the first two array elements: 1045 arr.set( [ 4.0, 5.0 ] ); 1046 1047 var v = arr[ 0 ]; 1048 // returns 4.0 1049 1050 v = arr[ 1 ]; 1051 // returns 5.0 1052 ``` 1053 1054 By default, the method starts writing values at the first array index. To specify an alternative index, provide an index `offset`. 1055 1056 <!-- eslint-disable stdlib/require-globals --> 1057 1058 ```javascript 1059 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 1060 // returns <Float32Array>[ 1.0, 2.0, 3.0 ] 1061 1062 // Set the last two array elements: 1063 arr.set( [ 4.0, 5.0 ], 1 ); 1064 1065 var v = arr[ 1 ]; 1066 // returns 4.0 1067 1068 v = arr[ 2 ]; 1069 // returns 5.0 1070 ``` 1071 1072 <a name="method-slice"></a> 1073 1074 #### Float32Array.prototype.slice( \[begin\[, end]] ) 1075 1076 Copies array elements to a new array with the same underlying data type as the host array. 1077 1078 <!-- eslint-disable stdlib/require-globals --> 1079 1080 ```javascript 1081 var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 1082 1083 var arr2 = arr1.slice(); 1084 1085 var bool = ( arr1 === arr2 ); 1086 // returns false 1087 1088 bool = ( arr1.buffer === arr2.buffer ); 1089 // returns false 1090 1091 var v = arr2[ 0 ]; 1092 // returns 1.0 1093 1094 v = arr2[ 1 ]; 1095 // returns 2.0 1096 1097 v = arr2[ 2 ]; 1098 // returns 3.0 1099 ``` 1100 1101 By default, the method copies elements beginning with the first array element. To specify an alternative array index at which to begin copying, provide a `begin` index (inclusive). 1102 1103 <!-- eslint-disable stdlib/require-globals --> 1104 1105 ```javascript 1106 var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 1107 1108 var arr2 = arr1.slice( 1 ); 1109 1110 var len = arr2.length; 1111 // returns 2 1112 1113 var v = arr2[ 0 ]; 1114 // returns 2.0 1115 1116 v = arr2[ 1 ]; 1117 // returns 3.0 1118 ``` 1119 1120 By default, the method copies all array elements after `begin`. To specify an alternative array index at which to end copying, provide an `end` index (exclusive). 1121 1122 <!-- eslint-disable stdlib/require-globals --> 1123 1124 ```javascript 1125 var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 1126 1127 var arr2 = arr1.slice( 0, 2 ); 1128 1129 var len = arr2.length; 1130 // returns 2 1131 1132 var v = arr2[ 0 ]; 1133 // returns 1.0 1134 1135 v = arr2[ 1 ]; 1136 // returns 2.0 1137 ``` 1138 1139 When a `begin` and/or `end` index is negative, the respective index is determined relative to the last array element. 1140 1141 <!-- eslint-disable stdlib/require-globals --> 1142 1143 ```javascript 1144 var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 1145 1146 var arr2 = arr1.slice( -arr1.length, -1 ); 1147 1148 var len = arr2.length; 1149 // returns 2 1150 1151 var v = arr2[ 0 ]; 1152 // returns 1.0 1153 1154 v = arr2[ 1 ]; 1155 // returns 2.0 1156 ``` 1157 1158 <a name="method-some"></a> 1159 1160 #### Float32Array.prototype.some( predicate\[, thisArg] ) 1161 1162 Tests whether at least one array element passes a test implemented by a `predicate` function. 1163 1164 <!-- eslint-disable stdlib/require-globals --> 1165 1166 ```javascript 1167 function predicate( v ) { 1168 return ( v >= 2.0 ); 1169 } 1170 1171 var arr = new Float32Array( [ 1.0, 2.0 ] ); 1172 1173 var bool = arr.some( predicate ); 1174 // returns true 1175 ``` 1176 1177 A `predicate` function is provided three arguments: 1178 1179 - `value`: array element 1180 - `index`: array index 1181 - `arr`: array on which the method is invoked 1182 1183 To set the callback execution context, provide a `thisArg`. 1184 1185 <!-- eslint-disable stdlib/require-globals --> 1186 1187 ```javascript 1188 function predicate( v ) { 1189 this.count += 1; 1190 return ( v >= 2.0 ); 1191 } 1192 1193 var ctx = { 1194 'count': 0 1195 }; 1196 1197 var arr = new Float32Array( [ 1.0, 1.0 ] ); 1198 1199 var bool = arr.some( predicate, ctx ); 1200 // returns false 1201 1202 var n = ctx.count; 1203 // returns 2 1204 ``` 1205 1206 <a name="method-sort"></a> 1207 1208 #### Float32Array.prototype.sort( \[compareFunction] ) 1209 1210 Sorts an array **in-place** (thus mutating the array on which the method is invoked). 1211 1212 <!-- eslint-disable stdlib/require-globals --> 1213 1214 ```javascript 1215 var arr = new Float32Array( [ 2.0, 3.0, 0.0 ] ); 1216 1217 // Sort the array (in ascending order): 1218 arr.sort(); 1219 1220 var v = arr[ 0 ]; 1221 // returns 0.0 1222 1223 v = arr[ 1 ]; 1224 // returns 2.0 1225 1226 v = arr[ 2 ]; 1227 // returns 3.0 1228 ``` 1229 1230 By default, the method sorts array elements in ascending order. To impose a custom order, provide a `compareFunction`. 1231 1232 <!-- eslint-disable stdlib/require-globals --> 1233 1234 ```javascript 1235 function descending( a, b ) { 1236 return b - a; 1237 } 1238 1239 var arr = new Float32Array( [ 2.0, 3.0, 0.0 ] ); 1240 1241 // Sort the array (in descending order): 1242 arr.sort( descending ); 1243 1244 var v = arr[ 0 ]; 1245 // returns 3.0 1246 1247 v = arr[ 1 ]; 1248 // returns 2.0 1249 1250 v = arr[ 2 ]; 1251 // returns 0.0 1252 ``` 1253 1254 The comparison function is provided two array elements, `a` and `b`, per invocation, and its return value determines the sort order as follows: 1255 1256 - If the comparison function returns a value **less** than zero, then the method sorts `a` to an index lower than `b` (i.e., `a` should come **before** `b`). 1257 - If the comparison function returns a value **greater** than zero, then the method sorts `a` to an index higher than `b` (i.e., `b` should come **before** `a`). 1258 - If the comparison function returns **zero**, then the relative order of `a` and `b` _should_ remain unchanged. 1259 1260 <a name="method-subarray"></a> 1261 1262 #### Float32Array.prototype.subarray( \[begin\[, end]] ) 1263 1264 Creates a new typed array view over the same underlying [`ArrayBuffer`][@stdlib/array/buffer] and with the same underlying data type as the host array. 1265 1266 <!-- eslint-disable stdlib/require-globals --> 1267 1268 ```javascript 1269 var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 1270 1271 var arr2 = arr1.subarray(); 1272 // returns <Float32Array>[ 1.0, 2.0, 3.0 ] 1273 1274 var bool = ( arr1.buffer === arr2.buffer ); 1275 // returns true 1276 ``` 1277 1278 By default, the method creates a typed array view beginning with the first array element. To specify an alternative array index at which to begin, provide a `begin` index (inclusive). 1279 1280 <!-- eslint-disable stdlib/require-globals --> 1281 1282 ```javascript 1283 var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 1284 1285 var arr2 = arr1.subarray( 1 ); 1286 // returns <Float32Array>[ 2.0, 3.0 ] 1287 1288 var bool = ( arr1.buffer === arr2.buffer ); 1289 // returns true 1290 ``` 1291 1292 By default, the method creates a typed array view which includes all array elements after `begin`. To limit the number of array elements after `begin`, provide an `end` index (exclusive). 1293 1294 <!-- eslint-disable stdlib/require-globals --> 1295 1296 ```javascript 1297 var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 1298 1299 var arr2 = arr1.subarray( 0, 2 ); 1300 // returns <Float32Array>[ 1.0, 2.0 ] 1301 1302 var bool = ( arr1.buffer === arr2.buffer ); 1303 // returns true 1304 ``` 1305 1306 When a `begin` and/or `end` index is negative, the respective index is determined relative to the last array element. 1307 1308 <!-- eslint-disable stdlib/require-globals --> 1309 1310 ```javascript 1311 var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 1312 1313 var arr2 = arr1.subarray( -arr1.length, -1 ); 1314 // returns <Float32Array>[ 1.0, 2.0 ] 1315 1316 var bool = ( arr1.buffer === arr2.buffer ); 1317 // returns true 1318 ``` 1319 1320 If the method is unable to resolve indices to a non-empty array subsequence, the method returns an empty typed array. 1321 1322 <!-- eslint-disable stdlib/require-globals --> 1323 1324 ```javascript 1325 var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 1326 1327 var arr2 = arr1.subarray( 10, -1 ); 1328 // returns <Float32Array>[] 1329 ``` 1330 1331 <a name="method-to-locale-string"></a> 1332 1333 #### Float32Array.prototype.toLocaleString( \[locales\[, options]] ) 1334 1335 Serializes an array as a locale-specific `string`. 1336 1337 <!-- eslint-disable stdlib/require-globals --> 1338 1339 ```javascript 1340 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 1341 1342 var str = arr.toLocaleString(); 1343 // returns '1,2,3' 1344 ``` 1345 1346 <a name="method-to-string"></a> 1347 1348 #### Float32Array.prototype.toString() 1349 1350 Serializes an array as a `string`. 1351 1352 <!-- eslint-disable stdlib/require-globals --> 1353 1354 ```javascript 1355 var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 1356 1357 var str = arr.toString(); 1358 // returns '1,2,3' 1359 ``` 1360 1361 <a name="method-values"></a> 1362 1363 #### Float32Array.prototype.values() 1364 1365 Returns an iterator for iterating over array elements. 1366 1367 <!-- eslint-disable stdlib/require-globals --> 1368 1369 ```javascript 1370 var arr = new Float32Array( [ 1.0, 2.0 ] ); 1371 1372 // Create an iterator: 1373 var it = arr.values(); 1374 1375 // Iterate over array elements... 1376 var v = it.next().value; 1377 // returns 1.0 1378 1379 v = it.next().value; 1380 // returns 2.0 1381 1382 var bool = it.next().done; 1383 // returns true 1384 ``` 1385 1386 </section> 1387 1388 <!-- /.usage --> 1389 1390 * * * 1391 1392 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 1393 1394 <section class="notes"> 1395 1396 </section> 1397 1398 <!-- /.notes --> 1399 1400 <!-- Package usage examples. --> 1401 1402 <section class="examples"> 1403 1404 ## Examples 1405 1406 <!-- eslint no-undef: "error" --> 1407 1408 ```javascript 1409 var randu = require( '@stdlib/random/base/randu' ); 1410 var ctor = require( '@stdlib/array/float32' ); 1411 1412 var arr; 1413 var i; 1414 1415 arr = new ctor( 10 ); 1416 for ( i = 0; i < arr.length; i++ ) { 1417 arr[ i ] = randu() * 100.0; 1418 } 1419 console.log( arr ); 1420 ``` 1421 1422 </section> 1423 1424 <!-- /.examples --> 1425 1426 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 1427 1428 <section class="references"> 1429 1430 </section> 1431 1432 <!-- /.references --> 1433 1434 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 1435 1436 <section class="links"> 1437 1438 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 1439 1440 [@stdlib/array/buffer]: https://www.npmjs.com/package/@stdlib/array/tree/main/buffer 1441 1442 </section> 1443 1444 <!-- /.links -->