integer fail ...

Discussion in 'Perl Misc' started by stratus, Jul 28, 2005.

  1. stratus

    stratus Guest

    $input_startx=223.0000;
    $input_starty=221.0000;

    $input_endx=2392.2000;
    $input_endy=2484.2000;

    $input_step=0.8;

    $total=$input_endy-$input_starty;
    $track =$total/$input_step;
    print "...$track....\n"; ====================> 2829
    $track=int $track;
    print "...$track....\n"; ====================> 2828

    Why ?????? 2829 changed to 2828 ???
    stratus, Jul 28, 2005
    #1
    1. Advertising

  2. stratus wrote:
    [...]
    > $input_step=0.8;

    [...]
    > Why ?????? 2829 changed to 2828 ???


    You ignored the first law of computer numerics: "Thou shalt not use floating
    point for precision computations."
    See "perldoc -q 9999"

    jue
    Jürgen Exner, Jul 28, 2005
    #2
    1. Advertising

  3. stratus

    Sisyphus Guest

    "stratus" <> wrote in message
    news:dc9jqa$2prt$...
    > $input_startx=223.0000;
    > $input_starty=221.0000;
    >
    > $input_endx=2392.2000;
    > $input_endy=2484.2000;
    >
    > $input_step=0.8;
    >
    > $total=$input_endy-$input_starty;
    > $track =$total/$input_step;
    > print "...$track....\n"; ====================> 2829
    > $track=int $track;
    > print "...$track....\n"; ====================> 2828
    >
    > Why ?????? 2829 changed to 2828 ???
    >
    >


    To (partly) see why:

    use warnings;
    use Devel::peek;

    $input_starty=221.0000;

    $input_endy=2484.2000;

    $input_step=0.8;

    $total=$input_endy-$input_starty;
    $track =$total/$input_step;

    print "############\n";
    Dump($track);
    print "############\n";

    print "...$track....\n";
    $track=int $track;

    print "############\n";
    Dump($track);
    print "############\n";

    print "...$track....\n";

    __END__

    Which produces for me:

    ############
    SV = NV(0x8b09bc) at 0x8c07c8
    REFCNT = 1
    FLAGS = (NOK,pNOK)
    NV = 2829
    ############
    ....2829....
    ############
    SV = PVNV(0x3f8704) at 0x8c07c8
    REFCNT = 1
    FLAGS = (IOK,pIOK)
    IV = 2828
    NV = 2829
    PV = 0x89304c "2829"\0
    CUR = 4
    LEN = 35
    ############
    ....2828....

    The first time $track was printed, the NOK flag was set - so it printed the
    value contained in the NV slot.
    The second time $track was printed, the IOK flag was set (because the int
    function had been called) - so it printed the value contained in the IV
    slot.

    Of course the above doesn't demonstrate how the IV slot came to contain
    2828. Does someone know of a way of providing such a demonstration ?

    (We can surmise that the reason the IV slot contains 2828 is that the int()
    function received a value something like 2828.99999.... .
    But it would be so much more conclusive if there were a way to actually
    *demonstrate* that that's what happened.)

    Cheers,
    Rob
    Sisyphus, Jul 28, 2005
    #3
  4. stratus <> wrote:
    > $input_startx=223.0000;
    > $input_starty=221.0000;
    >
    > $input_endx=2392.2000;
    > $input_endy=2484.2000;
    >
    > $input_step=0.8;
    >
    > $total=$input_endy-$input_starty;
    > $track =$total/$input_step;
    > print "...$track....\n"; ====================> 2829
    > $track=int $track;
    > print "...$track....\n"; ====================> 2828



    change your print() statements to:

    printf "...%30.20f....\n", $track;

    ....

    > Why ?????? 2829 changed to 2828 ???


    .... and you will see why. :)



    Then see the answer to your Frequently Asked Question:

    Why am I getting long decimals (eg, 19.9499999999999) instead of the
    numbers I should be getting (eg, 19.95)?


    And you might also benefit from this FAQ:

    Does Perl have a round() function? ...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jul 28, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    CType(x,Integer) vs. Integer.Parse(x)

    =?Utf-8?B?Sm9l?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,949
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Feb 7, 2006
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    No Math.min(Integer, Integer)?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 29, 2003, in forum: Java
    Replies:
    0
    Views:
    502
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Jul 29, 2003
  3. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    2
    Views:
    490
    Yu SONG
    Oct 15, 2004
  4. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    6
    Views:
    45,405
    JavaBean2010
    Apr 7, 2010
  5. Wenjie

    if (f() != FAIL) or if (FAIL != f())?

    Wenjie, Jul 28, 2003, in forum: C Programming
    Replies:
    3
    Views:
    441
    E. Robert Tisdale
    Jul 31, 2003
Loading...

Share This Page