README.md (2463B)
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 # Flipsign 22 23 > Return a [double-precision floating-point number][ieee754] with the magnitude of `x` and the sign of `x*y`. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var flipsign = require( '@stdlib/math/base/special/flipsign' ); 31 ``` 32 33 #### flipsign( x, y ) 34 35 Returns a [double-precision floating-point number][ieee754] with the magnitude of `x` and the sign of `x*y`; i.e., only return `-x` when `y` is a negative number. 36 37 ```javascript 38 var z = flipsign( -3.14, 10.0 ); 39 // returns -3.14 40 41 z = flipsign( -3.14, -1.0 ); 42 // returns 3.14 43 44 z = flipsign( 1.0, -0.0 ); 45 // returns -1.0 46 47 z = flipsign( -3.14, -0.0 ); 48 // returns 3.14 49 50 z = flipsign( -0.0, 1.0 ); 51 // returns -0.0 52 53 z = flipsign( 0.0, -1.0 ); 54 // returns -0.0 55 ``` 56 57 </section> 58 59 <!-- /.usage --> 60 61 <section class="notes"> 62 63 ## Notes 64 65 - According to the [IEEE 754][ieee754] standard, a `NaN` has a biased exponent equal to `2047`, a significand greater than `0`, and a sign bit equal to **either** `1` **or** `0`. In which case, `NaN` may not correspond to just one but many binary representations. Accordingly, care should be taken to ensure that `y` is **not** `NaN`, else behavior may be indeterminate. 66 67 </section> 68 69 <!-- /.notes --> 70 71 <section class="examples"> 72 73 ## Examples 74 75 <!-- eslint no-undef: "error" --> 76 77 ```javascript 78 var randu = require( '@stdlib/random/base/randu' ); 79 var flipsign = require( '@stdlib/math/base/special/flipsign' ); 80 81 var x; 82 var y; 83 var z; 84 var i; 85 86 // Generate random double-precision floating-point numbers `x` and `y` and flip the sign of `x` only if `y` is negative... 87 for ( i = 0; i < 100; i++ ) { 88 x = (randu()*100.0) - 50.0; 89 y = (randu()*10.0) - 5.0; 90 z = flipsign( x, y ); 91 console.log( 'x: %d, y: %d => %d', x, y, z ); 92 } 93 ``` 94 95 </section> 96 97 <!-- /.examples --> 98 99 <section class="links"> 100 101 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 102 103 </section> 104 105 <!-- /.links -->