From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org> To: korablev@tarantool.org Cc: tarantool-patches@dev.tarantool.org Subject: [Tarantool-patches] [PATCH v1 1/4] decimal: introduce decimal_is_neg() Date: Mon, 16 Aug 2021 18:57:00 +0300 [thread overview] Message-ID: <175c9ed1210c35b9e51d89bf9abcdd2c0f941638.1629129129.git.imeevma@gmail.com> (raw) In-Reply-To: <cover.1629129129.git.imeevma@gmail.com> This patch introduces function decimal_is_neg() which checks that decimal is less than zero. Needed for #4415 --- src/lib/core/decimal.c | 6 ++++++ src/lib/core/decimal.h | 4 ++++ test/unit/decimal.c | 7 ++++++- test/unit/decimal.result | 10 +++++++++- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/lib/core/decimal.c b/src/lib/core/decimal.c index 6d2ccb96f..7543ef44d 100644 --- a/src/lib/core/decimal.c +++ b/src/lib/core/decimal.c @@ -115,6 +115,12 @@ decimal_is_int(decimal_t *dec) return decNumberIsInt(dec); } +bool +decimal_is_neg(const decimal_t *dec) +{ + return decNumberIsNegative(dec) && !decNumberIsZero(dec); +} + decimal_t * decimal_from_string(decimal_t *dec, const char *str) { diff --git a/src/lib/core/decimal.h b/src/lib/core/decimal.h index aeafd2c68..15a576648 100644 --- a/src/lib/core/decimal.h +++ b/src/lib/core/decimal.h @@ -81,6 +81,10 @@ decimal_zero(decimal_t *dec); bool decimal_is_int(decimal_t *dec); +/** @return true if the decimal is negative, false otherwise. */ +bool +decimal_is_neg(const decimal_t *dec); + /** * Initialize a decimal with a value from the string. * diff --git a/test/unit/decimal.c b/test/unit/decimal.c index aea646e15..328b1b668 100644 --- a/test/unit/decimal.c +++ b/test/unit/decimal.c @@ -336,7 +336,7 @@ test_mp_print(void) int main(void) { - plan(304); + plan(312); dectest(314, 271, uint64, uint64_t); dectest(65535, 23456, uint64, uint64_t); @@ -416,5 +416,10 @@ main(void) dectest_is(is_int, 1.0000, true); dectest_is(is_int, 1.0000001, false); + dectest_is(is_neg, 1, false); + dectest_is(is_neg, -1, true); + dectest_is(is_neg, 0, false); + dectest_is(is_neg, -0, false); + return check_plan(); } diff --git a/test/unit/decimal.result b/test/unit/decimal.result index b7da2d2ce..ceaaf718a 100644 --- a/test/unit/decimal.result +++ b/test/unit/decimal.result @@ -1,4 +1,4 @@ -1..304 +1..312 ok 1 - decimal(314) ok 2 - decimal(271) ok 3 - decimal(314) + decimal(271) @@ -747,3 +747,11 @@ ok 301 - decimal_from_string(1.0000) ok 302 - decimal_is_int(1.0000) - expected true ok 303 - decimal_from_string(1.0000001) ok 304 - decimal_is_int(1.0000001) - expected false +ok 305 - decimal_from_string(1) +ok 306 - decimal_is_neg(1) - expected false +ok 307 - decimal_from_string(-1) +ok 308 - decimal_is_neg(-1) - expected true +ok 309 - decimal_from_string(0) +ok 310 - decimal_is_neg(0) - expected false +ok 311 - decimal_from_string(-0) +ok 312 - decimal_is_neg(-0) - expected false -- 2.25.1
next prev parent reply other threads:[~2021-08-16 15:57 UTC|newest] Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-08-16 15:56 [Tarantool-patches] [PATCH v1 0/4] Introduce DECIMAL to SQL Mergen Imeev via Tarantool-patches 2021-08-16 15:57 ` Mergen Imeev via Tarantool-patches [this message] 2021-08-18 16:54 ` [Tarantool-patches] [PATCH v1 1/4] decimal: introduce decimal_is_neg() Safin Timur via Tarantool-patches 2021-08-16 15:57 ` [Tarantool-patches] [PATCH v1 2/4] sql: introduce field type decimal Mergen Imeev via Tarantool-patches 2021-08-16 19:22 ` Safin Timur via Tarantool-patches 2021-08-18 13:01 ` Mergen Imeev via Tarantool-patches 2021-08-18 16:52 ` Safin Timur via Tarantool-patches 2021-08-16 15:57 ` [Tarantool-patches] [PATCH v1 3/4] sql: introduce cast for decimal Mergen Imeev via Tarantool-patches 2021-08-16 19:34 ` Safin Timur via Tarantool-patches 2021-08-18 13:29 ` Mergen Imeev via Tarantool-patches 2021-08-18 16:53 ` Safin Timur via Tarantool-patches 2021-08-16 15:57 ` [Tarantool-patches] [PATCH v1 4/4] sql: introduce decimal to arithmetic Mergen Imeev via Tarantool-patches 2021-08-16 19:48 ` Safin Timur via Tarantool-patches 2021-08-17 12:23 ` Serge Petrenko via Tarantool-patches 2021-08-18 13:32 ` Mergen Imeev via Tarantool-patches 2021-08-18 16:53 ` Safin Timur via Tarantool-patches
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=175c9ed1210c35b9e51d89bf9abcdd2c0f941638.1629129129.git.imeevma@gmail.com \ --to=tarantool-patches@dev.tarantool.org \ --cc=imeevma@tarantool.org \ --cc=korablev@tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH v1 1/4] decimal: introduce decimal_is_neg()' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox