-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdecimal.html
183 lines (170 loc) · 9.32 KB
/
decimal.html
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>6.8. Decimal Functions and Operators — Presto 0.202 Documentation</title>
<link rel="stylesheet" href="../_static/presto.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '0.202',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="top" title="Presto 0.202 Documentation" href="../index.html" />
<link rel="up" title="6. Functions and Operators" href="../functions.html" />
<link rel="next" title="6.9. String Functions and Operators" href="string.html" />
<link rel="prev" title="6.7. Bitwise Functions" href="bitwise.html" />
</head>
<body role="document">
<div class="header">
<h1 class="heading"><a href="../index.html">
<span>Presto 0.202 Documentation</span></a></h1>
<h2 class="heading"><span>6.8. Decimal Functions and Operators</span></h2>
</div>
<div class="topnav">
<p class="nav">
<span class="left">
« <a href="bitwise.html">6.7. Bitwise Functions</a>
</span>
<span class="right">
<a href="string.html">6.9. String Functions and Operators</a> »
</span>
</p>
</div>
<div class="content">
<div class="section" id="decimal-functions-and-operators">
<h1>6.8. Decimal Functions and Operators</h1>
<div class="section" id="decimal-literals">
<h2>Decimal Literals</h2>
<blockquote>
<div><p>Use <code class="docutils literal"><span class="pre">DECIMAL</span> <span class="pre">'xxxxxxx.yyyyyyy'</span></code> syntax to define literal of DECIMAL type.</p>
<p>The precision of DECIMAL type for literal will be equal to number of digits
in literal (including trailing and leading zeros).
The scale will be equal to number of digits in fractional part (including trailing zeros).</p>
</div></blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="60%" />
<col width="40%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Example literal</th>
<th class="head">Data type</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal"><span class="pre">DECIMAL</span> <span class="pre">'0'</span></code></td>
<td><code class="docutils literal"><span class="pre">DECIMAL(1)</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">DECIMAL</span> <span class="pre">'12345'</span></code></td>
<td><code class="docutils literal"><span class="pre">DECIMAL(5)</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">DECIMAL</span> <span class="pre">'0000012345.1234500000'</span></code></td>
<td><code class="docutils literal"><span class="pre">DECIMAL(20,</span> <span class="pre">10)</span></code></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="binary-arithmetic-decimal-operators">
<h2>Binary Arithmetic Decimal Operators</h2>
<blockquote>
<div>Standard mathematical operators are supported. The table below explains
precision and scale calculation rules for result.
Assuming <code class="docutils literal"><span class="pre">x</span></code> is of type <code class="docutils literal"><span class="pre">DECIMAL(xp,</span> <span class="pre">xs)</span></code> and <code class="docutils literal"><span class="pre">y</span></code> is of type <code class="docutils literal"><span class="pre">DECIMAL(yp,</span> <span class="pre">ys)</span></code>.</div></blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="18%" />
<col width="41%" />
<col width="41%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td>Operation</td>
<td>Result type precision</td>
<td>Result type scale</td>
</tr>
<tr class="row-even"><td><p class="first"><code class="docutils literal"><span class="pre">x</span> <span class="pre">+</span> <span class="pre">y</span></code></p>
<p>and</p>
<p class="last"><code class="docutils literal"><span class="pre">x</span> <span class="pre">-</span> <span class="pre">y</span></code></p>
</td>
<td><div class="first last highlight-sql"><div class="highlight"><pre><span></span><span class="k">min</span><span class="p">(</span><span class="mi">38</span><span class="p">,</span>
<span class="mi">1</span> <span class="o">+</span>
<span class="k">min</span><span class="p">(</span><span class="n">xs</span><span class="p">,</span> <span class="n">ys</span><span class="p">)</span> <span class="o">+</span>
<span class="k">min</span><span class="p">(</span><span class="n">xp</span> <span class="o">-</span> <span class="n">xs</span><span class="p">,</span> <span class="n">yp</span> <span class="o">-</span> <span class="n">ys</span><span class="p">)</span>
<span class="p">)</span>
</pre></div>
</div>
</td>
<td><code class="docutils literal"><span class="pre">max(xs,</span> <span class="pre">ys)</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre">*</span> <span class="pre">y</span></code></td>
<td><code class="docutils literal"><span class="pre">min(38,</span> <span class="pre">xp</span> <span class="pre">+</span> <span class="pre">yp)</span></code></td>
<td><code class="docutils literal"><span class="pre">xs</span> <span class="pre">+</span> <span class="pre">ys</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre">/</span> <span class="pre">y</span></code></td>
<td><div class="first last highlight-sql"><div class="highlight"><pre><span></span><span class="k">min</span><span class="p">(</span><span class="mi">38</span><span class="p">,</span>
<span class="n">xp</span> <span class="o">+</span> <span class="n">ys</span>
<span class="o">+</span> <span class="k">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">ys</span><span class="o">-</span><span class="n">xs</span><span class="p">)</span>
<span class="p">)</span>
</pre></div>
</div>
</td>
<td><code class="docutils literal"><span class="pre">max(xs,</span> <span class="pre">ys)</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre">%</span> <span class="pre">y</span></code></td>
<td><div class="first last highlight-sql"><div class="highlight"><pre><span></span><span class="k">min</span><span class="p">(</span><span class="n">xp</span> <span class="o">-</span> <span class="n">xs</span><span class="p">,</span> <span class="n">yp</span> <span class="o">-</span> <span class="n">ys</span><span class="p">)</span> <span class="o">+</span>
<span class="k">max</span><span class="p">(</span><span class="n">xs</span><span class="p">,</span> <span class="n">bs</span><span class="p">)</span>
</pre></div>
</div>
</td>
<td><code class="docutils literal"><span class="pre">max(xs,</span> <span class="pre">ys)</span></code></td>
</tr>
</tbody>
</table>
<blockquote>
<div><p>If the mathematical result of the operation is not exactly representable with
the precision and scale of the result data type,
then an exception condition is raised - <code class="docutils literal"><span class="pre">Value</span> <span class="pre">is</span> <span class="pre">out</span> <span class="pre">of</span> <span class="pre">range</span></code>.</p>
<p>When operating on decimal types with different scale and precision, the values are
first coerced to a common super type. For types near the largest representable precision (38),
this can result in Value is out of range errors when one of the operands doesn’t fit
in the common super type. For example, the common super type of decimal(38, 0) and
decimal(38, 1) is decimal(38, 1), but certain values that fit in decimal(38, 0)
cannot be represented as a decimal(38, 1).</p>
</div></blockquote>
</div>
<div class="section" id="comparison-operators">
<h2>Comparison Operators</h2>
<blockquote>
<div>All standard comparison operators and <code class="docutils literal"><span class="pre">BETWEEN</span></code> operator work for <code class="docutils literal"><span class="pre">DECIMAL</span></code> type.</div></blockquote>
</div>
<div class="section" id="unary-decimal-operators">
<h2>Unary Decimal Operators</h2>
<blockquote>
<div>The <code class="docutils literal"><span class="pre">-</span></code> operator performs negation. The type of result is same as type of argument.</div></blockquote>
</div>
</div>
</div>
<div class="bottomnav">
<p class="nav">
<span class="left">
« <a href="bitwise.html">6.7. Bitwise Functions</a>
</span>
<span class="right">
<a href="string.html">6.9. String Functions and Operators</a> »
</span>
</p>
</div>
<div class="footer" role="contentinfo">
</div>
</body>
</html>