1 /* @(#)k_standard.c 5.1 93/09/24 */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
10 * ====================================================
13 #include <sys/cdefs.h>
14 #if defined(LIBM_SCCS) && !defined(lint)
15 __RCSID("$NetBSD: k_standard.c,v 1.11 2002/05/26 22:01:53 wiz Exp $");
19 #include "math_private.h"
23 #include <stdio.h> /* fputs(), stderr */
24 #define WRITE2(u,v) fputs(u, stderr)
25 #else /* !defined(_USE_WRITE) */
26 #include <unistd.h> /* write */
27 #define WRITE2(u,v) write(2, u, v)
29 #endif /* !defined(_USE_WRITE) */
31 static const double zero
= 0.0; /* used as const */
34 * Standard conformance (non-IEEE) on exception cases.
49 * 14-- lgamma(finite) overflow
50 * 15-- lgamma(-integer)
56 * 21-- pow(x,y) overflow
57 * 22-- pow(x,y) underflow
58 * 23-- pow(0,negative)
59 * 24-- pow(neg,non-integral)
60 * 25-- sinh(finite) overflow
68 * 33-- scalb underflow
69 * 34-- j0(|x|>X_TLOSS)
71 * 36-- j1(|x|>X_TLOSS)
73 * 38-- jn(|x|>X_TLOSS, n)
74 * 39-- yn(x>X_TLOSS, n)
75 * 40-- gamma(finite) overflow
76 * 41-- gamma(-integer)
84 __kernel_standard(double x
, double y
, int type
)
87 #ifndef HUGE_VAL /* this is the only routine that uses HUGE_VAL */
91 SET_HIGH_WORD(inf
,0x7ff00000); /* set inf to infinite */
95 (void) fflush(stdout
);
104 exc
.name
= type
< 100 ? "acos" : "acosf";
106 if (_LIB_VERSION
== _POSIX_
)
108 else if (!matherr(&exc
)) {
109 if(_LIB_VERSION
== _SVID_
) {
110 (void) WRITE2("acos: DOMAIN error\n", 19);
119 exc
.name
= type
< 100 ? "asin" : "asinf";
121 if(_LIB_VERSION
== _POSIX_
)
123 else if (!matherr(&exc
)) {
124 if(_LIB_VERSION
== _SVID_
) {
125 (void) WRITE2("asin: DOMAIN error\n", 19);
136 exc
.name
= type
< 100 ? "atan2" : "atan2f";
138 if(_LIB_VERSION
== _POSIX_
)
140 else if (!matherr(&exc
)) {
141 if(_LIB_VERSION
== _SVID_
) {
142 (void) WRITE2("atan2: DOMAIN error\n", 20);
149 /* hypot(finite,finite) overflow */
151 exc
.name
= type
< 100 ? "hypot" : "hypotf";
152 if (_LIB_VERSION
== _SVID_
)
155 exc
.retval
= HUGE_VAL
;
156 if (_LIB_VERSION
== _POSIX_
)
158 else if (!matherr(&exc
)) {
164 /* cosh(finite) overflow */
166 exc
.name
= type
< 100 ? "cosh" : "coshf";
167 if (_LIB_VERSION
== _SVID_
)
170 exc
.retval
= HUGE_VAL
;
171 if (_LIB_VERSION
== _POSIX_
)
173 else if (!matherr(&exc
)) {
179 /* exp(finite) overflow */
181 exc
.name
= type
< 100 ? "exp" : "expf";
182 if (_LIB_VERSION
== _SVID_
)
185 exc
.retval
= HUGE_VAL
;
186 if (_LIB_VERSION
== _POSIX_
)
188 else if (!matherr(&exc
)) {
194 /* exp(finite) underflow */
195 exc
.type
= UNDERFLOW
;
196 exc
.name
= type
< 100 ? "exp" : "expf";
198 if (_LIB_VERSION
== _POSIX_
)
200 else if (!matherr(&exc
)) {
207 exc
.type
= DOMAIN
; /* should be SING for IEEE */
208 exc
.name
= type
< 100 ? "y0" : "y0f";
209 if (_LIB_VERSION
== _SVID_
)
212 exc
.retval
= -HUGE_VAL
;
213 if (_LIB_VERSION
== _POSIX_
)
215 else if (!matherr(&exc
)) {
216 if (_LIB_VERSION
== _SVID_
) {
217 (void) WRITE2("y0: DOMAIN error\n", 17);
226 exc
.name
= type
< 100 ? "y0" : "y0f";
227 if (_LIB_VERSION
== _SVID_
)
230 exc
.retval
= -HUGE_VAL
;
231 if (_LIB_VERSION
== _POSIX_
)
233 else if (!matherr(&exc
)) {
234 if (_LIB_VERSION
== _SVID_
) {
235 (void) WRITE2("y0: DOMAIN error\n", 17);
243 exc
.type
= DOMAIN
; /* should be SING for IEEE */
244 exc
.name
= type
< 100 ? "y1" : "y1f";
245 if (_LIB_VERSION
== _SVID_
)
248 exc
.retval
= -HUGE_VAL
;
249 if (_LIB_VERSION
== _POSIX_
)
251 else if (!matherr(&exc
)) {
252 if (_LIB_VERSION
== _SVID_
) {
253 (void) WRITE2("y1: DOMAIN error\n", 17);
262 exc
.name
= type
< 100 ? "y1" : "y1f";
263 if (_LIB_VERSION
== _SVID_
)
266 exc
.retval
= -HUGE_VAL
;
267 if (_LIB_VERSION
== _POSIX_
)
269 else if (!matherr(&exc
)) {
270 if (_LIB_VERSION
== _SVID_
) {
271 (void) WRITE2("y1: DOMAIN error\n", 17);
279 exc
.type
= DOMAIN
; /* should be SING for IEEE */
280 exc
.name
= type
< 100 ? "yn" : "ynf";
281 if (_LIB_VERSION
== _SVID_
)
284 exc
.retval
= -HUGE_VAL
;
285 if (_LIB_VERSION
== _POSIX_
)
287 else if (!matherr(&exc
)) {
288 if (_LIB_VERSION
== _SVID_
) {
289 (void) WRITE2("yn: DOMAIN error\n", 17);
298 exc
.name
= type
< 100 ? "yn" : "ynf";
299 if (_LIB_VERSION
== _SVID_
)
302 exc
.retval
= -HUGE_VAL
;
303 if (_LIB_VERSION
== _POSIX_
)
305 else if (!matherr(&exc
)) {
306 if (_LIB_VERSION
== _SVID_
) {
307 (void) WRITE2("yn: DOMAIN error\n", 17);
314 /* lgamma(finite) overflow */
316 exc
.name
= type
< 100 ? "lgamma" : "lgammaf";
317 if (_LIB_VERSION
== _SVID_
)
320 exc
.retval
= HUGE_VAL
;
321 if (_LIB_VERSION
== _POSIX_
)
323 else if (!matherr(&exc
)) {
329 /* lgamma(-integer) or lgamma(0) */
331 exc
.name
= type
< 100 ? "lgamma" : "lgammaf";
332 if (_LIB_VERSION
== _SVID_
)
335 exc
.retval
= HUGE_VAL
;
336 if (_LIB_VERSION
== _POSIX_
)
338 else if (!matherr(&exc
)) {
339 if (_LIB_VERSION
== _SVID_
) {
340 (void) WRITE2("lgamma: SING error\n", 19);
349 exc
.name
= type
< 100 ? "log" : "logf";
350 if (_LIB_VERSION
== _SVID_
)
353 exc
.retval
= -HUGE_VAL
;
354 if (_LIB_VERSION
== _POSIX_
)
356 else if (!matherr(&exc
)) {
357 if (_LIB_VERSION
== _SVID_
) {
358 (void) WRITE2("log: SING error\n", 16);
367 exc
.name
= type
< 100 ? "log" : "logf";
368 if (_LIB_VERSION
== _SVID_
)
371 exc
.retval
= -HUGE_VAL
;
372 if (_LIB_VERSION
== _POSIX_
)
374 else if (!matherr(&exc
)) {
375 if (_LIB_VERSION
== _SVID_
) {
376 (void) WRITE2("log: DOMAIN error\n", 18);
385 exc
.name
= type
< 100 ? "log10" : "log10f";
386 if (_LIB_VERSION
== _SVID_
)
389 exc
.retval
= -HUGE_VAL
;
390 if (_LIB_VERSION
== _POSIX_
)
392 else if (!matherr(&exc
)) {
393 if (_LIB_VERSION
== _SVID_
) {
394 (void) WRITE2("log10: SING error\n", 18);
403 exc
.name
= type
< 100 ? "log10" : "log10f";
404 if (_LIB_VERSION
== _SVID_
)
407 exc
.retval
= -HUGE_VAL
;
408 if (_LIB_VERSION
== _POSIX_
)
410 else if (!matherr(&exc
)) {
411 if (_LIB_VERSION
== _SVID_
) {
412 (void) WRITE2("log10: DOMAIN error\n", 20);
420 /* error only if _LIB_VERSION == _SVID_ */
422 exc
.name
= type
< 100 ? "pow" : "powf";
424 if (_LIB_VERSION
!= _SVID_
) exc
.retval
= 1.0;
425 else if (!matherr(&exc
)) {
426 (void) WRITE2("pow(0,0): DOMAIN error\n", 23);
432 /* pow(x,y) overflow */
434 exc
.name
= type
< 100 ? "pow" : "powf";
435 if (_LIB_VERSION
== _SVID_
) {
438 if(x
<zero
&&rint(y
)!=y
) exc
.retval
= -HUGE
;
440 exc
.retval
= HUGE_VAL
;
442 if(x
<zero
&&rint(y
)!=y
) exc
.retval
= -HUGE_VAL
;
444 if (_LIB_VERSION
== _POSIX_
)
446 else if (!matherr(&exc
)) {
452 /* pow(x,y) underflow */
453 exc
.type
= UNDERFLOW
;
454 exc
.name
= type
< 100 ? "pow" : "powf";
456 if (_LIB_VERSION
== _POSIX_
)
458 else if (!matherr(&exc
)) {
466 exc
.name
= type
< 100 ? "pow" : "powf";
467 if (_LIB_VERSION
== _SVID_
)
470 exc
.retval
= -HUGE_VAL
;
471 if (_LIB_VERSION
== _POSIX_
)
473 else if (!matherr(&exc
)) {
474 if (_LIB_VERSION
== _SVID_
) {
475 (void) WRITE2("pow(0,neg): DOMAIN error\n", 25);
482 /* neg**non-integral */
484 exc
.name
= type
< 100 ? "pow" : "powf";
485 if (_LIB_VERSION
== _SVID_
)
488 exc
.retval
= zero
/zero
; /* X/Open allow NaN */
489 if (_LIB_VERSION
== _POSIX_
)
491 else if (!matherr(&exc
)) {
492 if (_LIB_VERSION
== _SVID_
) {
493 (void) WRITE2("neg**non-integral: DOMAIN error\n", 32);
500 /* sinh(finite) overflow */
502 exc
.name
= type
< 100 ? "sinh" : "sinhf";
503 if (_LIB_VERSION
== _SVID_
)
504 exc
.retval
= ( (x
>zero
) ? HUGE
: -HUGE
);
506 exc
.retval
= ( (x
>zero
) ? HUGE_VAL
: -HUGE_VAL
);
507 if (_LIB_VERSION
== _POSIX_
)
509 else if (!matherr(&exc
)) {
517 exc
.name
= type
< 100 ? "sqrt" : "sqrtf";
518 if (_LIB_VERSION
== _SVID_
)
521 exc
.retval
= zero
/zero
;
522 if (_LIB_VERSION
== _POSIX_
)
524 else if (!matherr(&exc
)) {
525 if (_LIB_VERSION
== _SVID_
) {
526 (void) WRITE2("sqrt: DOMAIN error\n", 19);
535 exc
.name
= type
< 100 ? "fmod" : "fmodf";
536 if (_LIB_VERSION
== _SVID_
)
539 exc
.retval
= zero
/zero
;
540 if (_LIB_VERSION
== _POSIX_
)
542 else if (!matherr(&exc
)) {
543 if (_LIB_VERSION
== _SVID_
) {
544 (void) WRITE2("fmod: DOMAIN error\n", 20);
553 exc
.name
= type
< 100 ? "remainder" : "remainderf";
554 exc
.retval
= zero
/zero
;
555 if (_LIB_VERSION
== _POSIX_
)
557 else if (!matherr(&exc
)) {
558 if (_LIB_VERSION
== _SVID_
) {
559 (void) WRITE2("remainder: DOMAIN error\n", 24);
568 exc
.name
= type
< 100 ? "acosh" : "acoshf";
569 exc
.retval
= zero
/zero
;
570 if (_LIB_VERSION
== _POSIX_
)
572 else if (!matherr(&exc
)) {
573 if (_LIB_VERSION
== _SVID_
) {
574 (void) WRITE2("acosh: DOMAIN error\n", 20);
583 exc
.name
= type
< 100 ? "atanh" : "atanhf";
584 exc
.retval
= zero
/zero
;
585 if (_LIB_VERSION
== _POSIX_
)
587 else if (!matherr(&exc
)) {
588 if (_LIB_VERSION
== _SVID_
) {
589 (void) WRITE2("atanh: DOMAIN error\n", 20);
598 exc
.name
= type
< 100 ? "atanh" : "atanhf";
599 exc
.retval
= x
/zero
; /* sign(x)*inf */
600 if (_LIB_VERSION
== _POSIX_
)
602 else if (!matherr(&exc
)) {
603 if (_LIB_VERSION
== _SVID_
) {
604 (void) WRITE2("atanh: SING error\n", 18);
611 /* scalb overflow; SVID also returns +-HUGE_VAL */
613 exc
.name
= type
< 100 ? "scalb" : "scalbf";
614 exc
.retval
= x
> zero
? HUGE_VAL
: -HUGE_VAL
;
615 if (_LIB_VERSION
== _POSIX_
)
617 else if (!matherr(&exc
)) {
623 /* scalb underflow */
624 exc
.type
= UNDERFLOW
;
625 exc
.name
= type
< 100 ? "scalb" : "scalbf";
626 exc
.retval
= copysign(zero
,x
);
627 if (_LIB_VERSION
== _POSIX_
)
629 else if (!matherr(&exc
)) {
635 /* j0(|x|>X_TLOSS) */
637 exc
.name
= type
< 100 ? "j0" : "j0f";
639 if (_LIB_VERSION
== _POSIX_
)
641 else if (!matherr(&exc
)) {
642 if (_LIB_VERSION
== _SVID_
) {
643 (void) WRITE2(exc
.name
, 2);
644 (void) WRITE2(": TLOSS error\n", 14);
653 exc
.name
= type
< 100 ? "y0" : "y0f";
655 if (_LIB_VERSION
== _POSIX_
)
657 else if (!matherr(&exc
)) {
658 if (_LIB_VERSION
== _SVID_
) {
659 (void) WRITE2(exc
.name
, 2);
660 (void) WRITE2(": TLOSS error\n", 14);
667 /* j1(|x|>X_TLOSS) */
669 exc
.name
= type
< 100 ? "j1" : "j1f";
671 if (_LIB_VERSION
== _POSIX_
)
673 else if (!matherr(&exc
)) {
674 if (_LIB_VERSION
== _SVID_
) {
675 (void) WRITE2(exc
.name
, 2);
676 (void) WRITE2(": TLOSS error\n", 14);
685 exc
.name
= type
< 100 ? "y1" : "y1f";
687 if (_LIB_VERSION
== _POSIX_
)
689 else if (!matherr(&exc
)) {
690 if (_LIB_VERSION
== _SVID_
) {
691 (void) WRITE2(exc
.name
, 2);
692 (void) WRITE2(": TLOSS error\n", 14);
699 /* jn(|x|>X_TLOSS) */
701 exc
.name
= type
< 100 ? "jn" : "jnf";
703 if (_LIB_VERSION
== _POSIX_
)
705 else if (!matherr(&exc
)) {
706 if (_LIB_VERSION
== _SVID_
) {
707 (void) WRITE2(exc
.name
, 2);
708 (void) WRITE2(": TLOSS error\n", 14);
717 exc
.name
= type
< 100 ? "yn" : "ynf";
719 if (_LIB_VERSION
== _POSIX_
)
721 else if (!matherr(&exc
)) {
722 if (_LIB_VERSION
== _SVID_
) {
723 (void) WRITE2(exc
.name
, 2);
724 (void) WRITE2(": TLOSS error\n", 14);
731 /* gamma(finite) overflow */
733 exc
.name
= type
< 100 ? "gamma" : "gammaf";
734 if (_LIB_VERSION
== _SVID_
)
737 exc
.retval
= HUGE_VAL
;
738 if (_LIB_VERSION
== _POSIX_
)
740 else if (!matherr(&exc
)) {
746 /* gamma(-integer) or gamma(0) */
748 exc
.name
= type
< 100 ? "gamma" : "gammaf";
749 if (_LIB_VERSION
== _SVID_
)
752 exc
.retval
= HUGE_VAL
;
753 if (_LIB_VERSION
== _POSIX_
)
755 else if (!matherr(&exc
)) {
756 if (_LIB_VERSION
== _SVID_
) {
757 (void) WRITE2("gamma: SING error\n", 18);
765 /* error only if _LIB_VERSION == _SVID_ & _XOPEN_ */
767 exc
.name
= type
< 100 ? "pow" : "powf";
769 if (_LIB_VERSION
== _IEEE_
||
770 _LIB_VERSION
== _POSIX_
) exc
.retval
= 1.0;
771 else if (!matherr(&exc
)) {
779 exc
.name
= type
< 100 ? "log2" : "log2f";
780 if (_LIB_VERSION
== _SVID_
)
783 exc
.retval
= -HUGE_VAL
;
784 if (_LIB_VERSION
== _POSIX_
)
786 else if (!matherr(&exc
)) {
787 if (_LIB_VERSION
== _SVID_
) {
788 (void) WRITE2("log2: SING error\n", 18);
797 exc
.name
= type
< 100 ? "log2" : "log2f";
798 if (_LIB_VERSION
== _SVID_
)
801 exc
.retval
= -HUGE_VAL
;
802 if (_LIB_VERSION
== _POSIX_
)
804 else if (!matherr(&exc
)) {
805 if (_LIB_VERSION
== _SVID_
) {
806 (void) WRITE2("log2: DOMAIN error\n", 20);