cacos.c revision ddc0e0b53c661f6e439e3b7072b3ef353eadb4af
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * CDDL HEADER START
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * The contents of this file are subject to the terms of the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Common Development and Distribution License (the "License").
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * You may not use this file except in compliance with the License.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * or http://www.opensolaris.org/os/licensing.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * See the License for the specific language governing permissions
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * and limitations under the License.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner]
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * CDDL HEADER END
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Use is subject to license terms.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
ddc0e0b53c661f6e439e3b7072b3ef353eadb4afRichard Lowe#pragma weak __cacos = cacos
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* INDENT OFF */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * dcomplex cacos(dcomplex z);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Alogrithm
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * (based on T.E.Hull, Thomas F. Fairgrieve and Ping Tak Peter Tang's
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * paper "Implementing the Complex Arcsine and Arccosine Functins Using
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Exception Handling", ACM TOMS, Vol 23, pp 299-335)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * The principal value of complex inverse cosine function cacos(z),
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * where z = x+iy, can be defined by
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos(z) = acos(B) - i sign(y) log (A + sqrt(A*A-1)),
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * where the log function is the natural log, and
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ____________ ____________
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 1 / 2 2 1 / 2 2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * A = --- / (x+1) + y + --- / (x-1) + y
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 2 \/ 2 \/
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ____________ ____________
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 1 / 2 2 1 / 2 2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * B = --- / (x+1) + y - --- / (x-1) + y .
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 2 \/ 2 \/
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * The Branch cuts are on the real line from -inf to -1 and from 1 to inf.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * The real and imaginary parts are based on Abramowitz and Stegun
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [Handbook of Mathematic Functions, 1972]. The sign of the imaginary
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * part is chosen to be the generally considered the principal value of
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * this function.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Notes:1. A is the average of the distances from z to the points (1,0)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * and (-1,0) in the complex z-plane, and in particular A>=1.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 2. B is in [-1,1], and A*B = x
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Basic relations
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos(conj(z)) = conj(cacos(z))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos(-z) = pi - cacos(z)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos( z) = pi/2 - casin(z)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Special cases (conform to ISO/IEC 9899:1999(E)):
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos(+-0 + i y ) = pi/2 - i y for y is +-0, +-inf, NaN
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos( x + i inf) = pi/2 - i inf for all x
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos( x + i NaN) = NaN + i NaN with invalid for non-zero finite x
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos(-inf + i y ) = pi - i inf for finite +y
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos( inf + i y ) = 0 - i inf for finite +y
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos(-inf + i inf) = 3pi/4- i inf
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos( inf + i inf) = pi/4 - i inf
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos(+-inf+ i NaN) = NaN - i inf (sign of imaginary is unspecified)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos(NaN + i y ) = NaN + i NaN with invalid for finite y
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos(NaN + i inf) = NaN - i inf
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos(NaN + i NaN) = NaN + i NaN
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Special Regions (better formula for accuracy and for avoiding spurious
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * overflow or underflow) (all x and y are assumed nonnegative):
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * case 1: y = 0
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * case 2: tiny y relative to x-1: y <= ulp(0.5)*|x-1|
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * case 3: tiny y: y < 4 sqrt(u), where u = minimum normal number
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * case 4: huge y relative to x+1: y >= (1+x)/ulp(0.5)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * case 5: huge x and y: x and y >= sqrt(M)/8, where M = maximum normal number
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * case 6: tiny x: x < 4 sqrt(u)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * --------
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * case 1 & 2. y=0 or y/|x-1| is tiny. We have
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ____________ _____________
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * / 2 2 / y 2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * / (x+-1) + y = |x+-1| / 1 + (------)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * \/ \/ |x+-1|
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 1 y 2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ~ |x+-1| ( 1 + --- (------) )
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 2 |x+-1|
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * y
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * = |x+-1| + --------.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 2|x+-1|
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Consequently, it is not difficult to see that
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * y
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [ 1 + ------------ , if x < 1,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [ 2(1+x)(1-x)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [ x, if x = 1 (y = 0),
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * A ~= [ 2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [ x * y
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [ x + ------------ ~ x, if x > 1
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [ 2(x+1)(x-1)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * and hence
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ______ 2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * / 2 y y
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * A + \/ A - 1 ~ 1 + ---------------- + -----------, if x < 1,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * sqrt((x+1)(1-x)) 2(x+1)(1-x)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ~ x + sqrt((x-1)*(x+1)), if x >= 1.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * y
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [ x(1 - -----------) ~ x, if x < 1,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [ 2(1+x)(1-x)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * B = x/A ~ [
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [ 1, if x = 1,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [ 2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [ y
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [ 1 - ------------ , if x > 1,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [ 2(x+1)(x-1)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Thus
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [ acos(x) - i y/sqrt((x-1)*(x+1)), if x < 1,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos(x+i*y)~ [ 0 - i 0, if x = 1,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [ y/sqrt(x*x-1) - i log(x+sqrt(x*x-1)), if x > 1.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Note: y/sqrt(x*x-1) ~ y/x when x >= 2**26.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * case 3. y < 4 sqrt(u), where u = minimum normal x.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * After case 1 and 2, this will only occurs when x=1. When x=1, we have
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * A = (sqrt(4+y*y)+y)/2 ~ 1 + y/2 + y^2/8 + ...
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * and
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * B = 1/A = 1 - y/2 + y^2/8 + ...
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Since
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cos(sqrt(y)) ~ 1 - y/2 + ...
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * we have, for the real part,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * acos(B) ~ acos(1 - y/2) ~ sqrt(y)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * For the imaginary part,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * log(A+sqrt(A*A-1)) ~ log(1+y/2+sqrt(2*y/2))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * = log(1+y/2+sqrt(y))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * = (y/2+sqrt(y)) - (y/2+sqrt(y))^2/2 + ...
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ~ sqrt(y) - y*(sqrt(y)+y/2)/2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ~ sqrt(y)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * case 4. y >= (x+1)/ulp(0.5). In this case, A ~ y and B ~ x/y. Thus
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * real part = acos(B) ~ pi/2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * and
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * imag part = log(y+sqrt(y*y-one))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * case 5. Both x and y are large: x and y > sqrt(M)/8, where M = maximum x
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * In this case,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * A ~ sqrt(x*x+y*y)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * B ~ x/sqrt(x*x+y*y).
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Thus
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * real part = acos(B) = atan(y/x),
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * imag part = log(A+sqrt(A*A-1)) ~ log(2A)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * = log(2) + 0.5*log(x*x+y*y)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * = log(2) + log(y) + 0.5*log(1+(x/y)^2)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * case 6. x < 4 sqrt(u). In this case, we have
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * A ~ sqrt(1+y*y), B = x/sqrt(1+y*y).
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Since B is tiny, we have
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * real part = acos(B) ~ pi/2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * imag part = log(A+sqrt(A*A-1)) = log (A+sqrt(y*y))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * = log(y+sqrt(1+y*y))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * = 0.5*log(y^2+2ysqrt(1+y^2)+1+y^2)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * = 0.5*log(1+2y(y+sqrt(1+y^2)));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * = 0.5*log1p(2y(y+A));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos(z) = acos(B) - i sign(y) log (A + sqrt(A*A-1)),
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* INDENT ON */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#include "libm.h"
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#include "complex_wrapper.h"
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* INDENT OFF */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtisstatic const double
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis zero = 0.0,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis one = 1.0,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis E = 1.11022302462515654042e-16, /* 2**-53 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis ln2 = 6.93147180559945286227e-01,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis pi = 3.1415926535897931159979634685,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis pi_l = 1.224646799147353177e-16,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis pi_2 = 1.570796326794896558e+00,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis pi_2_l = 6.123233995736765886e-17,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis pi_4 = 0.78539816339744827899949,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis pi_4_l = 3.061616997868382943e-17,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis pi3_4 = 2.356194490192344836998,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis pi3_4_l = 9.184850993605148829195e-17,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis Foursqrtu = 5.96667258496016539463e-154, /* 2**(-509) */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis Acrossover = 1.5,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis Bcrossover = 0.6417,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis half = 0.5;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* INDENT ON */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtisdcomplex
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtiscacos(dcomplex z) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis double x, y, t, R, S, A, Am1, B, y2, xm1, xp1, Apx;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis int ix, iy, hx, hy;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis unsigned lx, ly;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis dcomplex ans;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis x = D_RE(z);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis y = D_IM(z);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis hx = HI_WORD(x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis lx = LO_WORD(x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis hy = HI_WORD(y);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis ly = LO_WORD(y);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis ix = hx & 0x7fffffff;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis iy = hy & 0x7fffffff;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* x is 0 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if ((ix | lx) == 0) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (((iy | ly) == 0) || (iy >= 0x7ff00000)) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = pi_2;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = -y;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (ans);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* |y| is inf or NaN */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (iy >= 0x7ff00000) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (ISINF(iy, ly)) { /* cacos(x + i inf) = pi/2 - i inf */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = -y;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (ix < 0x7ff00000) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = pi_2 + pi_2_l;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else if (ISINF(ix, lx)) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hx >= 0)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = pi_4 + pi_4_l;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = pi3_4 + pi3_4_l;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = x;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else { /* cacos(x + i NaN) = NaN + i NaN */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = y + x;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (ISINF(ix, lx))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = -fabs(x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = y;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (ans);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis x = fabs(x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis y = fabs(y);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* x is inf or NaN */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (ix >= 0x7ff00000) { /* x is inf or NaN */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (ISINF(ix, lx)) { /* x is INF */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = -x;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (iy >= 0x7ff00000) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (ISINF(iy, ly)) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* INDENT OFF */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* cacos(inf + i inf) = pi/4 - i inf */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* cacos(-inf+ i inf) =3pi/4 - i inf */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* INDENT ON */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hx >= 0)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = pi_4 + pi_4_l;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = pi3_4 + pi3_4_l;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* INDENT OFF */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* cacos(inf + i NaN) = NaN - i inf */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* INDENT ON */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = y + y;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* INDENT OFF */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* cacos(inf + iy ) = 0 - i inf */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* cacos(-inf+ iy ) = pi - i inf */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* INDENT ON */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hx >= 0)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = zero;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = pi + pi_l;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else { /* x is NaN */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* INDENT OFF */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos(NaN + i inf) = NaN - i inf
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos(NaN + i y ) = NaN + i NaN
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * cacos(NaN + i NaN) = NaN + i NaN
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* INDENT ON */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = x + y;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (iy >= 0x7ff00000) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = -y;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = x;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hy < 0)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = -D_IM(ans);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (ans);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if ((iy | ly) == 0) { /* region 1: y=0 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (ix < 0x3ff00000) { /* |x| < 1 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = acos(x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = zero;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = zero;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (ix >= 0x43500000) /* |x| >= 2**54 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = ln2 + log(x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else if (ix >= 0x3ff80000) /* x > Acrossover */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = log(x + sqrt((x - one) * (x +
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis one)));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis xm1 = x - one;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = log1p(xm1 + sqrt(xm1 * (x + one)));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else if (y <= E * fabs(x - one)) { /* region 2: y < tiny*|x-1| */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (ix < 0x3ff00000) { /* x < 1 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = acos(x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = y / sqrt((one + x) * (one - x));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else if (ix >= 0x43500000) { /* |x| >= 2**54 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = y / x;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = ln2 + log(x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis t = sqrt((x - one) * (x + one));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = y / t;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (ix >= 0x3ff80000) /* x > Acrossover */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = log(x + t);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = log1p((x - one) + t);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else if (y < Foursqrtu) { /* region 3 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis t = sqrt(y);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = t;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = t;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else if (E * y - one >= x) { /* region 4 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = pi_2;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = ln2 + log(y);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else if (ix >= 0x5fc00000 || iy >= 0x5fc00000) { /* x,y>2**509 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* region 5: x+1 or y is very large (>= sqrt(max)/8) */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis t = x / y;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = atan(y / x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = ln2 + log(y) + half * log1p(t * t);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else if (x < Foursqrtu) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* region 6: x is very small, < 4sqrt(min) */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = pi_2;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis A = sqrt(one + y * y);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (iy >= 0x3ff80000) /* if y > Acrossover */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = log(y + A);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = half * log1p((y + y) * (y + A));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else { /* safe region */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis y2 = y * y;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis xp1 = x + one;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis xm1 = x - one;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis R = sqrt(xp1 * xp1 + y2);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis S = sqrt(xm1 * xm1 + y2);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis A = half * (R + S);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis B = x / A;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (B <= Bcrossover)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = acos(B);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else { /* use atan and an accurate approx to a-x */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis Apx = A + x;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (x <= one)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = atan(sqrt(half * Apx * (y2 / (R +
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis xp1) + (S - xm1))) / x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = atan((y * sqrt(half * (Apx / (R +
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis xp1) + Apx / (S + xm1)))) / x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (A <= Acrossover) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* use log1p and an accurate approx to A-1 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (x < one)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis Am1 = half * (y2 / (R + xp1) + y2 / (S - xm1));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis Am1 = half * (y2 / (R + xp1) + (S + xm1));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = log1p(Am1 + sqrt(Am1 * (A + one)));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = log(A + sqrt(A * A - one));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hx < 0)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_RE(ans) = pi - D_RE(ans);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hy >= 0)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis D_IM(ans) = -D_IM(ans);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (ans);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis}