About stdlib...
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
Return a double-precision floating-point number with the magnitude of
x
and the sign ofy
.
To use in Observable,
copysign = require( 'https://cdn.jsdelivr.net/gh/stdlib-js/math-base-special-copysign@umd/browser.js' )
To vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:
var copysign = require( 'path/to/vendor/umd/math-base-special-copysign/index.js' )
To include the bundle in a webpage,
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/math-base-special-copysign@umd/browser.js"></script>
If no recognized module system is present, access bundle contents via the global scope:
<script type="text/javascript">
(function () {
window.copysign;
})();
</script>
Returns a double-precision floating-point number with the magnitude of x
and the sign of y
.
var z = copysign( -3.14, 10.0 );
// returns 3.14
z = copysign( 3.14, -1.0 );
// returns -3.14
z = copysign( 1.0, -0.0 );
// returns -1.0
z = copysign( -3.14, -0.0 );
// returns -3.14
z = copysign( -0.0, 1.0 );
// returns 0.0
- According to the IEEE754 standard, a
NaN
has a biased exponent equal to2047
, a significand greater than0
, and a sign bit equal to either1
or0
. In which case,NaN
may not correspond to just one but many binary representations. Accordingly, care should be taken to ensure thaty
is notNaN
; otherwise, behavior may be indeterminate.
<!DOCTYPE html>
<html lang="en">
<body>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/random-array-uniform@umd/browser.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/console-log-each-map@umd/browser.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/math-base-special-copysign@umd/browser.js"></script>
<script type="text/javascript">
(function () {
var opts = {
'dtype': 'float64'
};
var x = uniform( 100, -50.0, 50.0, opts );
var y = uniform( 100, -5.0, 5.0, opts );
// Generate random double-precision floating-point numbers `x` and `y` and copy the sign of `y` to `x`...
logEachMap( 'x: %0.4f, y: %0.4f => %0.4f', x, y, copysign );
})();
</script>
</body>
</html>
@stdlib/math-base/special/flipsign
: return a double-precision floating-point number with the magnitude of x and the sign of x*y.
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
See LICENSE.
Copyright © 2016-2025. The Stdlib Authors.