develooper Front page | perl.perl5.porters | Postings from October 2018

[perl #133582] POSIX::log2 weird behaviour

Thread Previous | Thread Next
From:
sisyphus@cpan.org via RT
Date:
October 15, 2018 23:31
Subject:
[perl #133582] POSIX::log2 weird behaviour
Message ID:
rt-4.0.24-2837-1539646256-1181.133582-15-0@perl.org
On Mon, 15 Oct 2018 08:27:01 -0700, hmbrand wrote:

> I can confirm, but it is a Windows-issue, or a Strawberry issue

It's a perl issue that applies only to builds of perl on Windows.
For windows, rather than utilizing gcc's libmath implementation of log2, perl is using its own implementation that differs slightly.

To get a correct implementation of log2 on your current build of Strawberry Perl, you could use Inline::C:

##############################
use warnings;
use strict;

use Inline C => <<'EOC';


double libm_log2(double op) {
  return log2(op);
}

EOC

print int libm_log2(64); # 6
##############################

Short of patching the perl source and rebuilding perl, I think that's the simplest way for you to get the correct implementation.
You already have Inline::C as it ships with Strawberry Perl.

Fixing the perl source so that POSIX::log2 works correctly is not particularly difficult. It should (untested) be as simple as including the defines pertaining to "log2" that are currently present (but commented out) in win32/config.gc and win32/config_H.gc.

But it looks to me that there's other functions requiring similar treatment.
For example, POSIX::cbrt is also not using the libm implementation and suffers some inaccuracy as a result:

##############################
use warnings;
use strict;
use POSIX;

use Inline C => <<'EOC';


double libm_cbrt(double op) {
  return cbrt(op);
}

EOC

my $x = 53.6;
printf " %.16e\n %.16e\n",
       libm_cbrt($x),
       POSIX::cbrt($x);

printf " %.16e\n",
       libm_cbrt($x) ** 3;

printf " %.16e\n",
       POSIX::cbrt($x) ** 3;
##############################

And there is a large number of other libm functions being ignored in POSIX on Windows builds.
Installing Math::Libm might be a good workaround for  many of them - though that module has not been updated for 18 years and it seems not to include log2.

Incidentally, on Ubuntu-18.04, perl-5.29.3, POSIX::cbrt() fails to provide the best approximation of cbrt(53.6).
It's giving me 3.7704072620419726 whereas the most accurate representation is 3.7704072620419722 (as provided by my Windows libm implementation of cbrt).
Something's apparently a bit bodgey in that Ubuntu libm implementation of cbrt().

Cheers,
Rob

---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=133582

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About