Ion C
C library for Ion
Loading...
Searching...
No Matches
ion_decimal.h
Go to the documentation of this file.
1/*
2 * Copyright 2011-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at:
7 *
8 * http://aws.amazon.com/apache2.0/
9 *
10 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
12 * language governing permissions and limitations under the License.
13 */
14
36#ifndef ION_DECIMAL_H_
37#define ION_DECIMAL_H_
38
39#include "ion_types.h"
40#include "ion_platform_config.h"
41
42#ifndef DECNUMDIGITS
43 #error DECNUMDIGITS must be defined to be >= DECQUAD_Pmax
44#elif DECNUMDIGITS < DECQUAD_Pmax
45 #error DECNUMDIGITS must be defined to be >= DECQUAD_Pmax
46#endif
47
51#define ION_DECIMAL_STRLEN(ion_decimal) \
52 ((size_t)(((ion_decimal)->type == ION_DECIMAL_TYPE_QUAD) \
53 ? DECQUAD_String \
54 : (((ion_decimal)->type == ION_DECIMAL_TYPE_UNKNOWN) \
55 ? -1 \
56 : ((ion_decimal)->value.num_value->digits + 14) /* +14 is specified by decNumberToString. */ \
57 ) \
58 ))
59
60#ifdef __cplusplus
61extern "C" {
62#endif
63
64
65/*
66 * Internal structure definitions. The internals should NOT be depended upon (or cared about) by the end user.
67 */
68
72typedef enum {
73 ION_DECIMAL_TYPE_UNKNOWN = 0,
86
88
90
92
93 union {
94 decQuad quad_value;
95 decNumber *num_value;
96 } value;
97};
98
99
100/* Memory management */
101
108ION_API_EXPORT iERR ion_decimal_zero(ION_DECIMAL *value);
109
118ION_API_EXPORT iERR ion_decimal_claim(ION_DECIMAL *value);
119
125ION_API_EXPORT iERR ion_decimal_free(ION_DECIMAL *value);
126
127
128/* Conversions */
129
136ION_API_EXPORT iERR ion_decimal_to_string(const ION_DECIMAL *value, char *p_string);
137
148ION_API_EXPORT iERR ion_decimal_from_string(ION_DECIMAL *value, const char *str, decContext *context);
149
155ION_API_EXPORT iERR ion_decimal_from_uint32(ION_DECIMAL *value, uint32_t num);
156
162ION_API_EXPORT iERR ion_decimal_from_int32(ION_DECIMAL *value, int32_t num);
163
170ION_API_EXPORT iERR ion_decimal_from_quad(ION_DECIMAL *value, decQuad *quad);
171
181ION_API_EXPORT iERR ion_decimal_from_number(ION_DECIMAL *value, decNumber *number);
182
186ION_API_EXPORT iERR ion_decimal_from_ion_int(ION_DECIMAL *value, decContext *context, ION_INT *p_int);
187
193ION_API_EXPORT iERR ion_decimal_to_ion_int(const ION_DECIMAL *value, decContext *context, ION_INT *p_int);
194
195ION_API_EXPORT iERR ion_decimal_to_int32(const ION_DECIMAL *value, decContext *context, int32_t *p_int);
196ION_API_EXPORT iERR ion_decimal_to_uint32(const ION_DECIMAL *value, decContext *context, uint32_t *p_int);
197
198
199/* Operator APIs (computational) */
200
201ION_API_EXPORT iERR ion_decimal_fma(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, const ION_DECIMAL *fhs, decContext *context);
202ION_API_EXPORT iERR ion_decimal_add(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
203ION_API_EXPORT iERR ion_decimal_and(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
204ION_API_EXPORT iERR ion_decimal_divide(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
205ION_API_EXPORT iERR ion_decimal_divide_integer(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
206ION_API_EXPORT iERR ion_decimal_max(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
207ION_API_EXPORT iERR ion_decimal_max_mag(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
208ION_API_EXPORT iERR ion_decimal_min(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
209ION_API_EXPORT iERR ion_decimal_min_mag(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
210ION_API_EXPORT iERR ion_decimal_multiply(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
211ION_API_EXPORT iERR ion_decimal_or(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
212ION_API_EXPORT iERR ion_decimal_quantize(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
213ION_API_EXPORT iERR ion_decimal_remainder(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
214ION_API_EXPORT iERR ion_decimal_remainder_near(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
215ION_API_EXPORT iERR ion_decimal_rotate(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
216ION_API_EXPORT iERR ion_decimal_scaleb(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
217ION_API_EXPORT iERR ion_decimal_shift(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
218ION_API_EXPORT iERR ion_decimal_subtract(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
219ION_API_EXPORT iERR ion_decimal_xor(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
220ION_API_EXPORT iERR ion_decimal_abs(ION_DECIMAL *value, const ION_DECIMAL *rhs, decContext *context);
221ION_API_EXPORT iERR ion_decimal_invert(ION_DECIMAL *value, const ION_DECIMAL *rhs, decContext *context);
222ION_API_EXPORT iERR ion_decimal_logb(ION_DECIMAL *value, const ION_DECIMAL *rhs, decContext *context);
223ION_API_EXPORT iERR ion_decimal_minus(ION_DECIMAL *value, const ION_DECIMAL *rhs, decContext *context);
224ION_API_EXPORT iERR ion_decimal_plus(ION_DECIMAL *value, const ION_DECIMAL *rhs, decContext *context);
225ION_API_EXPORT iERR ion_decimal_reduce(ION_DECIMAL *value, const ION_DECIMAL *rhs, decContext *context);
226ION_API_EXPORT iERR ion_decimal_to_integral_exact(ION_DECIMAL *value, const ION_DECIMAL *rhs, decContext *context);
227ION_API_EXPORT iERR ion_decimal_to_integral_value(ION_DECIMAL *value, const ION_DECIMAL *rhs, decContext *context);
228
229
230/* Utility APIs (non-computational) */
231
232ION_API_EXPORT uint32_t ion_decimal_digits(const ION_DECIMAL *value);
233ION_API_EXPORT int32_t ion_decimal_get_exponent(const ION_DECIMAL *value);
234ION_API_EXPORT uint32_t ion_decimal_radix(const ION_DECIMAL *value);
235ION_API_EXPORT uint32_t ion_decimal_same_quantum(const ION_DECIMAL *lhs, const ION_DECIMAL *rhs);
236ION_API_EXPORT uint32_t ion_decimal_is_integer(const ION_DECIMAL *value);
237ION_API_EXPORT uint32_t ion_decimal_is_subnormal(const ION_DECIMAL *value, decContext *context);
238ION_API_EXPORT uint32_t ion_decimal_is_normal(const ION_DECIMAL *value, decContext *context);
239ION_API_EXPORT uint32_t ion_decimal_is_finite(const ION_DECIMAL *value);
240ION_API_EXPORT uint32_t ion_decimal_is_infinite(const ION_DECIMAL *value);
241ION_API_EXPORT uint32_t ion_decimal_is_nan(const ION_DECIMAL *value);
242ION_API_EXPORT uint32_t ion_decimal_is_negative(const ION_DECIMAL *value);
243ION_API_EXPORT uint32_t ion_decimal_is_zero(const ION_DECIMAL *value);
244ION_API_EXPORT uint32_t ion_decimal_is_canonical(const ION_DECIMAL *value);
245
246/* Comparisons */
247
254ION_API_EXPORT iERR ion_decimal_compare(const ION_DECIMAL *left, const ION_DECIMAL *right, decContext *context, int32_t *result);
255
262ION_API_EXPORT iERR ion_decimal_equals_quad(const decQuad *left, const decQuad *right, decContext *context, BOOL *is_equal);
263
268ION_API_EXPORT iERR ion_decimal_equals(const ION_DECIMAL *left, const ION_DECIMAL *right, decContext *context, BOOL *is_equal);
269
270/* Copies */
271
272ION_API_EXPORT iERR ion_decimal_canonical(ION_DECIMAL *value, const ION_DECIMAL *rhs);
273ION_API_EXPORT iERR ion_decimal_copy(ION_DECIMAL *value, const ION_DECIMAL *rhs);
274ION_API_EXPORT iERR ion_decimal_copy_abs(ION_DECIMAL *value, const ION_DECIMAL *rhs);
275ION_API_EXPORT iERR ion_decimal_copy_negate(ION_DECIMAL *value, const ION_DECIMAL *rhs);
276ION_API_EXPORT iERR ion_decimal_copy_sign(ION_DECIMAL *value, const ION_DECIMAL *rhs, const ION_DECIMAL *lhs, decContext *context);
277
278#ifdef __cplusplus
279}
280#endif
281
282#endif /* ION_DECIMAL_H_ */
ION_API_EXPORT iERR ion_decimal_from_quad(ION_DECIMAL *value, decQuad *quad)
ION_API_EXPORT iERR ion_decimal_equals_quad(const decQuad *left, const decQuad *right, decContext *context, BOOL *is_equal)
ION_API_EXPORT iERR ion_decimal_from_number(ION_DECIMAL *value, decNumber *number)
ION_API_EXPORT iERR ion_decimal_free(ION_DECIMAL *value)
ION_API_EXPORT iERR ion_decimal_to_ion_int(const ION_DECIMAL *value, decContext *context, ION_INT *p_int)
ION_DECIMAL_TYPE
Definition ion_decimal.h:72
@ ION_DECIMAL_TYPE_QUAD
Definition ion_decimal.h:77
@ ION_DECIMAL_TYPE_NUMBER
Definition ion_decimal.h:81
@ ION_DECIMAL_TYPE_NUMBER_OWNED
Definition ion_decimal.h:85
ION_API_EXPORT iERR ion_decimal_zero(ION_DECIMAL *value)
ION_API_EXPORT iERR ion_decimal_from_ion_int(ION_DECIMAL *value, decContext *context, ION_INT *p_int)
ION_API_EXPORT iERR ion_decimal_to_string(const ION_DECIMAL *value, char *p_string)
ION_API_EXPORT iERR ion_decimal_compare(const ION_DECIMAL *left, const ION_DECIMAL *right, decContext *context, int32_t *result)
ION_API_EXPORT iERR ion_decimal_from_int32(ION_DECIMAL *value, int32_t num)
ION_API_EXPORT iERR ion_decimal_from_uint32(ION_DECIMAL *value, uint32_t num)
ION_API_EXPORT iERR ion_decimal_equals(const ION_DECIMAL *left, const ION_DECIMAL *right, decContext *context, BOOL *is_equal)
ION_API_EXPORT iERR ion_decimal_from_string(ION_DECIMAL *value, const char *str, decContext *context)
ION_API_EXPORT iERR ion_decimal_claim(ION_DECIMAL *value)
Definition ion_decimal.h:89
Definition ion_int.h:100