Round a double multiplication before casting it to integer
authorBenjamin Barenblat <bbaren@google.com>
Fri, 25 Feb 2022 01:39:43 +0000 (01:39 +0000)
committerPeter Michael Green <plugwash@raspbian.org>
Fri, 25 Feb 2022 01:39:43 +0000 (01:39 +0000)
commit7010ab5487783da65cfa48b01503912f5dfce133
tree40fc382bc1f2335995d7e41ebf3703e2c7d46830
parentd298916f574c6532ae9bffa18870682c3801471e
Round a double multiplication before casting it to integer

Forwarded: yes
Applied-Upstream: https://github.com/abseil/abseil-cpp/commit/60be12ed9822078970f05f3c560324184302df6b

The code

    static_cast<int>(x * y)

(for double x and y) performs a double multiplication into a temporary
that, by standard, may have excess precision. The subsequent cast to int
discards the excess precision. However, the cast may examine the excess
precision during conversion, producing surprising results like

    static_cast<int>(1.7 * 10) == 16

on certain systems. Correct this case by explicitly rounding 1.7 * 10
before casting it.

The author works at Google. Upstream applied this patch as Piper
revision 378922064 and exported it to GitHub; the Applied-Upstream URL
above points to the exported commit.

Gbp-Pq: Name missing-rint.diff
absl/random/mocking_bit_gen_test.cc