File: get_fmpz.c

package info (click to toggle)
flint-arb 1%3A2.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,672 kB
  • sloc: ansic: 204,753; sh: 570; makefile: 287; python: 268
file content (111 lines) | stat: -rw-r--r-- 2,962 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
    Copyright (C) 2012 Fredrik Johansson

    This file is part of Arb.

    Arb is free software: you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.  See <http://www.gnu.org/licenses/>.
*/

#include "fmpr.h"

void
fmpr_get_fmpz(fmpz_t z, const fmpr_t x, fmpr_rnd_t rnd)
{
    slong exp;

    if (fmpr_is_special(x))
    {
        if (fmpr_is_zero(x))
        {
            fmpz_zero(z);
        }
        else
        {
            flint_printf("fmpr_get_fmpz: cannot convert infinity or nan to integer\n");
            flint_abort();
        }
    }

    if (!COEFF_IS_MPZ(*fmpr_expref(x)))
    {
        exp = *fmpr_expref(x);
    }
    else
    {
        /* tiny */
        if (fmpz_sgn(fmpr_expref(x)) < 0)
        {
            int sign = fmpz_sgn(fmpr_manref(x));

            if (rnd == FMPR_RND_NEAR
                || rnd == FMPR_RND_DOWN
                || (rnd == FMPR_RND_FLOOR && sign > 0)
                || (rnd == FMPR_RND_CEIL && sign < 0))
                fmpz_zero(z);
            else
                fmpz_set_si(z, sign);
            return;
        }
        else
        {
            flint_printf("fmpr_get_fmpz: number too large to convert to integer\n");
            flint_abort();
            return; /* dummy return because flint_abort() is not declared noreturn */
        }
    }

    if (exp >= 0)
    {
        fmpz_mul_2exp(z, fmpr_manref(x), exp);
    }
    else
    {
        exp = -exp;

        if (rnd == FMPR_RND_NEAR)
        {
            int sign = fmpz_sgn(fmpr_manref(x));

            /* half-integer -> tie, so round to even */
            if (exp == 1)
            {
                fmpz_tdiv_q_2exp(z, fmpr_manref(x), 1);

                if (fmpz_is_odd(z))
                {
                    if (sign > 0)
                        fmpz_add_ui(z, z, 1);
                    else
                        fmpz_sub_ui(z, z, 1);
                }
            }
            else if (fmpz_bits(fmpr_manref(x)) < exp)  /* < 0.5 */
            {
                fmpz_zero(z);
            }
            else
            {
                /* add 1/2 and round to floor */
                fmpz_t t;
                fmpz_init(t);
                fmpz_one(t);
                fmpz_mul_2exp(t, t, exp - 1);
                fmpz_add(t, t, fmpr_manref(x));
                fmpz_fdiv_q_2exp(z, t, exp);
                fmpz_clear(t);
            }
        }
        else if (rnd == FMPR_RND_DOWN)
            fmpz_tdiv_q_2exp(z, fmpr_manref(x), exp);
        else if (rnd == FMPR_RND_UP)
            fmpz_adiv_q_2exp(z, fmpr_manref(x), exp);
        else if (rnd == FMPR_RND_FLOOR)
            fmpz_fdiv_q_2exp(z, fmpr_manref(x), exp);
        else if (rnd == FMPR_RND_CEIL)
            fmpz_cdiv_q_2exp(z, fmpr_manref(x), exp);
    }
}