Skip to content

Commit d61a0bd

Browse files
jlhcrawfordfabergaJack Crawfordp-steuer
authored
Floating point comparison issues
Floating point comparison issues * Use GTest EXPECT_DOUBLE_EQ to compare the real and the imaginary part of each complex vector entry instead of EXPECT_EQ to compare the vectors. * Replaced EXPECT_EQ with macro for comparing vectors of complex double * Changed EXPECT_DOUBLE_EQ to EXPECT_FLOAT_EQ to avoid precision error in multiplyBy CKKS Co-authored-by: Flavio Bergamaschi <[email protected]> Co-authored-by: Jack Crawford <[email protected]> Co-authored-by: Patrck Steuer <[email protected]>
1 parent e571b78 commit d61a0bd

File tree

4 files changed

+87
-74
lines changed

4 files changed

+87
-74
lines changed

tests/GTestPGFFT.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ static void TestIt(long n)
299299
if (vv_norm == 0) {
300300
// vv has norm = 0. Cheching if the fft is correct looking only the
301301
// enumerator.
302-
EXPECT_EQ(diff_norm, 0);
302+
EXPECT_DOUBLE_EQ(diff_norm, 0);
303303
} else {
304304
// Check if the fft relative error is smaller than the treshold.
305305
ldbl rel_err = diff_norm / vv_norm;

tests/TestCKKS.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ TEST_P(TestCKKS, negatingCiphertextWorks)
207207

208208
negateVec(vd1);
209209

210-
EXPECT_TRUE(cx_equals(vd2, vd1, NTL::conv<double>(epsilon)))
210+
EXPECT_TRUE(cx_equals(vd2, vd1, epsilon))
211211
<< " max(vd1)=" << helib::largestCoeff(vd1)
212212
<< ", max(vd2)=" << helib::largestCoeff(vd2)
213213
<< ", maxDiff=" << calcMaxDiff(vd1, vd2) << std::endl
@@ -234,7 +234,7 @@ TEST_P(TestCKKS, addingPolyConstantToCiphertextWorks)
234234

235235
add(vd1, vd2);
236236

237-
EXPECT_TRUE(cx_equals(vd3, vd1, NTL::conv<double>(epsilon)))
237+
EXPECT_TRUE(cx_equals(vd3, vd1, epsilon))
238238
<< " max(vd1)=" << helib::largestCoeff(vd1)
239239
<< ", max(vd3)=" << helib::largestCoeff(vd3)
240240
<< ", maxDiff=" << calcMaxDiff(vd1, vd3) << std::endl
@@ -260,7 +260,7 @@ TEST_P(TestCKKS, addingNegatedPolyConstantToCiphertextWorks)
260260

261261
add(vd1, vd2);
262262

263-
EXPECT_TRUE(cx_equals(vd3, vd1, NTL::conv<double>(epsilon)))
263+
EXPECT_TRUE(cx_equals(vd3, vd1, epsilon))
264264
<< " max(vd1)=" << helib::largestCoeff(vd1)
265265
<< ", max(vd3)=" << helib::largestCoeff(vd3) << std::endl
266266
<< ", maxDiff=" << calcMaxDiff(vd1, vd3)
@@ -287,7 +287,7 @@ TEST_P(TestCKKS, multiplyingPolyConstantToCiphertextWorks)
287287
mul(vd1, vd2);
288288
rf *= ea.encodeScalingFactor();
289289

290-
EXPECT_TRUE(cx_equals(vd3, vd1, NTL::conv<double>(epsilon)))
290+
EXPECT_TRUE(cx_equals(vd3, vd1, epsilon))
291291
<< " max(vd1)=" << helib::largestCoeff(vd1)
292292
<< ", max(vd3)=" << helib::largestCoeff(vd3)
293293
<< ", maxDiff=" << calcMaxDiff(vd1, vd3) << std::endl
@@ -313,7 +313,7 @@ TEST_P(TestCKKS, addingDoubleToCiphertextWorks)
313313

314314
add(vd1, vd[0]);
315315

316-
EXPECT_TRUE(cx_equals(vd2, vd1, NTL::conv<double>(epsilon)))
316+
EXPECT_TRUE(cx_equals(vd2, vd1, epsilon))
317317
<< " max(vd1)=" << helib::largestCoeff(vd1)
318318
<< ", max(vd2)=" << helib::largestCoeff(vd2)
319319
<< ", maxDiff=" << calcMaxDiff(vd1, vd2) << std::endl
@@ -340,7 +340,7 @@ TEST_P(TestCKKS, multiplyingDoubleToCiphertextWorks)
340340
rf /= std::abs(vd[0]);
341341
pm *= std::abs(vd[0]);
342342

343-
EXPECT_TRUE(cx_equals(vd2, vd1, NTL::conv<double>(epsilon)))
343+
EXPECT_TRUE(cx_equals(vd2, vd1, epsilon))
344344
<< " max(vd1)=" << helib::largestCoeff(vd1)
345345
<< ", max(vd2)=" << helib::largestCoeff(vd2)
346346
<< ", maxDiff=" << calcMaxDiff(vd1, vd2) << std::endl
@@ -367,7 +367,7 @@ TEST_P(TestCKKS, gettingTheComplexConjugateWorks)
367367

368368
conjVec(vd1);
369369

370-
EXPECT_TRUE(cx_equals(vd2, vd1, NTL::conv<double>(epsilon)))
370+
EXPECT_TRUE(cx_equals(vd2, vd1, epsilon))
371371
<< " max(vd1)=" << helib::largestCoeff(vd1)
372372
<< ", max(vd2)=" << helib::largestCoeff(vd2)
373373
<< ", maxDiff=" << calcMaxDiff(vd1, vd2) << std::endl
@@ -393,7 +393,7 @@ TEST_P(TestCKKS, rotatingCiphertextWorks)
393393
rotate(vd1, 3);
394394
// vd1 is now the expected result
395395

396-
EXPECT_TRUE(cx_equals(vd2, vd1, NTL::conv<double>(epsilon)))
396+
EXPECT_TRUE(cx_equals(vd2, vd1, epsilon))
397397
<< " max(vd1)=" << helib::largestCoeff(vd1)
398398
<< ", max(vd2)=" << helib::largestCoeff(vd2)
399399
<< ", maxDiff=" << calcMaxDiff(vd1, vd2) << std::endl
@@ -415,7 +415,7 @@ TEST_P(TestCKKS, addingCiphertextsWorks)
415415

416416
add(vd1, vd2);
417417

418-
EXPECT_TRUE(cx_equals(vd3, vd1, NTL::conv<double>(epsilon)))
418+
EXPECT_TRUE(cx_equals(vd3, vd1, epsilon))
419419
<< " max(vd1)=" << helib::largestCoeff(vd1)
420420
<< ", max(vd3)=" << helib::largestCoeff(vd3)
421421
<< ", maxDiff=" << calcMaxDiff(vd1, vd3) << std::endl
@@ -436,7 +436,7 @@ TEST_P(TestCKKS, subtractingCiphertextsWorks)
436436

437437
sub(vd1, vd2);
438438

439-
EXPECT_TRUE(cx_equals(vd3, vd1, NTL::conv<double>(epsilon)))
439+
EXPECT_TRUE(cx_equals(vd3, vd1, epsilon))
440440
<< " max(vd1)=" << helib::largestCoeff(vd1)
441441
<< ", max(vd3)=" << helib::largestCoeff(vd3)
442442
<< ", maxDiff=" << calcMaxDiff(vd1, vd3) << std::endl
@@ -458,7 +458,7 @@ TEST_P(TestCKKS, rawMultiplicationOfCiphertextsWorks)
458458

459459
mul(vd1, vd2);
460460

461-
EXPECT_TRUE(cx_equals(vd3, vd1, NTL::conv<double>(epsilon)))
461+
EXPECT_TRUE(cx_equals(vd3, vd1, epsilon))
462462
<< " max(vd1)=" << helib::largestCoeff(vd1)
463463
<< ", max(vd3)=" << helib::largestCoeff(vd3)
464464
<< ", maxDiff=" << calcMaxDiff(vd1, vd3) << std::endl
@@ -481,7 +481,7 @@ TEST_P(TestCKKS, highLevelMultiplicationOfCiphertextsWorks)
481481

482482
mul(vd1, vd2);
483483

484-
EXPECT_TRUE(cx_equals(vd3, vd1, NTL::conv<double>(epsilon)))
484+
EXPECT_TRUE(cx_equals(vd3, vd1, epsilon))
485485
<< " max(vd1)=" << helib::largestCoeff(vd1)
486486
<< ", max(vd3)=" << helib::largestCoeff(vd3)
487487
<< ", maxDiff=" << calcMaxDiff(vd1, vd3) << std::endl

0 commit comments

Comments
 (0)