Добавление проверки при делении, стандартизация возвращаемого значения (BGC_SUCCESS, BGC_FAILURE)
This commit is contained in:
parent
a4b9f8b2b9
commit
e9558ff977
27 changed files with 589 additions and 370 deletions
|
|
@ -174,12 +174,12 @@ inline int bgc_fp32_complex_normalize(BGC_FP32_Complex* number)
|
|||
{
|
||||
const float square_modulus = bgc_fp32_complex_get_square_modulus(number);
|
||||
|
||||
if (bgc_fp32_is_square_unit(square_modulus)) {
|
||||
return 1;
|
||||
if (square_modulus <= BGC_FP32_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
if (square_modulus <= BGC_FP32_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return 0;
|
||||
if (bgc_fp32_is_square_unit(square_modulus)) {
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
const float multiplicand = sqrtf(1.0f / square_modulus);
|
||||
|
|
@ -187,19 +187,19 @@ inline int bgc_fp32_complex_normalize(BGC_FP32_Complex* number)
|
|||
number->real *= multiplicand;
|
||||
number->imaginary *= multiplicand;
|
||||
|
||||
return 1;
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
inline int bgc_fp64_complex_normalize(BGC_FP64_Complex* number)
|
||||
{
|
||||
const double square_modulus = bgc_fp64_complex_get_square_modulus(number);
|
||||
|
||||
if (bgc_fp64_is_square_unit(square_modulus)) {
|
||||
return 1;
|
||||
if (square_modulus <= BGC_FP64_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
if (square_modulus <= BGC_FP64_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return 0;
|
||||
if (bgc_fp64_is_square_unit(square_modulus)) {
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
const double multiplicand = sqrt(1.0 / square_modulus);
|
||||
|
|
@ -207,23 +207,23 @@ inline int bgc_fp64_complex_normalize(BGC_FP64_Complex* number)
|
|||
number->real *= multiplicand;
|
||||
number->imaginary *= multiplicand;
|
||||
|
||||
return 1;
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
inline int bgc_fp32_complex_get_normalized(BGC_FP32_Complex* normalized, const BGC_FP32_Complex* number)
|
||||
{
|
||||
const float square_modulus = bgc_fp32_complex_get_square_modulus(number);
|
||||
|
||||
if (bgc_fp32_is_square_unit(square_modulus)) {
|
||||
normalized->real = number->real;
|
||||
normalized->imaginary = number->imaginary;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (square_modulus <= BGC_FP32_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
normalized->real = 0.0f;
|
||||
normalized->imaginary = 0.0f;
|
||||
return 0;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
if (bgc_fp32_is_square_unit(square_modulus)) {
|
||||
normalized->real = number->real;
|
||||
normalized->imaginary = number->imaginary;
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
const float multiplicand = sqrtf(1.0f / square_modulus);
|
||||
|
|
@ -231,23 +231,23 @@ inline int bgc_fp32_complex_get_normalized(BGC_FP32_Complex* normalized, const B
|
|||
normalized->real = number->real * multiplicand;
|
||||
normalized->imaginary = number->imaginary * multiplicand;
|
||||
|
||||
return 1;
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
inline int bgc_fp64_complex_get_normalized(BGC_FP64_Complex* normalized, const BGC_FP64_Complex* number)
|
||||
{
|
||||
const double square_modulus = bgc_fp64_complex_get_square_modulus(number);
|
||||
|
||||
if (bgc_fp64_is_square_unit(square_modulus)) {
|
||||
normalized->real = number->real;
|
||||
normalized->imaginary = number->imaginary;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (square_modulus <= BGC_FP64_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
normalized->real = 0.0;
|
||||
normalized->imaginary = 0.0;
|
||||
return 0;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
if (bgc_fp64_is_square_unit(square_modulus)) {
|
||||
normalized->real = number->real;
|
||||
normalized->imaginary = number->imaginary;
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
const double multiplicand = sqrt(1.0 / square_modulus);
|
||||
|
|
@ -255,7 +255,7 @@ inline int bgc_fp64_complex_get_normalized(BGC_FP64_Complex* normalized, const B
|
|||
normalized->real = number->real * multiplicand;
|
||||
normalized->imaginary = number->imaginary * multiplicand;
|
||||
|
||||
return 1;
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
// ================= Conjugate ================== //
|
||||
|
|
@ -289,7 +289,7 @@ inline int bgc_fp32_complex_get_inverse(BGC_FP32_Complex* inverse, const BGC_FP3
|
|||
const float square_modulus = bgc_fp32_complex_get_square_modulus(number);
|
||||
|
||||
if (square_modulus <= BGC_FP32_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return 0;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
const float multiplicand = 1.0f / square_modulus;
|
||||
|
|
@ -297,7 +297,7 @@ inline int bgc_fp32_complex_get_inverse(BGC_FP32_Complex* inverse, const BGC_FP3
|
|||
inverse->real = number->real * multiplicand;
|
||||
inverse->imaginary = -number->imaginary * multiplicand;
|
||||
|
||||
return 1;
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
inline int bgc_fp64_complex_get_inverse(BGC_FP64_Complex* inverse, const BGC_FP64_Complex* number)
|
||||
|
|
@ -305,7 +305,7 @@ inline int bgc_fp64_complex_get_inverse(BGC_FP64_Complex* inverse, const BGC_FP6
|
|||
const double square_modulus = bgc_fp64_complex_get_square_modulus(number);
|
||||
|
||||
if (square_modulus <= BGC_FP64_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return 0;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
const double multiplicand = 1.0 / square_modulus;
|
||||
|
|
@ -313,7 +313,7 @@ inline int bgc_fp64_complex_get_inverse(BGC_FP64_Complex* inverse, const BGC_FP6
|
|||
inverse->real = number->real * multiplicand;
|
||||
inverse->imaginary = -number->imaginary * multiplicand;
|
||||
|
||||
return 1;
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
inline int bgc_fp32_complex_invert(BGC_FP32_Complex* number)
|
||||
|
|
@ -374,90 +374,142 @@ inline void bgc_fp64_complex_subtract(BGC_FP64_Complex* difference, const BGC_FP
|
|||
difference->imaginary = minuend->imaginary - subtrahend->imaginary;
|
||||
}
|
||||
|
||||
// ================== Multiply ================== //
|
||||
// ========== Multiply By Real Number =========== //
|
||||
|
||||
inline void bgc_fp32_complex_get_product(BGC_FP32_Complex* product, const BGC_FP32_Complex* number1, const BGC_FP32_Complex* number2)
|
||||
{
|
||||
const float real = number1->real * number2->real - number1->imaginary * number2->imaginary;
|
||||
const float imaginary = number1->real * number2->imaginary + number1->imaginary * number2->real;
|
||||
|
||||
product->real = real;
|
||||
product->imaginary = imaginary;
|
||||
}
|
||||
|
||||
inline void bgc_fp64_complex_get_product(BGC_FP64_Complex* product, const BGC_FP64_Complex* number1, const BGC_FP64_Complex* number2)
|
||||
{
|
||||
const double real = number1->real * number2->real - number1->imaginary * number2->imaginary;
|
||||
const double imaginary = number1->real * number2->imaginary + number1->imaginary * number2->real;
|
||||
|
||||
product->real = real;
|
||||
product->imaginary = imaginary;
|
||||
}
|
||||
|
||||
// ============= Multiply By Number ============= //
|
||||
|
||||
inline void bgc_fp32_complex_multiply(BGC_FP32_Complex* product, const BGC_FP32_Complex* multiplicand, const float multiplier)
|
||||
inline void bgc_fp32_complex_multiply_by_real(BGC_FP32_Complex* product, const BGC_FP32_Complex* multiplicand, const float multiplier)
|
||||
{
|
||||
product->real = multiplicand->real * multiplier;
|
||||
product->imaginary = multiplicand->imaginary * multiplier;
|
||||
}
|
||||
|
||||
inline void bgc_fp64_complex_multiply(BGC_FP64_Complex* product, const BGC_FP64_Complex* multiplicand, const double multiplier)
|
||||
inline void bgc_fp64_complex_multiply_by_real(BGC_FP64_Complex* product, const BGC_FP64_Complex* multiplicand, const double multiplier)
|
||||
{
|
||||
product->real = multiplicand->real * multiplier;
|
||||
product->imaginary = multiplicand->imaginary * multiplier;
|
||||
}
|
||||
|
||||
// =================== Divide =================== //
|
||||
// ========= Multiply By Complex Number ========= //
|
||||
|
||||
inline int bgc_fp32_complex_get_ratio(BGC_FP32_Complex* quotient, const BGC_FP32_Complex* divident, const BGC_FP32_Complex* divisor)
|
||||
inline void bgc_fp32_complex_multiply_by_complex(BGC_FP32_Complex* product, const BGC_FP32_Complex* multiplicand, const BGC_FP32_Complex* multiplier)
|
||||
{
|
||||
const float real = multiplicand->real * multiplier->real - multiplicand->imaginary * multiplier->imaginary;
|
||||
const float imaginary = multiplicand->real * multiplier->imaginary + multiplicand->imaginary * multiplier->real;
|
||||
|
||||
product->real = real;
|
||||
product->imaginary = imaginary;
|
||||
}
|
||||
|
||||
inline void bgc_fp64_complex_multiply_by_complex(BGC_FP64_Complex* product, const BGC_FP64_Complex* multiplicand, const BGC_FP64_Complex* multiplier)
|
||||
{
|
||||
const double real = multiplicand->real * multiplier->real - multiplicand->imaginary * multiplier->imaginary;
|
||||
const double imaginary = multiplicand->real * multiplier->imaginary + multiplicand->imaginary * multiplier->real;
|
||||
|
||||
product->real = real;
|
||||
product->imaginary = imaginary;
|
||||
}
|
||||
|
||||
// ======== Multiply By Conjugate Number ======== //
|
||||
|
||||
inline void bgc_fp32_complex_multiply_by_conjugate(BGC_FP32_Complex* product, const BGC_FP32_Complex* multiplicand, const BGC_FP32_Complex* multiplier_to_conjugate)
|
||||
{
|
||||
const float real = multiplicand->real * multiplier_to_conjugate->real + multiplicand->imaginary * multiplier_to_conjugate->imaginary;
|
||||
const float imaginary = multiplicand->imaginary * multiplier_to_conjugate->real - multiplicand->real * multiplier_to_conjugate->imaginary;
|
||||
|
||||
product->real = real;
|
||||
product->imaginary = imaginary;
|
||||
}
|
||||
|
||||
inline void bgc_fp64_complex_multiply_by_conjugate(BGC_FP64_Complex* product, const BGC_FP64_Complex* multiplicand, const BGC_FP64_Complex* multiplier_to_conjugate)
|
||||
{
|
||||
const double real = multiplicand->real * multiplier_to_conjugate->real + multiplicand->imaginary * multiplier_to_conjugate->imaginary;
|
||||
const double imaginary = multiplicand->imaginary * multiplier_to_conjugate->real - multiplicand->real * multiplier_to_conjugate->imaginary;
|
||||
|
||||
product->real = real;
|
||||
product->imaginary = imaginary;
|
||||
}
|
||||
|
||||
// =========== Divide by Real Number ============ //
|
||||
|
||||
inline int bgc_fp32_complex_divide_by_real(BGC_FP32_Complex* quotient, const BGC_FP32_Complex* dividend, const float divisor)
|
||||
{
|
||||
if (bgc_fp32_is_zero(divisor) || isnan(divisor)) {
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
bgc_fp32_complex_multiply_by_real(quotient, dividend, 1.0f / divisor);
|
||||
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
inline int bgc_fp64_complex_divide_by_real(BGC_FP64_Complex* quotient, const BGC_FP64_Complex* dividend, const double divisor)
|
||||
{
|
||||
if (bgc_fp64_is_zero(divisor) || isnan(divisor)) {
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
bgc_fp64_complex_multiply_by_real(quotient, dividend, 1.0 / divisor);
|
||||
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
// ========== Divide by Complex Number ========== //
|
||||
|
||||
inline int bgc_fp32_complex_divide_by_complex(BGC_FP32_Complex* quotient, const BGC_FP32_Complex* divident, const BGC_FP32_Complex* divisor)
|
||||
{
|
||||
const float square_modulus = bgc_fp32_complex_get_square_modulus(divisor);
|
||||
|
||||
if (square_modulus <= BGC_FP32_SQUARE_EPSILON) {
|
||||
return 0;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
const float real = divident->real * divisor->real + divident->imaginary * divisor->imaginary;
|
||||
const float imaginary = divident->imaginary * divisor->real - divident->real * divisor->imaginary;
|
||||
bgc_fp32_complex_multiply_by_conjugate(quotient, divident, divisor);
|
||||
bgc_fp32_complex_multiply_by_real(quotient, quotient, 1.0f / square_modulus);
|
||||
|
||||
const float multiplier = 1.0f / square_modulus;
|
||||
|
||||
quotient->real = real * multiplier;
|
||||
quotient->imaginary = imaginary * multiplier;
|
||||
|
||||
return 1;
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
inline int bgc_fp64_complex_get_ratio(BGC_FP64_Complex* quotient, const BGC_FP64_Complex* divident, const BGC_FP64_Complex* divisor)
|
||||
inline int bgc_fp64_complex_divide_by_complex(BGC_FP64_Complex* quotient, const BGC_FP64_Complex* divident, const BGC_FP64_Complex* divisor)
|
||||
{
|
||||
const double square_modulus = bgc_fp64_complex_get_square_modulus(divisor);
|
||||
|
||||
if (square_modulus <= BGC_FP64_SQUARE_EPSILON) {
|
||||
return 0;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
const double real = divident->real * divisor->real + divident->imaginary * divisor->imaginary;
|
||||
const double imaginary = divident->imaginary * divisor->real - divident->real * divisor->imaginary;
|
||||
bgc_fp64_complex_multiply_by_conjugate(quotient, divident, divisor);
|
||||
bgc_fp64_complex_multiply_by_real(quotient, quotient, 1.0 / square_modulus);
|
||||
|
||||
const double multiplier = 1.0 / square_modulus;
|
||||
|
||||
quotient->real = real * multiplier;
|
||||
quotient->imaginary = imaginary * multiplier;
|
||||
|
||||
return 1;
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
// ============== Divide By Number ============== //
|
||||
// ========= Divide By Conjugate Number ========= //
|
||||
|
||||
inline void bgc_fp32_complex_divide(BGC_FP32_Complex* quotient, const BGC_FP32_Complex* dividend, const float divisor)
|
||||
inline int bgc_fp32_complex_divide_by_conjugate(BGC_FP32_Complex* quotient, const BGC_FP32_Complex* divident, const BGC_FP32_Complex* divisor_to_conjugate)
|
||||
{
|
||||
bgc_fp32_complex_multiply(quotient, dividend, 1.0f / divisor);
|
||||
const float square_modulus = bgc_fp32_complex_get_square_modulus(divisor_to_conjugate);
|
||||
|
||||
if (square_modulus <= BGC_FP32_SQUARE_EPSILON) {
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
bgc_fp32_complex_multiply_by_complex(quotient, divident, divisor_to_conjugate);
|
||||
bgc_fp32_complex_multiply_by_real(quotient, quotient, 1.0f / square_modulus);
|
||||
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
inline void bgc_fp64_complex_divide(BGC_FP64_Complex* quotient, const BGC_FP64_Complex* dividend, const double divisor)
|
||||
inline int bgc_fp64_complex_divide_by_conjugate(BGC_FP64_Complex* quotient, const BGC_FP64_Complex* divident, const BGC_FP64_Complex* divisor_to_conjugate)
|
||||
{
|
||||
bgc_fp64_complex_multiply(quotient, dividend, 1.0 / divisor);
|
||||
const double square_modulus = bgc_fp64_complex_get_square_modulus(divisor_to_conjugate);
|
||||
|
||||
if (square_modulus <= BGC_FP64_SQUARE_EPSILON) {
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
bgc_fp32_complex_multiply_by_complex(quotient, divident, divisor_to_conjugate);
|
||||
bgc_fp32_complex_multiply_by_real(quotient, quotient, 1.0 / square_modulus);
|
||||
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
// ================== Average2 ================== //
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue