From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 398FA6EC40; Wed, 18 Aug 2021 19:53:28 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 398FA6EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1629305608; bh=MNZqm2uOMdiPcR4NwYjR6e7M8M8pBmQPy5yH0dz49js=; h=To:References:Date:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=LDOo5q4qycXvSgQsddPUtGpmnEBxGdBo3BYPktxjjrhBomS+WFPlkEXMbj2uHebp8 RORfvV4xfqm9svfqAOHYaJuY8WqCuok+SIdzPCraxyU3XivHuKBNeRLj5LO/No8cYH NLlKnipByokqrDr73xV3T5Rx38Ilx0bOnFbyl5RA= Received: from smtp53.i.mail.ru (smtp53.i.mail.ru [94.100.177.113]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 589896EC40 for ; Wed, 18 Aug 2021 19:53:17 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 589896EC40 Received: by smtp53.i.mail.ru with esmtpa (envelope-from ) id 1mGOoV-0004G8-SL; Wed, 18 Aug 2021 19:53:16 +0300 To: Mergen Imeev References: <2ce4a5bdfd31a3da379dc030a9ffdc21ca0114ed.1629129129.git.imeevma@gmail.com> <20210818132926.GB392133@tarantool.org> Message-ID: <5a2134fc-9567-f36f-e31b-bd6c502eeef1@tarantool.org> Date: Wed, 18 Aug 2021 19:53:03 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: <20210818132926.GB392133@tarantool.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD9D5AC6413C25DCF08CC98B8FCC5CD86F3182A05F538085040ECF4DD0401CBC7FD5D2570831C11074861ECFCDAA8A2682466807215CCB7FA59 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE77BF46084C0059042EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637C6EF84A9D301917D8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8C2C755C934A2FA4F360211B754FE636E117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8B3A703B70628EAD7BA471835C12D1D977C4224003CC836476EB9C4185024447017B076A6E789B0E975F5C1EE8F4F765FCD01FCE77DFBB0C0C3AA81AA40904B5D9CF19DD082D7633A078D18283394535A93AA81AA40904B5D98AA50765F79006375908BCD2CB23604ED81D268191BDAD3D698AB9A7B718F8C4D1B931868CE1C5781A620F70A64A45A98AA50765F79006372E808ACE2090B5E1725E5C173C3A84C3C5EA940A35A165FF2DBA43225CD8A89FB26E97DCB74E6252C6EABA9B74D0DA47B5C8C57E37DE458BEDA766A37F9254B7 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458F0AFF96BAACF4158235E5A14AD4A4A4625E192CAD1D9E79D0B18DC6AC13D9A1CF971CA2051AA37E3 X-C1DE0DAB: 0D63561A33F958A5B8234C95ACC6C0F46F18BEFE9F1176335B939F21FBF1130AD59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA75C69C5C0DDE134364410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34D4140D813EC137C67E8998616FF9D6B596605CD6A6777A53C52A50AABA36A6FC581FF29E45F5AC5B1D7E09C32AA3244C374F87F6436363EEDCF4B3BA2BB1562024AF4FAF06DA24FDFACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojGSxK+6r6oBF3SZX6yYzdQw== X-Mailru-Sender: 6CA451E36783D721CBEA96CEA26D325DE580D1367AB93719EE965608AF72CC8CB7CBEF92542CD7C82F97C478340294DCC77752E0C033A69E0F0C7111264B8915FF1320A92A5534336C18EFA0BB12DBB0 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v1 3/4] sql: introduce cast for decimal X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Safin Timur via Tarantool-patches Reply-To: Safin Timur Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" LGTM On 18.08.2021 16:29, Mergen Imeev wrote: > Thank you for the review! My answer and diff below. > > On Mon, Aug 16, 2021 at 10:34:16PM +0300, Safin Timur wrote: >> Oh, now I see the reasonable implicit and explicit casts table for decimals. >> I disavow my prior complain about missing directions for CAST(exprt AS >> DECIMAL). >> >> Below there is minor complain though... >> >> On 16.08.2021 18:57, Mergen Imeev via Tarantool-patches wrote: >>> This patch introduces implicit and explicit cast of the DECIMAL field in >>> SQL. Implicit cast to and from DECIMAL is precise. Explicit conversion >>> to decimal and back could lead to loss of precision. >>> >>> Part of #4415 >>> --- >>> src/box/sql/mem.c | 334 ++++++++++++++++++++++++++++++- >>> test/sql-tap/decimal.test.lua | 366 +++++++++++++++++++++++++++++++++- >>> 2 files changed, 695 insertions(+), 5 deletions(-) >>> >>> diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c >>> index 016f0e80b..ff8b40d7f 100644 >>> --- a/src/box/sql/mem.c >>> +++ b/src/box/sql/mem.c >>> @@ -678,6 +678,17 @@ int_to_double_forced(struct Mem *mem) >>> return CMP_OLD_NEW(i, d, int64_t); >>> } >>> +static inline int >>> +int_to_dec(struct Mem *mem) >>> +{ >>> + assert(mem->type == MEM_TYPE_INT); >>> + int64_t i = mem->u.i; >>> + decimal_from_int64(&mem->u.d, i); >>> + mem->type = MEM_TYPE_DEC; >>> + mem->flags = 0; >>> + return 0; >>> +} >>> + >>> static inline int >>> uint_to_double_precise(struct Mem *mem) >>> { >>> @@ -709,6 +720,17 @@ uint_to_double_forced(struct Mem *mem) >>> return CMP_OLD_NEW(u, d, uint64_t); >>> } >>> +static inline int >>> +uint_to_dec(struct Mem *mem) >>> +{ >>> + assert(mem->type == MEM_TYPE_UINT); >>> + int64_t u = mem->u.u; >>> + decimal_from_uint64(&mem->u.d, u); >>> + mem->type = MEM_TYPE_DEC; >>> + mem->flags = 0; >>> + return 0; >>> +} >>> + >>> static inline int >>> int_to_str0(struct Mem *mem) >>> { >>> @@ -855,6 +877,19 @@ str_to_double(struct Mem *mem) >>> return 0; >>> } >>> +static inline int >>> +str_to_dec(struct Mem *mem) >>> +{ >>> + assert(mem->type == MEM_TYPE_STR); >>> + decimal_t dec; >>> + decimal_t *d; >>> + d = decimal_from_string(&dec, mem->z); >>> + if (d == NULL) >>> + return -1; >>> + mem_set_dec(mem, &dec); >>> + return 0; >>> +} >>> + >>> static inline int >>> double_to_int(struct Mem *mem) >>> { >>> @@ -988,6 +1023,69 @@ double_to_uint_forced(struct Mem *mem) >>> return res; >>> } >>> +static inline int >>> +double_to_dec(struct Mem *mem) >>> +{ >>> + assert(mem->type == MEM_TYPE_DOUBLE); >>> + double d = mem->u.r; >>> + if (d >= 1e38 || d <= -1e38) >>> + return -1; >>> + decimal_from_double(&mem->u.d, d); >>> + mem->type = MEM_TYPE_DEC; >>> + mem->flags = 0; >>> + return 0; >>> +} >>> + >>> +static inline int >>> +double_to_dec_precise(struct Mem *mem) >>> +{ >>> + assert(mem->type == MEM_TYPE_DOUBLE); >>> + double d = mem->u.r; >>> + if (d >= 1e38 || d <= -1e38) >>> + return -1; >>> + decimal_t dec; >>> + decimal_from_double(&dec, d); >>> + if (atof(decimal_str(&mem->u.d)) != d) >>> + return -1; >>> + mem->u.d = dec; >>> + mem->type = MEM_TYPE_DEC; >>> + mem->flags = 0; >>> + return 0; >>> +} >>> + >>> +/** >>> + * Cast MEM with DOUBLE to DECIMAL. Doesn't fail. The return value is < 0 >>> if >>> + * the original value is less than the result, > 0 if the original value >>> is >>> + * greater than the result, and 0 if the cast is precise. >>> + */ >>> +static inline int >>> +double_to_dec_forced(struct Mem *mem) >>> +{ >>> + assert(mem->type == MEM_TYPE_DOUBLE); >>> + double d = mem->u.r; >>> + mem->type = MEM_TYPE_DEC; >>> + mem->flags = 0; >>> + if (d >= 1e38) { >>> + const char *val = >>> "99999999999999999999999999999999999999"; >>> + assert(strlen(val) == 38); >>> + decimal_from_string(&mem->u.d, val); >>> + return 1; >>> + } >>> + if (d <= -1e38) { >>> + const char *val = >>> "-99999999999999999999999999999999999999"; >>> + assert(strlen(val) == 39); >>> + decimal_from_string(&mem->u.d, val); >>> + return -1; >>> + } >>> + decimal_from_double(&mem->u.d, d); >>> + double tmp = atof(decimal_str(&mem->u.d)); >>> + if (d > tmp) >>> + return 1; >>> + if (d < tmp) >>> + return -1; >>> + return 0; >>> +} >>> + >>> static inline int >>> double_to_str0(struct Mem *mem) >>> { >>> @@ -1001,6 +1099,173 @@ double_to_str0(struct Mem *mem) >>> return 0; >>> } >>> +static inline int >>> +dec_to_int(struct Mem *mem) >>> +{ >>> + assert(mem->type == MEM_TYPE_DEC); >>> + if (decimal_is_neg(&mem->u.d)) { >>> + int64_t i; >>> + if (decimal_to_int64(&mem->u.d, &i) == NULL) >>> + return -1; >>> + assert(i < 0); >>> + mem->u.i = i; >>> + mem->type = MEM_TYPE_INT; >>> + mem->flags = 0; >>> + return 0; >>> + } >>> + uint64_t u; >>> + if (decimal_to_uint64(&mem->u.d, &u) == NULL) >>> + return -1; >>> + mem->u.u = u; >>> + mem->type = MEM_TYPE_UINT; >>> + mem->flags = 0; >>> + return 0; >>> +} >>> + >>> +static inline int >>> +dec_to_int_precise(struct Mem *mem) >>> +{ >>> + assert(mem->type == MEM_TYPE_DEC); >>> + if (!decimal_is_int(&mem->u.d)) >>> + return -1; >>> + return dec_to_int(mem); >>> +} >>> + >>> +static inline int >>> +dec_to_int_forced(struct Mem *mem) >>> +{ >>> + assert(mem->type == MEM_TYPE_DEC); >>> + if (decimal_is_neg(&mem->u.d)) { >>> + int64_t i; >>> + mem->type = MEM_TYPE_INT; >>> + mem->flags = 0; >>> + if (decimal_to_int64(&mem->u.d, &i) == NULL) { >>> + mem->u.i = INT64_MIN; >>> + return -1; >>> + } >>> + assert(i < 0); >>> + mem->u.i = i; >>> + /* >>> + * Decimal is floored when cast to int, which means that >>> after >>> + * cast it becomes bigger if it was not integer. >>> + */ >>> + return decimal_is_int(&mem->u.d) ? 0 : -1; >>> + } >>> + uint64_t u; >>> + mem->type = MEM_TYPE_UINT; >>> + mem->flags = 0; >>> + if (decimal_to_uint64(&mem->u.d, &u) == NULL) { >>> + mem->u.u = UINT64_MAX; >>> + return 1; >>> + } >>> + mem->u.u = u; >>> + /* >>> + * Decimal is floored when cast to uint, which means that after >>> cast it >>> + * becomes less if it was not integer. >>> + */ >>> + return decimal_is_int(&mem->u.d) ? 0 : 1; >>> +} >>> + >>> +static inline int >>> +dec_to_uint(struct Mem *mem) >>> +{ >>> + assert(mem->type == MEM_TYPE_DEC); >>> + uint64_t u; >>> + if (decimal_to_uint64(&mem->u.d, &u) == NULL) >>> + return -1; >>> + mem->u.u = u; >>> + mem->type = MEM_TYPE_UINT; >>> + mem->flags = 0; >>> + return 0; >>> +} >>> + >>> +static inline int >>> +dec_to_uint_precise(struct Mem *mem) >>> +{ >>> + assert(mem->type == MEM_TYPE_DEC); >>> + if (!decimal_is_int(&mem->u.d)) >>> + return -1; >>> + return dec_to_uint(mem); >>> +} >>> + >>> +static inline int >>> +dec_to_uint_forced(struct Mem *mem) >>> +{ >>> + assert(mem->type == MEM_TYPE_DEC); >>> + uint64_t u; >>> + mem->type = MEM_TYPE_UINT; >>> + mem->flags = 0; >>> + if (decimal_to_uint64(&mem->u.d, &u) == NULL) { >>> + if (decimal_is_neg(&mem->u.d)) { >>> + mem->u.u = 0; >>> + return -1; >>> + } >>> + mem->u.u = UINT64_MAX; >>> + return 1; >>> + } >>> + mem->u.u = u; >>> + /* >>> + * Decimal is floored when cast to uint, which means that after >>> cast if >>> + * if was not integer it becomes less if it was positive, and move >>> if it >>> + * was negative. >>> + */ >>> + if (decimal_is_int(&mem->u.d)) >>> + return 0; >>> + return decimal_is_neg(&mem->u.d) ? -1 : 1; >>> +} >>> + >>> +static inline int >>> +dec_to_double(struct Mem *mem) >>> +{ >>> + assert(mem->type == MEM_TYPE_DEC); >>> + double r = atof(decimal_str(&mem->u.d)); >>> + mem->u.r = r; >>> + mem->type = MEM_TYPE_DOUBLE; >>> + mem->flags = 0; >>> + return 0; >>> +} >>> + >>> +static inline int >>> +dec_to_double_precise(struct Mem *mem) >>> +{ >>> + assert(mem->type == MEM_TYPE_DEC); >>> + double r = atof(decimal_str(&mem->u.d)); >>> + decimal_t d; >>> + decimal_t *dec = decimal_from_double(&d, r); >>> + if (dec == NULL || decimal_compare(dec, &mem->u.d) != 0) >>> + return -1; >>> + mem->u.r = r; >>> + mem->type = MEM_TYPE_DOUBLE; >>> + mem->flags = 0; >>> + return 0; >>> +} >>> + >>> +static inline int >>> +dec_to_double_forced(struct Mem *mem) >>> +{ >>> + assert(mem->type == MEM_TYPE_DEC); >>> + mem->type = MEM_TYPE_DOUBLE; >>> + mem->flags = 0; >>> + double r = atof(decimal_str(&mem->u.d)); >>> + int res; >>> + decimal_t d; >>> + if (r <= -1e38) >>> + res = 1; >>> + else if (r >= 1e38) >>> + res = -1; >>> + else >>> + res = decimal_compare(&mem->u.d, decimal_from_double(&d, >>> r)); >>> + mem->u.r = r; >>> + return res; >>> +} >>> + >>> +static inline int >>> +dec_to_str0(struct Mem *mem) >>> +{ >>> + assert(mem->type == MEM_TYPE_DEC); >>> + return mem_copy_str0(mem, decimal_str(&mem->u.d)); >>> +} >>> + >>> static inline int >>> bool_to_str0(struct Mem *mem) >>> { >>> @@ -1053,6 +1318,8 @@ mem_to_int(struct Mem *mem) >>> return str_to_int(mem); >>> if (mem->type == MEM_TYPE_DOUBLE) >>> return double_to_int(mem); >>> + if (mem->type == MEM_TYPE_DEC) >>> + return dec_to_int(mem); >>> return -1; >>> } >>> @@ -1068,6 +1335,8 @@ mem_to_int_precise(struct Mem *mem) >>> return str_to_int(mem); >>> if (mem->type == MEM_TYPE_DOUBLE) >>> return double_to_int_precise(mem); >>> + if (mem->type == MEM_TYPE_DEC) >>> + return dec_to_int(mem); >>> return -1; >>> } >>> @@ -1083,6 +1352,8 @@ mem_to_double(struct Mem *mem) >>> return int_to_double(mem); >>> if (mem->type == MEM_TYPE_STR) >>> return str_to_double(mem); >>> + if (mem->type == MEM_TYPE_DEC) >>> + return dec_to_double(mem); >>> return -1; >>> } >>> @@ -1129,6 +1400,8 @@ mem_to_str0(struct Mem *mem) >>> return array_to_str0(mem); >>> case MEM_TYPE_UUID: >>> return uuid_to_str0(mem); >>> + case MEM_TYPE_DEC: >>> + return dec_to_str0(mem); >>> default: >>> return -1; >>> } >>> @@ -1157,6 +1430,8 @@ mem_to_str(struct Mem *mem) >>> return array_to_str0(mem); >>> case MEM_TYPE_UUID: >>> return uuid_to_str0(mem); >>> + case MEM_TYPE_DEC: >>> + return dec_to_str0(mem); >>> default: >>> return -1; >>> } >>> @@ -1177,6 +1452,8 @@ mem_cast_explicit(struct Mem *mem, enum field_type >>> type) >>> return str_to_uint(mem); >>> case MEM_TYPE_DOUBLE: >>> return double_to_uint(mem); >>> + case MEM_TYPE_DEC: >>> + return dec_to_uint(mem); >>> default: >>> return -1; >>> } >>> @@ -1209,9 +1486,21 @@ mem_cast_explicit(struct Mem *mem, enum field_type >>> type) >>> case FIELD_TYPE_NUMBER: >>> return mem_to_number(mem); >>> case FIELD_TYPE_DECIMAL: >>> - if (mem->type == MEM_TYPE_DEC) >>> + switch (mem->type) { >>> + case MEM_TYPE_INT: >>> + return int_to_dec(mem); >>> + case MEM_TYPE_UINT: >>> + return uint_to_dec(mem); >>> + case MEM_TYPE_STR: >>> + return str_to_dec(mem); >>> + case MEM_TYPE_DOUBLE: >>> + return double_to_dec(mem); >>> + case MEM_TYPE_DEC: >>> + mem->flags = 0; >>> return 0; >>> - return -1; >>> + default: >>> + return -1; >>> + } >> >> For consistency with majority of other numeric directions (mem_to_int(), >> mem_to_number(), mem_to_double()) I'd extract this code similarly to >> mem_to_decimal(). But this is up to you... >> >> > I will do this later, when I have time for some refactoring. > >>> case FIELD_TYPE_UUID: >>> if (mem->type == MEM_TYPE_UUID) { >>> mem->flags = 0; >> ... >> >>> @@ -1296,9 +1591,19 @@ mem_cast_implicit(struct Mem *mem, enum field_type >>> type) >>> mem->flags = MEM_Number; >>> return 0; >>> case FIELD_TYPE_DECIMAL: >>> - if (mem->type == MEM_TYPE_DEC) >>> + switch (mem->type) { >>> + case MEM_TYPE_INT: >>> + return int_to_dec(mem); >>> + case MEM_TYPE_UINT: >>> + return uint_to_dec(mem); >>> + case MEM_TYPE_DOUBLE: >>> + return double_to_dec_precise(mem); >>> + case MEM_TYPE_DEC: >>> + mem->flags = 0; >>> return 0; >>> - return -1; >>> + default: >>> + return -1; >>> + } >>> case FIELD_TYPE_MAP: >>> if (mem->type == MEM_TYPE_MAP) >>> return 0; >>> @@ -1343,6 +1648,8 @@ mem_cast_implicit_number(struct Mem *mem, enum >>> field_type type) >>> return -1; >>> case MEM_TYPE_DOUBLE: >>> return double_to_uint_forced(mem); >>> + case MEM_TYPE_DEC: >>> + return dec_to_uint_forced(mem); >>> default: >>> unreachable(); >>> } >>> @@ -1353,6 +1660,8 @@ mem_cast_implicit_number(struct Mem *mem, enum >>> field_type type) >>> return int_to_double_forced(mem); >>> case MEM_TYPE_UINT: >>> return uint_to_double_forced(mem); >>> + case MEM_TYPE_DEC: >>> + return dec_to_double_forced(mem); >>> case MEM_TYPE_DOUBLE: >>> mem->flags = 0; >>> return 0; >>> @@ -1368,6 +1677,23 @@ mem_cast_implicit_number(struct Mem *mem, enum >>> field_type type) >>> return 0; >>> case MEM_TYPE_DOUBLE: >>> return double_to_int_forced(mem); >>> + case MEM_TYPE_DEC: >>> + return dec_to_int_forced(mem); >>> + default: >>> + unreachable(); >>> + } >>> + break; >>> + case FIELD_TYPE_DECIMAL: >>> + switch (mem->type) { >>> + case MEM_TYPE_INT: >>> + return int_to_dec(mem); >>> + case MEM_TYPE_UINT: >>> + return uint_to_dec(mem); >>> + case MEM_TYPE_DEC: >>> + mem->flags = 0; >>> + return 0; >>> + case MEM_TYPE_DOUBLE: >>> + return double_to_dec_forced(mem); >>> default: >>> unreachable(); >>> } >> >> Otherwise great patch, with the expected extension to explicit/implicit >> casts tables. >> >> Thanks, >> Timur > > > Diff: > > > diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c > index ff8b40d7f..92f671ab5 100644 > --- a/src/box/sql/mem.c > +++ b/src/box/sql/mem.c > @@ -1028,9 +1028,10 @@ double_to_dec(struct Mem *mem) > { > assert(mem->type == MEM_TYPE_DOUBLE); > double d = mem->u.r; > - if (d >= 1e38 || d <= -1e38) > + decimal_t dec; > + if (decimal_from_double(&dec, d) == NULL) > return -1; > - decimal_from_double(&mem->u.d, d); > + mem->u.d = dec; > mem->type = MEM_TYPE_DEC; > mem->flags = 0; > return 0; > @@ -1041,11 +1042,9 @@ double_to_dec_precise(struct Mem *mem) > { > assert(mem->type == MEM_TYPE_DOUBLE); > double d = mem->u.r; > - if (d >= 1e38 || d <= -1e38) > - return -1; > decimal_t dec; > - decimal_from_double(&dec, d); > - if (atof(decimal_str(&mem->u.d)) != d) > + if (decimal_from_double(&dec, d) == NULL || > + atof(decimal_str(&dec)) != d) > return -1; > mem->u.d = dec; > mem->type = MEM_TYPE_DEC; > @@ -1206,8 +1205,9 @@ dec_to_uint_forced(struct Mem *mem) > mem->u.u = u; > /* > * Decimal is floored when cast to uint, which means that after cast if > - * if was not integer it becomes less if it was positive, and move if it > - * was negative. > + * it was not integer it becomes less if it was positive, and move if it > + * was negative. For example, DECIMAL value -1.5 becomes -1 after cast > + * to INTEGER and DECIMAL value 1.5 becomes 1 after cast to INTEGER. > */ > if (decimal_is_int(&mem->u.d)) > return 0; >