Annotation of OpenXM_contrib/gmp/tests/mpf/t-trunc.c, Revision 1.1
1.1 ! ohara 1: /* Test mpf_trunc, mpf_ceil, mpf_floor.
! 2:
! 3: Copyright 2001, 2002 Free Software Foundation, Inc.
! 4:
! 5: This file is part of the GNU MP Library.
! 6:
! 7: The GNU MP Library is free software; you can redistribute it and/or modify
! 8: it under the terms of the GNU Lesser General Public License as published by
! 9: the Free Software Foundation; either version 2.1 of the License, or (at your
! 10: option) any later version.
! 11:
! 12: The GNU MP Library is distributed in the hope that it will be useful, but
! 13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
! 14: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
! 15: License for more details.
! 16:
! 17: You should have received a copy of the GNU Lesser General Public License
! 18: along with the GNU MP Library; see the file COPYING.LIB. If not, write to
! 19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
! 20: MA 02111-1307, USA. */
! 21:
! 22: #include <stdio.h>
! 23: #include <stdlib.h>
! 24: #include "gmp.h"
! 25: #include "gmp-impl.h"
! 26: #include "tests.h"
! 27:
! 28:
! 29: void
! 30: check_print (mpf_srcptr src, mpf_srcptr got, mpf_srcptr want)
! 31: {
! 32: mp_trace_base = 16;
! 33: mpf_trace ("src ", src);
! 34: mpf_trace ("got ", got);
! 35: mpf_trace ("want", want);
! 36:
! 37: printf ("got size=%d exp=%ld\n", SIZ(got), EXP(got));
! 38: mpn_trace (" limbs=", PTR(got), ABSIZ(got));
! 39:
! 40: printf ("want size=%d exp=%ld\n", SIZ(want), EXP(want));
! 41: mpn_trace (" limbs=", PTR(want), ABSIZ(want));
! 42: }
! 43:
! 44: void
! 45: check_one (mpf_srcptr src, mpf_srcptr trunc, mpf_srcptr ceil, mpf_srcptr floor)
! 46: {
! 47: mpf_t got;
! 48:
! 49: mpf_init2 (got, mpf_get_prec (trunc));
! 50: ASSERT_ALWAYS (PREC(got) == PREC(trunc));
! 51: ASSERT_ALWAYS (PREC(got) == PREC(ceil));
! 52: ASSERT_ALWAYS (PREC(got) == PREC(floor));
! 53:
! 54: #define CHECK_SEP(name, fun, want) \
! 55: mpf_set_ui (got, 54321L); /* initial junk */ \
! 56: fun (got, src); \
! 57: MPF_CHECK_FORMAT (got); \
! 58: if (mpf_cmp (got, want) != 0) \
! 59: { \
! 60: printf ("%s wrong\n", name); \
! 61: check_print (src, got, want); \
! 62: abort (); \
! 63: }
! 64:
! 65: CHECK_SEP ("mpf_trunc", mpf_trunc, trunc);
! 66: CHECK_SEP ("mpf_ceil", mpf_ceil, ceil);
! 67: CHECK_SEP ("mpf_floor", mpf_floor, floor);
! 68:
! 69: #define CHECK_INPLACE(name, fun, want) \
! 70: mpf_set (got, src); \
! 71: fun (got, got); \
! 72: MPF_CHECK_FORMAT (got); \
! 73: if (mpf_cmp (got, want) != 0) \
! 74: { \
! 75: printf ("%s wrong\n", name); \
! 76: check_print (src, got, want); \
! 77: abort (); \
! 78: }
! 79:
! 80: CHECK_INPLACE ("mpf_trunc", mpf_trunc, trunc);
! 81:
! 82: /* Can't do these unconditionally in case truncation by mpf_set strips
! 83: some low non-zero limbs which would have rounded the result. */
! 84: if (ABSIZ(src) <= PREC(trunc)+1)
! 85: {
! 86: CHECK_INPLACE ("mpf_ceil", mpf_ceil, ceil);
! 87: CHECK_INPLACE ("mpf_floor", mpf_floor, floor);
! 88: }
! 89:
! 90: mpf_clear (got);
! 91: }
! 92:
! 93: void
! 94: check_all (mpf_ptr src, mpf_ptr trunc, mpf_ptr ceil, mpf_ptr floor)
! 95: {
! 96: /* some of these values are generated with direct field assignments */
! 97: MPF_CHECK_FORMAT (src);
! 98: MPF_CHECK_FORMAT (trunc);
! 99: MPF_CHECK_FORMAT (ceil);
! 100: MPF_CHECK_FORMAT (floor);
! 101:
! 102: check_one (src, trunc, ceil, floor);
! 103:
! 104: mpf_neg (src, src);
! 105: mpf_neg (trunc, trunc);
! 106: mpf_neg (ceil, ceil);
! 107: mpf_neg (floor, floor);
! 108: check_one (src, trunc, floor, ceil);
! 109: }
! 110:
! 111: void
! 112: check_various (void)
! 113: {
! 114: mpf_t src, trunc, ceil, floor;
! 115: int n, i;
! 116:
! 117: mpf_init2 (src, 512L);
! 118: mpf_init2 (trunc, 256L);
! 119: mpf_init2 (ceil, 256L);
! 120: mpf_init2 (floor, 256L);
! 121:
! 122: /* 0 */
! 123: mpf_set_ui (src, 0L);
! 124: mpf_set_ui (trunc, 0L);
! 125: mpf_set_ui (ceil, 0L);
! 126: mpf_set_ui (floor, 0L);
! 127: check_all (src, trunc, ceil, floor);
! 128:
! 129: /* 1 */
! 130: mpf_set_ui (src, 1L);
! 131: mpf_set_ui (trunc, 1L);
! 132: mpf_set_ui (ceil, 1L);
! 133: mpf_set_ui (floor, 1L);
! 134: check_all (src, trunc, ceil, floor);
! 135:
! 136: /* 2^1024 */
! 137: mpf_set_ui (src, 1L);
! 138: mpf_mul_2exp (src, src, 1024L);
! 139: mpf_set (trunc, src);
! 140: mpf_set (ceil, src);
! 141: mpf_set (floor, src);
! 142: check_all (src, trunc, ceil, floor);
! 143:
! 144: /* 1/2^1024, fraction only */
! 145: mpf_set_ui (src, 1L);
! 146: mpf_div_2exp (src, src, 1024L);
! 147: mpf_set_si (trunc, 0L);
! 148: mpf_set_si (ceil, 1L);
! 149: mpf_set_si (floor, 0L);
! 150: check_all (src, trunc, ceil, floor);
! 151:
! 152: /* 1/2 */
! 153: mpf_set_ui (src, 1L);
! 154: mpf_div_2exp (src, src, 1L);
! 155: mpf_set_si (trunc, 0L);
! 156: mpf_set_si (ceil, 1L);
! 157: mpf_set_si (floor, 0L);
! 158: check_all (src, trunc, ceil, floor);
! 159:
! 160: /* 123+1/2^64 */
! 161: mpf_set_ui (src, 1L);
! 162: mpf_div_2exp (src, src, 64L);
! 163: mpf_add_ui (src, src, 123L);
! 164: mpf_set_si (trunc, 123L);
! 165: mpf_set_si (ceil, 124L);
! 166: mpf_set_si (floor, 123L);
! 167: check_all (src, trunc, ceil, floor);
! 168:
! 169: /* integer of full prec+1 limbs, unchanged */
! 170: n = PREC(trunc)+1;
! 171: ASSERT_ALWAYS (n <= PREC(src)+1);
! 172: EXP(src) = n;
! 173: SIZ(src) = n;
! 174: for (i = 0; i < SIZ(src); i++)
! 175: PTR(src)[i] = i+100;
! 176: mpf_set (trunc, src);
! 177: mpf_set (ceil, src);
! 178: mpf_set (floor, src);
! 179: check_all (src, trunc, ceil, floor);
! 180:
! 181: /* full prec+1 limbs, 1 trimmed for integer */
! 182: n = PREC(trunc)+1;
! 183: ASSERT_ALWAYS (n <= PREC(src)+1);
! 184: EXP(src) = n-1;
! 185: SIZ(src) = n;
! 186: for (i = 0; i < SIZ(src); i++)
! 187: PTR(src)[i] = i+200;
! 188: EXP(trunc) = n-1;
! 189: SIZ(trunc) = n-1;
! 190: for (i = 0; i < SIZ(trunc); i++)
! 191: PTR(trunc)[i] = i+201;
! 192: mpf_set (floor, trunc);
! 193: mpf_add_ui (ceil, trunc, 1L);
! 194: check_all (src, trunc, ceil, floor);
! 195:
! 196: /* prec+3 limbs, 2 trimmed for size */
! 197: n = PREC(trunc)+3;
! 198: ASSERT_ALWAYS (n <= PREC(src)+1);
! 199: EXP(src) = n;
! 200: SIZ(src) = n;
! 201: for (i = 0; i < SIZ(src); i++)
! 202: PTR(src)[i] = i+300;
! 203: EXP(trunc) = n;
! 204: SIZ(trunc) = n-2;
! 205: for (i = 0; i < SIZ(trunc); i++)
! 206: PTR(trunc)[i] = i+302;
! 207: mpf_set (floor, trunc);
! 208: mpf_set (ceil, trunc);
! 209: PTR(ceil)[0]++;
! 210: check_all (src, trunc, ceil, floor);
! 211:
! 212: /* prec+4 limbs, 2 trimmed for size, 1 trimmed for integer */
! 213: n = PREC(trunc)+4;
! 214: ASSERT_ALWAYS (n <= PREC(src)+1);
! 215: EXP(src) = n-1;
! 216: SIZ(src) = n;
! 217: for (i = 0; i < SIZ(src); i++)
! 218: PTR(src)[i] = i+400;
! 219: EXP(trunc) = n-1;
! 220: SIZ(trunc) = n-3;
! 221: for (i = 0; i < SIZ(trunc); i++)
! 222: PTR(trunc)[i] = i+403;
! 223: mpf_set (floor, trunc);
! 224: mpf_set (ceil, trunc);
! 225: PTR(ceil)[0]++;
! 226: check_all (src, trunc, ceil, floor);
! 227:
! 228: /* F.F, carry out of ceil */
! 229: EXP(src) = 1;
! 230: SIZ(src) = 2;
! 231: PTR(src)[0] = GMP_NUMB_MAX;
! 232: PTR(src)[1] = GMP_NUMB_MAX;
! 233: EXP(trunc) = 1;
! 234: SIZ(trunc) = 1;
! 235: PTR(trunc)[0] = GMP_NUMB_MAX;
! 236: mpf_set (floor, trunc);
! 237: EXP(ceil) = 2;
! 238: SIZ(ceil) = 1;
! 239: PTR(ceil)[0] = 1;
! 240: check_all (src, trunc, ceil, floor);
! 241:
! 242: /* FF.F, carry out of ceil */
! 243: EXP(src) = 2;
! 244: SIZ(src) = 3;
! 245: PTR(src)[0] = GMP_NUMB_MAX;
! 246: PTR(src)[1] = GMP_NUMB_MAX;
! 247: PTR(src)[2] = GMP_NUMB_MAX;
! 248: EXP(trunc) = 2;
! 249: SIZ(trunc) = 2;
! 250: PTR(trunc)[0] = GMP_NUMB_MAX;
! 251: PTR(trunc)[1] = GMP_NUMB_MAX;
! 252: mpf_set (floor, trunc);
! 253: EXP(ceil) = 3;
! 254: SIZ(ceil) = 1;
! 255: PTR(ceil)[0] = 1;
! 256: check_all (src, trunc, ceil, floor);
! 257:
! 258: mpf_clear (src);
! 259: mpf_clear (trunc);
! 260: mpf_clear (ceil);
! 261: mpf_clear (floor);
! 262: }
! 263:
! 264: int
! 265: main (void)
! 266: {
! 267: tests_start ();
! 268:
! 269: check_various ();
! 270:
! 271: tests_end ();
! 272: exit (0);
! 273: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>