_copysign, _copysignl

From RAD Studio
Jump to: navigation, search

Go Up to float.h Index


Header File

float.h

Prototype

double _copysign(double da, double db);
long double _copysignl(long double lda, long double ldb);

Description

Returns the double-precision floating point argument da, with the same sign as the double-precision floating-point argument db.

_copysignl is the long double version; it takes a long double argument and returns a long double result.

Return Value

Returns the first value with the same magnitude and exponent, but with the sign of the second value. There is no error value returned. When you use 0 as the second parameter, the return value is the first parameter with the same magnitude and a + sign.

Portability

POSIX ANSI C ANSI C++ Win32 Win64 macOS
_copysign +
_copysignl +

Example

#include <float.h>
#include <stdio.h>

/* Compares a to b. But ignores b's sign and uses a's one */
int compare_no_sign(double a, double b) {
	/* Obtain a value of b with the same sign as a */
	double b_as_a = '' '_copysign' ''(b, a);

	/* Do a simple double comparison */
	if (a > b_as_a)
		return 1;
	else if (a < b_as_a)
		return -1;
	else
		return 0;
}