Skip to content

Commit 0201e94

Browse files
cynthiajoanCynthia Jiang
andauthored
Fix database double type precision bug (#709)
* Fix VariantToJson double data precision issue * format the code * add readme * restore messaging test * restore messaging integration test * fix the test typo * Update review feedback Co-authored-by: Cynthia Jiang <[email protected]>
1 parent 3990d4c commit 0201e94

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

app/src/variant_util.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ static bool VariantToJson(const Variant& variant, bool prettyPrint,
5555
break;
5656
}
5757
case Variant::kTypeDouble: {
58-
*ss << variant.double_value();
58+
// IEEE 754 double-precision binary floating-point format: binary64 — The
59+
// 53-bit significand precision gives from 15 to 17 significant decimal
60+
// digits Default 32-bit only keeps 7 digit precision
61+
*ss << std::setprecision(17) << variant.double_value();
5962
break;
6063
}
6164
case Variant::kTypeBool: {

database/integration_test/src/integration_test.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ static const int kSimpleInt = 2;
391391
static const int kSimplePriority = 100;
392392
static const double kSimpleDouble = 3.4;
393393
static const bool kSimpleBool = true;
394+
static const double kLongDouble = 0.123456789876543;
394395

395396
TEST_F(FirebaseDatabaseTest, TestSetAndGetSimpleValues) {
396397
const char* test_name = test_info_->name();
@@ -415,12 +416,15 @@ TEST_F(FirebaseDatabaseTest, TestSetAndGetSimpleValues) {
415416
ref.Child(test_name)
416417
.Child("IntAndPriority")
417418
.SetValueAndPriority(kSimpleInt, kSimplePriority);
419+
firebase::Future<void> f7 =
420+
ref.Child(test_name).Child("LongDouble").SetValue(kLongDouble);
418421
WaitForCompletion(f1, "SetSimpleString");
419422
WaitForCompletion(f2, "SetSimpleInt");
420423
WaitForCompletion(f3, "SetSimpleDouble");
421424
WaitForCompletion(f4, "SetSimpleBool");
422425
WaitForCompletion(f5, "SetSimpleTimestamp");
423426
WaitForCompletion(f6, "SetSimpleIntAndPriority");
427+
WaitForCompletion(f7, "SetLongDouble");
424428
}
425429

426430
// Get the values that we just set, and confirm that they match what we
@@ -439,12 +443,15 @@ TEST_F(FirebaseDatabaseTest, TestSetAndGetSimpleValues) {
439443
ref.Child(test_name).Child("Timestamp").GetValue();
440444
firebase::Future<firebase::database::DataSnapshot> f6 =
441445
ref.Child(test_name).Child("IntAndPriority").GetValue();
446+
firebase::Future<firebase::database::DataSnapshot> f7 =
447+
ref.Child(test_name).Child("LongDouble").GetValue();
442448
WaitForCompletion(f1, "GetSimpleString");
443449
WaitForCompletion(f2, "GetSimpleInt");
444450
WaitForCompletion(f3, "GetSimpleDouble");
445451
WaitForCompletion(f4, "GetSimpleBool");
446452
WaitForCompletion(f5, "GetSimpleTimestamp");
447453
WaitForCompletion(f6, "GetSimpleIntAndPriority");
454+
WaitForCompletion(f7, "GetLongDouble");
448455

449456
// Get the current time to compare to the Timestamp.
450457
int64_t current_time_milliseconds =
@@ -458,6 +465,7 @@ TEST_F(FirebaseDatabaseTest, TestSetAndGetSimpleValues) {
458465
TimestampIsNear(current_time_milliseconds));
459466
EXPECT_EQ(f6.result()->value().AsInt64(), kSimpleInt);
460467
EXPECT_EQ(f6.result()->priority().AsInt64(), kSimplePriority);
468+
EXPECT_EQ(f7.result()->value().AsDouble(), kLongDouble);
461469
}
462470
}
463471

release_build_files/readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,10 @@ workflow use only during the development of your app, not for publicly shipping
567567
code.
568568

569569
## Release Notes
570+
### Next Release
571+
- Changes
572+
- General: Variant double type now support 64-bit while saving to json.
573+
([#1133](https://github.com/firebase/quickstart-unity/issues/1133)).
570574

571575
### 8.6.0
572576
- Changes

0 commit comments

Comments
 (0)