... %.{$var}f doesn't do any trick except to produce [a] ... warning.
works for me in ... perl 5, version 20 ... Doesn't throw any warnings.

I'm surprised to read that it works in 5.20. I don't see any mention of a new format specifier in the latest sprintf docs. The best I can test in ATM is 5.14:

c:\@Work\Perl\monks>perl -wMstrict -le "print 'perl version: ', $]; ;; my $p = 30; my $n = 1.111111111111111111111111111111; ;; printf qq{%0.{$p}f}, $n; " perl version: 5.014004 Invalid conversion in printf: "%0.{" at -e line 1. %0.{30}f

... I was surprised that the data changed ...

As to this, I'm sure that, having now read the docs, you realize that IEEE-754 floating point representation (or any other finite representation) is only an approximation that may sometimes happen to be exact, and that it handles base-2 relative-prime fractions no better than base-10 handles 1/3.


Give a man a fish:  <%-{-{-{-<


In reply to Re^3: Why do I get random numbers? by AnomalousMonk
in thread Why do I get random numbers? by Cleggy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.