Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^8: Why am I losing accuracy?

by haukex (Archbishop)
on Jan 12, 2020 at 15:08 UTC ( [id://11111340]=note: print w/replies, xml ) Need Help??


in reply to Re^7: Why am I losing accuracy?
in thread Why am I losing accuracy?

Fair enough it's a design choice but the unawary, common-sense Perl user is in for a big surprise when trying to ... increase precision.

I'm a little confused (about the whole subthread, actually, as well as this one) - isn't p=>1 explicitly asking for reduced precision?

Replies are listed 'Best First'.
Re^9: Why am I losing accuracy?
by roboticus (Chancellor) on Jan 12, 2020 at 15:56 UTC

    haukex:

    Yes, it's asking for reduced precision, but I think that bliako is surprised as I am by the sign of the precision argument. The various number formatting tools I've used have given me the expectation that a positive number indicates the number of digits I want to retain to the right of the decimal place, rather than to the left.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      yes, for me the sign of precision is confusing.

      I am also confused as to what precision and what accuracy mean for bignum and why in incrementing an integer (for example, in the loop) precision discards useful digits to replace them with zeros.

      What perl -Mbignum=p,1 -wle '$i = 1; print $i + 123456788,"\n"' does is round((1+123456788)/10) * 10 =  123456790 Why? Just add the numbers please? At least when the "width" of the largest can accommodate the result too. Instead it discards the LS"D" of the result and replaces it with a zero. The more increasing p the more LS"D"s are thrown away but the width of the result is not getting any smaller, so what's the practical reason?

      From roboticus home node I clicked on dominus home node and then on to his blog which happens to talk about bc just a few days ago. And points out that bc adding two numbers increases the number of digits to accommodate the result. Well bignum does that and then replaces "p" LS"D"s with zeros. thanks.

      edit: LS"D" = least significant digit

        I am also confused as to what precision and what accuracy mean for bignum and why in incrementing an integer (for example, in the loop) precision discards useful digits to replace them with zeros

        When you specify p => 1 you're actually saying that you want the values rounded to the nearest multiple of 10.
        To have the values rounded to the nearest multiple of 100, specify p => 2. For rounding to the nearest multiple of 1000, specify p => 3, and so on.
        So, with p => 1, 10 + 1 == 10 and 10 + 6 == 20, because 11 rounded to the nearest multiple of 10, is 10 and 16 rounded to the nearest multiple of 10 is 20.

        OTOH, a => 1 indicates that you want the value rounded to a value that comprises a single digit multiplied by a power of 10, a => 2 indicates that the value should be rounded to a double-digit value multiplied by a power of 10.
        It seems to me that "accuracy" would be better named "precision". (The trailing zeroes are just the exponent - it's the number of leading digits ahead of the exponent that determines the precision of the value.)
        Not sure what "precision" should be renamed to .... "accuracy" ?

        I think negative values for "p" just tell you how many decimal places you want after the decimal point.

        With practice and experience it's probably useful - if you keep your wits about you. (I have not tested this - the reliance on "wits" deters me.)

        Cheers,
        Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11111340]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-16 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found