diff options
-rw-r--r-- | src/util/integer_cln_imp.h | 9 | ||||
-rw-r--r-- | test/unit/util/integer_black.h | 21 |
2 files changed, 29 insertions, 1 deletions
diff --git a/src/util/integer_cln_imp.h b/src/util/integer_cln_imp.h index 78b775bee..ad21be287 100644 --- a/src/util/integer_cln_imp.h +++ b/src/util/integer_cln_imp.h @@ -456,7 +456,14 @@ public: if(s == 0){ return 1; }else if(s < 0){ - return cln::integer_length(-d_value); + size_t len = cln::integer_length(d_value); + /*If this is -2^n, return len+1 not len to stay consistent with the definition above! + * From CLN's documentation of integer_length: + * This is the smallest n >= 0 such that -2^n <= x < 2^n. + * If x > 0, this is the unique n > 0 such that 2^(n-1) <= x < 2^n. + */ + size_t ord2 = cln::ord2(d_value); + return (len == ord2) ? (len + 1) : len; }else{ return cln::integer_length(d_value); } diff --git a/test/unit/util/integer_black.h b/test/unit/util/integer_black.h index ead11e4df..d2f697637 100644 --- a/test/unit/util/integer_black.h +++ b/test/unit/util/integer_black.h @@ -366,4 +366,25 @@ public: TS_ASSERT( ! Integer(64).testBit(1) ); TS_ASSERT( ! Integer(64).testBit(0) ); } + + unsigned int internalLength(int i){ + if ( i == 0){ + return 1; + }else{ + int absi = i < 0 ? -i : i; + unsigned int n = 0; + int powN = 1; + do { + powN <<= 1; + ++n; + }while(absi >= powN); + return n; + } + } + + void testTestLength() { + for(int i = -17; i <= 17; ++i){ + TS_ASSERT_EQUALS(Integer(i).length(), internalLength(i)); + } + } }; |