From 6cb391f9a2ed1ea49ef8dc7b9456f5d1fe4fde22 Mon Sep 17 00:00:00 2001 From: Arne Welzel Date: Mon, 8 May 2023 12:44:31 +0200 Subject: [PATCH] zeek.bif: Add log2() and ceil() Closes #2930, #2931. --- NEWS | 2 ++ src/zeek.bif | 38 +++++++++++++++++++++++----- testing/btest/Baseline/bifs.math/out | 5 ++++ testing/btest/bifs/math.zeek | 7 +++++ 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/NEWS b/NEWS index a46b1a60e9..e58a91fc0f 100644 --- a/NEWS +++ b/NEWS @@ -234,6 +234,8 @@ New Functionality can happen in packet loss or packet re-ordering scenarios. Such connections will have a ``^`` added to their history. +- New bifs for ``ceil()`` and ``log2()`` have been added. + Changed Functionality --------------------- diff --git a/src/zeek.bif b/src/zeek.bif index 65b6df3054..b0e3c6611a 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -1680,19 +1680,33 @@ function print_raw%(...%): bool ## ## Returns: The next lowest integer of *d* as :zeek:type:`double`. ## -## .. zeek:see:: sqrt exp ln log10 pow +## .. zeek:see:: ceil sqrt exp ln log2 log10 pow function floor%(d: double%): double %{ return zeek::make_intrusive(floor(d)); %} +## Computes the smallest integer greater or equal than the given :zeek:type:`double` value. +## For example, ``ceil(3.14)`` returns ``4.0``, and ``ceil(-3.14)`` +## returns ``-3.0``. +## +## d: The :zeek:type:`double` to manipulate. +## +## Returns: The next lowest integer of *d* as :zeek:type:`double`. +## +## .. zeek:see:: floor sqrt exp ln log2 log10 pow +function ceil%(d: double%): double + %{ + return zeek::make_intrusive(ceil(d)); + %} + ## Computes the square root of a :zeek:type:`double`. ## ## x: The number to compute the square root of. ## ## Returns: The square root of *x*. ## -## .. zeek:see:: floor exp ln log10 pow +## .. zeek:see:: floor ceil exp ln log2 log10 pow function sqrt%(x: double%): double %{ if ( x < 0 ) @@ -1710,7 +1724,7 @@ function sqrt%(x: double%): double ## ## Returns: *e* to the power of *d*. ## -## .. zeek:see:: floor sqrt ln log10 pow +## .. zeek:see:: floor ceil sqrt ln log2 log10 pow function exp%(d: double%): double %{ return zeek::make_intrusive(exp(d)); @@ -1722,19 +1736,31 @@ function exp%(d: double%): double ## ## Returns: The natural logarithm of *d*. ## -## .. zeek:see:: exp floor sqrt log10 pow +## .. zeek:see:: floor ceil sqrt exp log2 log10 pow function ln%(d: double%): double %{ return zeek::make_intrusive(log(d)); %} +## Computes the base 2 logarithm of a number. +## +## d: The argument to the logarithm. +## +## Returns: The base 2 logarithm of *d*. +## +## .. zeek:see:: floor ceil sqrt exp ln log10 pow +function log2%(d: double%): double + %{ + return zeek::make_intrusive(log2(d)); + %} + ## Computes the common logarithm of a number. ## ## d: The argument to the logarithm. ## ## Returns: The common logarithm of *d*. ## -## .. zeek:see:: exp floor sqrt ln pow +## .. zeek:see:: floor ceil sqrt exp ln log2 pow function log10%(d: double%): double %{ return zeek::make_intrusive(log10(d)); @@ -1748,7 +1774,7 @@ function log10%(d: double%): double ## ## Returns: The number *x* raised to the power *y*. ## -## .. zeek:see:: exp floor sqrt ln log10 +## .. zeek:see:: floor ceil sqrt exp ln log2 log10 function pow%(x: double, y: double%): double %{ return zeek::make_intrusive(pow(x, y)); diff --git a/testing/btest/Baseline/bifs.math/out b/testing/btest/Baseline/bifs.math/out index 8960b7c859..7ba88b0911 100644 --- a/testing/btest/Baseline/bifs.math/out +++ b/testing/btest/Baseline/bifs.math/out @@ -3,8 +3,13 @@ 2.0 -4.0 -3.0 +4.0 +3.0 +-3.0 +-2.0 1.772005 23.103867 1.144223 0.49693 +1.650765 22.21669 diff --git a/testing/btest/bifs/math.zeek b/testing/btest/bifs/math.zeek index 63999b9a27..d3fe0189f6 100644 --- a/testing/btest/bifs/math.zeek +++ b/testing/btest/bifs/math.zeek @@ -14,6 +14,11 @@ event zeek_init() print floor(c); print floor(d); + print ceil(a); + print ceil(b); + print ceil(c); + print ceil(d); + print sqrt(a); print exp(a); @@ -22,5 +27,7 @@ event zeek_init() print log10(a); + print log2(a); + print pow(a, b); }