New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
printf uses wrong cached number #15273
Comments
From rehsack@gmail.comCreated by rehsack@gmail.comI play around with number representation to develop/prove a reasonable test for L::MU's minmax function. One of my tests was $ perl -le 'my $x = ~0; printf("%u\n", ++$x)' Seems that printf is using the values stored in STRUCT_SV.sv_u.svu_uv without proving (or maybe the check is broken) whether it's an IVUV or not or the documentation of sprintf is broken. Perl Info
|
From zefram@fysh.orgJens Rehsack wrote:
This looks fine to me. Your ~0 yields a UV of value 2**64-1, the ++ -zefram |
The RT System itself - Status changed from 'new' to 'open' |
From rehsack@gmail.com
I would expect either 0 or NaN or whatever - but not a wrong result. Cheers |
From zefram@fysh.orgJens Rehsack wrote:
0 would be an equally wrong result. NaN is impossible to get, because -zefram |
From @LeontOn Wed, Apr 13, 2016 at 7:35 PM, Jens Rehsack <rehsack@gmail.com> wrote:
There really isn't any correct answer there. I would support warning in Leon |
From rehsack@gmail.com
But a simple, stand alone '-' or '.' would be.
It is never necessary to produce a wrong result. Maybe you meant something
That's not true. printf can be disadvised, the documentation can be updated Cheers |
From zefram@fysh.orgJens Rehsack wrote:
"-" and "." are equally unrepresentable as UV. You've got 64 bits to You may of course argue that this rather low-level data type (copied
Given the input you supplied, there is no `right' result available.
perlnumber(1) describes the behaviour for conversions where the input -zefram |
From rehsack@gmail.com
Sure - I simply tried to say, it would be possible to represent an
Especially such a hint in printf documentation would be very helpful. Cheers |
From @kentfredricOn 14 April 2016 at 06:02, Jens Rehsack <rehsack@gmail.com> wrote:
I should point out that you can get a different behaviour if you don't perl -Mbigint -lE ' my $x = ~0; print(++$x) ' -- KENTNL - https://metacpan.org/author/KENTNL |
From rehsack@gmail.com
I should try to explain my motivation filling a ticket: I tried something very common for me and got a very surprising result. But I didn't find an explanation for the surprise within the typical 20 I think, it is important to explain such surprises. So a warn as Leon Cheers |
From @arcJens Rehsack <rehsack@gmail.com> wrote:
I am fairly strongly unconvinced that a warning is appropriate in I'm less sure about whether it's a good idea to extend the sprintf Note that you may get surprising results if the value to be However, I don't plan to apply that patch unless this idea receives -- |
From @arc0001-perl-127887-sprintf-doc-note-for-out-of-range-intege.patchFrom 632c43423fed670e4d9f83daf5f691f5e185dab7 Mon Sep 17 00:00:00 2001
From: Aaron Crane <arc@cpan.org>
Date: Sun, 8 May 2016 18:48:28 +0100
Subject: [PATCH] [perl #127887] sprintf doc note for out-of-range integers
If you ask for an integer conversion, sprintf does the best it can to honour
that; if that's impossible, the results may be surprising.
---
pod/perlfunc.pod | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod
index e9c7038..b0d2685 100644
--- a/pod/perlfunc.pod
+++ b/pod/perlfunc.pod
@@ -7850,6 +7850,12 @@ You can find out whether your Perl supports quads via L<Config>:
print "Nice quads!\n";
}
+Note that you may get surprising results if the value to be formatted by one
+of the integer conversions is outside the range of the relevant C-level
+type. For example, C<sprintf '%u', 1 + ~0> and C<sprintf '%u', ~0> will
+produce the same output, because the result of C<1 + ~0> will not fit into
+the underlying C-level type.
+
For floating-point conversions (C<e f g E F G>), numbers are usually assumed
to be the default floating-point size on your platform (double or long double),
but you can force "long double" with C<q>, C<L>, or C<ll> if your
--
2.7.4
|
From rehsack@gmail.com
And because it's still 1985, what is must stay :P Every language outside improved over time - and even K&R compatible $ cat precision.c int main() I just want to clarify: "It never had so never will" isn't an argument
Cheers |
From @sisyphus-----Original Message-----
But that's a compile time warning that will be generated irrespective of the printf("%u\n", (double)0); For perl, AFAICS, the closest we could get to that is to have a runtime In the meantime, I'm personally not too distressed about perl just silently Cheers, |
From rehsack@gmail.com
Precisely, because it has strict typing. Perl has dynamic typing, which causes the above described behavior ...
Yes
As maintainer of several modules I receive requests reporting those silent automatisms are surprising to a lot of people. Cheers |
From zefram@fysh.orgThe analogy with the C warning is bogus: C isn't warning about I'm unenthusiastic about the proposed doc patch. The sprintf() This ticket should be closed. -zefram |
From rehsack@gmail.com
I scanned (not studied in terms of: reading every sentence and try to
I don't think so. People complain and complain about the bad standing of perl on OSS I do not (really) ask for modifying behavior of Perl, I ask for And - if this is not imaginable - document it very loud. Mind that Cheers |
Migrated from rt.perl.org#127887 (status was 'open')
Searchable as RT127887$
The text was updated successfully, but these errors were encountered: