Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

index encode double wrong #3034

Closed
cangfengzhs opened this issue Oct 11, 2021 · 1 comment
Closed

index encode double wrong #3034

cangfengzhs opened this issue Oct 11, 2021 · 1 comment
Assignees
Labels
incompatible PR: incompatible with the recently released version type/bug Type: something is unexpected
Milestone

Comments

@cangfengzhs
Copy link
Contributor

  static std::string encodeDouble(double v) {
    if (v < 0) {
      /**
       *   TODO : now, the -(std::numeric_limits<double>::min())
       *   have a problem of precision overflow. current return value is -nan.
       */
      auto i = *reinterpret_cast<const int64_t*>(&v);
      i = -(std::numeric_limits<int64_t>::max() + i);
      v = *reinterpret_cast<const double*>(&i);
    }
    auto val = folly::Endian::big(v);
    auto* c = reinterpret_cast<char*>(&val);
    c[0] ^= 0x80;
    std::string raw;
    raw.reserve(sizeof(double));
    raw.append(c, sizeof(double));
    return raw;
  }

0.0 and -4.9406564584124654e-324,-0.0 and 4.9406564584124654e-324, encoding string are the same.

Detail:
(For simplicity, we use two bytes instead of eight bytes)

4.9406564584124654e-324 0.0 -0.0 -4.9406564584124654e-324
origin 0001 0000 1000 1001
+max 1111 0000
negative 0001 0000
^\x80 1001 1000 1001 1000

Suggestion:
In TiKV and yugabyteDB: negative values have all bits negated while positive values have the high bit turned on.
In MyRocks: Outside of the way above, exponent of positive values gets incremented.But I don’t know the meaning of doing this.

https://github.com/yugabyte/yugabyte-db/blob/d28d82e8c48bb068b538a3ea0e5165525e46838d/src/yb/util/kv_util.h#L67:6
https://github.com/pingcap/tidb/blob/master/util/codec/float.go
https://github.com/facebook/mysql-5.6/wiki/MyRocks-record-format

@cangfengzhs cangfengzhs added type/bug Type: something is unexpected incompatible PR: incompatible with the recently released version labels Oct 11, 2021
@Sophie-Xie Sophie-Xie modified the milestones: v2.7.0, v3.0.0 Oct 12, 2021
@cangfengzhs cangfengzhs linked a pull request Oct 26, 2021 that will close this issue
7 tasks
@cangfengzhs
Copy link
Contributor Author

fix in #3196

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
incompatible PR: incompatible with the recently released version type/bug Type: something is unexpected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants