File: make_cmath_testcases.py

package info (click to toggle)
jython 2.7.2%2Brepack1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 62,676 kB
  • sloc: python: 640,908; java: 306,458; xml: 1,984; sh: 522; ansic: 126; makefile: 76
file content (134 lines) | stat: -rw-r--r-- 4,523 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# This work is based on test_math.py in the Python test set.
# It a provides a tool to generate additional real test cases.

import math
import mpmath


# Table of additional real test cases. The layout is
#
# function : ( starting_number [ x1, x2, x3, ... ] ),
#
# Where tests will be numbered functionNNNN and each xn
# generates a new test with (complex) argument xn + 0j.

cases_to_generate = {

    'atan' : ( 400, [
                float.fromhex('0x1.fffffffffffffp1023'),
                float.fromhex('-0x1.fffffffffffffp1023'),
                1e-17, -1e-17, 1e-4, -1e-4,
                1 - 1e-15, 1 + 1e-15,
                14.101419947171719, # tan(1.5)
                1255.7655915007896, # tan(1.57)
             ]),

    'cos' : ( 50, [
                1e-150, 1e-18, 1e-9, 0.0003, 0.2, 1.0, 
                -1e-18, -0.0003, -1.0,
                1.0471975511965977, # -> 0.5
                2.5707963267948966,
                -2.5707963267948966,
                18, 18.0
             ]),

    'cosh' : ( 50, [
                1e-150, 1e-18, 1e-9, 0.0003, 0.2, 1.0, 
                -1e-18, -0.0003, -1.0,
                1.3169578969248167086, # -> 2.
                -1.3169578969248167086,
                25*math.log(2), # cosh != exp at 52 bits
                27*math.log(2), # cosh == exp at 52 bits
                709.7827, # not quite overflow
                -709.7827, # not quite overflow
             ]),

    'exp' : ( 70, [
                1e-8, 0.0003, 0.2, 1.0, 
                -1e-8, -0.0003, -1.0,
                2**-52, -2**-53,    # exp != 1 (just)
                2.3025850929940457, # -> 10
                -2.3025850929940457,
                709.7827, # not quite overflow
             ]),

    'sin' : ( 50, [
                1e-100, 3.7e-8, 0.001, 0.2, 1.0, 
                -3.7e-8, -0.001, -1.0, 
                0.5235987755982989, # -> 0.5
                -0.5235987755982989,
                2.617993877991494365,
                -2.617993877991494365,
             ]),

    'sinh' : ( 50, [
                1e-100, 5e-17, 1e-16, 3.7e-8, 0.001, 0.2, 1.0, 
                -3.7e-8, -0.001, -1.0, 
                1.44363547517881034, # -> 2.
                -1.44363547517881034,
                25*math.log(2), # sinh != exp at 52 bits
                27*math.log(2), # sinh == exp at 52 bits
                709.7827, # not quite overflow
                -709.7827, # not quite overflow
             ]),

    'tan' : ( 50, [
                1e-100, 3.7e-8, 0.001, 0.2, 1.0, 
                -3.7e-8, -0.001, -1.0, 
                0.463647609000806116,   # -> 0.5
                -0.463647609000806116,
                1.1071487177940905,     # -> 0.5
                -1.1071487177940905,
                1.5,
                1.57,
                math.pi/2 - 2**-51,
             ]),

    'tanh' : ( 50, [
                1e-100, 5e-17, 1e-16, 3.7e-8, 0.001, 0.2, 1.0, 
                -3.7e-8, -0.001, -1.0, 
                0.54930614433405484, # -> 0.5
                -0.54930614433405484,
                25*math.log(2), # sinh != cosh at 52 bits
                27*math.log(2), # sinh == cosh at 52 bits
                711,            # oveflow cosh in naive impl
                1.797e+308,     # risk overflow
             ]),

    'sqrt' : ( 150, [
                float.fromhex('0x1.fffffffffffffp1023'),
                float.fromhex('0x1.0p-1022'),
                float.fromhex('0x0.0000000000001p-1022'),
             ]),
    }

def generate_cases() :
    fmt = "{}{:04d} {} {!r} 0.0 -> {} {!r}"
    for fn in sorted(cases_to_generate.keys()):
        print "-- Additional real values (Jython)"
        count, xlist = cases_to_generate[fn]
        for x in xlist:
            # Compute the function (in the reference library)
            func = getattr(mpmath, fn)
            y = func(x)
            # For the benefit of cmath tests, get the sign of imaginary zero right
            zero = 0.0
            if math.copysign(1., x) > 0.:
                if fn=='cos' :
                    zero = -0.0
            else :
                if fn=='cosh' :
                    zero = -0.0
            # Output one test case at sufficient precision
            print fmt.format(fn, count, fn, x, mpmath.nstr(y, 20), zero )
            count += 1

def test_main():
    with mpmath.workprec(100):
        generate_cases()

if __name__ == '__main__':
    test_main()

    # Conveniences for interactive use
    from mpmath import mp, mpf, workprec, workdps, nstr