1 /* $OpenBSD: ldexp_test.c,v 1.1 2017/10/15 12:15:30 visa Exp $ */ 2 3 /* 4 * Copyright (c) 2017 Visa Hankala 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <assert.h> 20 #include <math.h> 21 22 int 23 main(int argc, char *argv[]) 24 { 25 double f; 26 27 /* 28 * Test that the result has the correct sign. 29 * Assumes IEEE 754 double-precision arithmetics. 30 */ 31 32 assert(ldexp(1.0, -1022) > 0.0); /* IEEE 754 minimum normal positive */ 33 assert(ldexp(1.0, -1023) > 0.0); /* subnormal positive */ 34 assert(ldexp(1.0, -1024) > 0.0); /* subnormal positive */ 35 assert(ldexp(1.0, -1074) > 0.0); /* minimum subnormal positive */ 36 assert(ldexp(1.0, -1075) >= 0.0); /* zero */ 37 assert(ldexp(ldexp(1.0, -1022), -53) >= 0.0); /* zero */ 38 39 assert(ldexp(1.0, 1023) > 0.0); /* normal positive */ 40 41 f = ldexp(1.0, 1024); /* infinite positive */ 42 assert(isinf(f)); 43 assert(!signbit(f)); 44 45 f = ldexp(ldexp(1.0, 1023), 1); /* infinite positive */ 46 assert(isinf(f)); 47 assert(!signbit(f)); 48 49 assert(ldexp(-1.0, -1022) < 0.0); /* IEEE 754 maximum normal negative */ 50 assert(ldexp(-1.0, -1023) < 0.0); /* subnormal negative */ 51 assert(ldexp(-1.0, -1024) < 0.0); /* subnormal negative */ 52 assert(ldexp(-1.0, -1074) < 0.0); /* maximum subnormal negative */ 53 assert(ldexp(-1.0, -1075) <= 0.0); /* zero */ 54 assert(ldexp(ldexp(-1.0, -1022), -53) <= 0.0); /* zero */ 55 56 assert(ldexp(-1.0, 1023) < 0.0); /* normal negative */ 57 58 f = ldexp(-1.0, 1024); /* infinite negative */ 59 assert(isinf(f)); 60 assert(signbit(f)); 61 62 f = ldexp(ldexp(-1.0, 1023), 1); /* infinite negative */ 63 assert(isinf(f)); 64 assert(signbit(f)); 65 66 f = ldexp(NAN, 0); 67 assert(isnan(f)); 68 assert(!signbit(f)); 69 70 f = ldexp(-NAN, 0); 71 assert(isnan(f)); 72 assert(signbit(f)); 73 74 return 0; 75 } 76