1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:37:45 +00:00

AK: Add FixedPoint base 2 logarithm

The log base 2 is implemented using the binary logarithm algorithm
by Clay Turner (see the link in the comment)
This commit is contained in:
kleines Filmröllchen 2022-02-15 00:38:54 +01:00 committed by Andreas Kling
parent 30002c2ccb
commit 98058f7efe
2 changed files with 48 additions and 0 deletions

View file

@ -7,6 +7,7 @@
#include <LibTest/TestCase.h>
#include <AK/FixedPoint.h>
#include <AK/NumericLimits.h>
using Type = FixedPoint<4>;
@ -73,6 +74,18 @@ TEST_CASE(rounding)
EXPECT_EQ(Type(-1.5).ltrunk(), -1);
}
TEST_CASE(logarithm)
{
EXPECT_EQ(Type(0).log2().raw(), NumericLimits<int>::min());
EXPECT_EQ(Type(1).log2(), Type(0));
EXPECT_EQ(Type(2).log2(), Type(1));
EXPECT_EQ(Type(8).log2(), Type(3));
EXPECT_EQ(Type(0.5).log2(), Type(-1));
EXPECT_EQ(Type(22.627416997969520780827019587355).log2(), Type(4.4375));
EXPECT_EQ(Type(3088).log2(), Type(11.592457037268080419637304576833));
}
TEST_CASE(comparison)
{
EXPECT(Type(0) < 1);