Skip to content

Commit

Permalink
[RF] extend testNaNPacker with demonstrations of arithmetic
Browse files Browse the repository at this point in the history
I wrote this small test just to make sure arithmetic worked as expected on what at first I thought was a rather exotic class. I think in the end it also serves as a demonstration of how cool and useful this class is.
  • Loading branch information
egpbos authored and Piter Amador Paye Mamani committed Jun 3, 2024
1 parent 672e415 commit 4bacb4f
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions roofit/roofitcore/test/testNaNPacker.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,43 @@ TEST(RooNaNPacker, CanPackStuffIntoNaNs)
dumpFloats(rnp._payload);
}

// Demonstrate value preserving behavior after arithmetic on packed NaNs.
TEST(RooNaNPacker, PackedNaNPreservedAfterArithmetic)
{
RooNaNPacker rnp, rnp2;
rnp.setPayload(1.337f);
EXPECT_TRUE(rnp.isNaNWithPayload());

// multiply the packed NaN by 1 and use the result as rnp2's NaN with payload
rnp2._payload = 1. * rnp.getNaNWithPayload();
EXPECT_TRUE(rnp2.isNaNWithPayload());
EXPECT_EQ(rnp.getPayload(), rnp2.getPayload());

// multiply the packed NaN by -1
rnp2._payload = -1. * rnp.getNaNWithPayload();
EXPECT_TRUE(rnp2.isNaNWithPayload());
// minus signs on the NaN don't affect the payload
EXPECT_EQ(rnp.getPayload(), rnp2.getPayload());

// multiply the packed NaN by 4242
rnp2._payload = 4242. * rnp.getNaNWithPayload();
EXPECT_TRUE(rnp2.isNaNWithPayload());
// random multiplicative values on the NaN don't affect it all either
EXPECT_EQ(rnp.getPayload(), rnp2.getPayload());

// add 4242 to the packed NaN
rnp2._payload = rnp.getNaNWithPayload() + 4242.;
EXPECT_TRUE(rnp2.isNaNWithPayload());
// addition also has no effect, the NaN retains its bits
EXPECT_EQ(rnp.getPayload(), rnp2.getPayload());

// divide packed NaN by 1337, subtract 20 and take the modulo of 38 before calculating the sine of all this
rnp2._payload = std::sin(std::fmod((rnp.getNaNWithPayload() / 1337. - 20.), 38.));
EXPECT_TRUE(rnp2.isNaNWithPayload());
// nothing can harm the PackedNaN
EXPECT_EQ(rnp.getPayload(), rnp2.getPayload());
}

/// Fit a simple linear function, that starts in the negative.
TEST(RooNaNPacker, FitSimpleLinear)
{
Expand Down

0 comments on commit 4bacb4f

Please sign in to comment.