File: numerics.out

package info (click to toggle)
postgresql-pllua 1%3A2.0.10-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,316 kB
  • sloc: ansic: 14,369; sql: 2,181; makefile: 163; sh: 59; javascript: 38
file content (146 lines) | stat: -rw-r--r-- 6,677 bytes parent folder | download | duplicates (4)
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
135
136
137
138
139
140
141
142
143
144
145
146
--
\set VERBOSITY terse
-- test numerics
create function lua_numexec(code text, n1 numeric, n2 numeric)
  returns text
  language pllua
  as $$
    local f,e = load("return function(n1,n2) return "..code.." end", code, "t", self)
    assert(f,e)
    f = f()
    assert(f)
    return tostring(f(n1,n2))
  end
  do
    num = require "pllua.numeric"
$$;
create function pg_numexec(code text, n1 numeric, n2 numeric)
  returns text
  language plpgsql
  as $$
    declare
      r text;
    begin
      execute format('select (%s)::text',
      	      	     regexp_replace(regexp_replace(code, '\mnum\.', '', 'g'),
		                    '\mn([0-9])', '$\1', 'g'))
	 into r using n1,n2;
      return r;
    end;
$$;
with
  t as (select code,
               lua_numexec(code, 5439.123456, -1.9) as lua,
               pg_numexec(code, 5439.123456, -1.9) as pg
          from unnest(array[
		$$ n1 + n2 $$,		$$ n1 - n2 $$,
		$$ n1 * n2 $$,		$$ n1 / n2 $$,
		$$ n1 % n2 $$,		$$ n1 ^ n2 $$,
		$$ (-n1) + n2 $$,	$$ (-n1) - n2 $$,
		$$ (-n1) * n2 $$,	$$ (-n1) / n2 $$,
		$$ (-n1) % n2 $$,	$$ (-n1) ^ 3 $$,
		$$ (-n1) + (-n2) $$,	$$ (-n1) - (-n2) $$,
		$$ (-n1) * (-n2) $$,    $$ (-n1) / (-n2) $$,
		$$ (-n1) % (-n2) $$,	$$ (-n1) ^ (-3) $$,
		$$ (n1) > (n2) $$,	$$ (n1) < (n2) $$,
		$$ (n1) >= (n2) $$,	$$ (n1) <= (n2) $$,
		$$ (n1) > (n2*10000) $$,
		$$ (n1) < (n2*10000) $$,
		$$ (n1) >= (n2 * -10000) $$,
		$$ (n1) <= (n2 * -10000) $$,
		$$ num.round(n1) $$,    $$ num.round(n2) $$,
		$$ num.round(n1,4) $$,	$$ num.round(n1,-1) $$,
		$$ num.trunc(n1) $$,	$$ num.trunc(n2) $$,
		$$ num.trunc(n1,4) $$,	$$ num.trunc(n1,-1) $$,
		$$ num.floor(n1) $$,	$$ num.floor(n2) $$,
		$$ num.ceil(n1) $$,	$$ num.ceil(n2) $$,
		$$ num.abs(n1) $$,	$$ num.abs(n2) $$,
		$$ num.sign(n1) $$,	$$ num.sign(n2) $$,
		$$ num.sqrt(n1) $$,
		$$ num.exp(12.3) $$,
		$$ num.exp(n2) $$
  ]) as u(code))
select (lua = pg) as ok, * from t;
 ok |          code           |              lua               |               pg               
----+-------------------------+--------------------------------+--------------------------------
 t  |  n1 + n2                | 5437.223456                    | 5437.223456
 t  |  n1 - n2                | 5441.023456                    | 5441.023456
 t  |  n1 * n2                | -10334.3345664                 | -10334.3345664
 t  |  n1 / n2                | -2862.6965557894736842         | -2862.6965557894736842
 t  |  n1 % n2                | 1.323456                       | 1.323456
 t  |  n1 ^ n2                | 0.00000007989048519637487      | 0.00000007989048519637487
 t  |  (-n1) + n2             | -5441.023456                   | -5441.023456
 t  |  (-n1) - n2             | -5437.223456                   | -5437.223456
 t  |  (-n1) * n2             | 10334.3345664                  | 10334.3345664
 t  |  (-n1) / n2             | 2862.6965557894736842          | 2862.6965557894736842
 t  |  (-n1) % n2             | -1.323456                      | -1.323456
 t  |  (-n1) ^ 3              | -160911376260.9068713240072028 | -160911376260.9068713240072028
 t  |  (-n1) + (-n2)          | -5437.223456                   | -5437.223456
 t  |  (-n1) - (-n2)          | -5441.023456                   | -5441.023456
 t  |  (-n1) * (-n2)          | -10334.3345664                 | -10334.3345664
 t  |  (-n1) / (-n2)          | -2862.6965557894736842         | -2862.6965557894736842
 t  |  (-n1) % (-n2)          | -1.323456                      | -1.323456
 t  |  (-n1) ^ (-3)           | -0.0000000000062146            | -0.0000000000062146
 t  |  (n1) > (n2)            | true                           | true
 t  |  (n1) < (n2)            | false                          | false
 t  |  (n1) >= (n2)           | true                           | true
 t  |  (n1) <= (n2)           | false                          | false
 t  |  (n1) > (n2*10000)      | true                           | true
 t  |  (n1) < (n2*10000)      | false                          | false
 t  |  (n1) >= (n2 * -10000)  | false                          | false
 t  |  (n1) <= (n2 * -10000)  | true                           | true
 t  |  num.round(n1)          | 5439                           | 5439
 t  |  num.round(n2)          | -2                             | -2
 t  |  num.round(n1,4)        | 5439.1235                      | 5439.1235
 t  |  num.round(n1,-1)       | 5440                           | 5440
 t  |  num.trunc(n1)          | 5439                           | 5439
 t  |  num.trunc(n2)          | -1                             | -1
 t  |  num.trunc(n1,4)        | 5439.1234                      | 5439.1234
 t  |  num.trunc(n1,-1)       | 5430                           | 5430
 t  |  num.floor(n1)          | 5439                           | 5439
 t  |  num.floor(n2)          | -2                             | -2
 t  |  num.ceil(n1)           | 5440                           | 5440
 t  |  num.ceil(n2)           | -1                             | -1
 t  |  num.abs(n1)            | 5439.123456                    | 5439.123456
 t  |  num.abs(n2)            | 1.9                            | 1.9
 t  |  num.sign(n1)           | 1                              | 1
 t  |  num.sign(n2)           | -1                             | -1
 t  |  num.sqrt(n1)           | 73.750413259859093             | 73.750413259859093
 t  |  num.exp(12.3)          | 219695.98867213773             | 219695.98867213773
 t  |  num.exp(n2)            | 0.1495686192226351             | 0.1495686192226351
(45 rows)

-- calculate pi to 40 places
do language pllua $$
  -- Chudnovsky formula; ~14 digits per round, we use 4 rounds
  local num = require 'pllua.numeric'
  local prec = 100  -- precision of intermediate values
  local function fact(n)
    local r = pgtype.numeric(1):round(prec)
    for i = 2,n do r = r * i end
    return r:round(prec)
  end
  local c640320 = pgtype.numeric(640320):round(prec)
  local c13591409 = pgtype.numeric(13591409):round(prec)
  local c545140134 = pgtype.numeric(545140134):round(prec)
  local function chn(k)
    return (fact(6*k) * (c13591409 + (c545140134 * k)))
           / (fact(3*k) * fact(k)^3 * (-c640320)^(3*k))
  end
  local function pi()
    return (1 / ((chn(0) + chn(1) + chn(2) + chn(3))*12 / num.sqrt(c640320^3))):round(40)
  end
  print(pi())
$$;
INFO:  3.1415926535897932384626433832795028841972
-- check sanity of maxinteger/mininteger
do language pllua $$
  local num = require 'pllua.numeric'
  local maxi = num.maxinteger
  local mini = num.mininteger
  print(type(num.tointeger(maxi)), type(num.tointeger(maxi+1)))
  print(type(num.tointeger(mini)), type(num.tointeger(mini-1)))
$$
--end
INFO:  number	nil
INFO:  number	nil