funny printf behaviour in need of explanation

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default funny printf behaviour in need of explanation

    Hi,
    i am attaching a snippet of code and two results that it produces. the
    first result is when the printf line is not commented out while the
    second result is when the line is commented. the code was run on a
    UNIX station running SunOS 5.8

    result #1:
    big_endian: 123456 78
    big_endian: 1234 7856
    big_endian: 12 785634
    big_endian: 0 78563412
    Result: 12345678 78563412

    result #2:
    Result: 12345678 ffffffff

    i am aware that $a reaches the value of 0 only after many divisions by
    256 (since divisions are in floating point context) and therefore
    $result reaches Infinity (hence ffffffff), my question is how and why
    does the printf change the context of $a from floating point to
    integer.

    btw, i am aware that "use integer" or $a=int($a/256)" would avoid the
    problem as well, therefore please dont offer them as an answer to my
    question.

    my $a = 0x12345678;
    $b = &big_endian($a);
    printf ("Result: %x %x\n",$a,$b);

    sub big_endian {
    my $data = shift;
    my $result = 0;
    while ($data) {
    $result *= 256;
    $result += $data%256;
    $data /= 256;
    printf "big_endian: %x %x\n",$data,$result;
    } return ($result);
    }
    Dov Levenglick Guest

  2. Similar Questions and Discussions

    1. Help with printf
      I'm working on a very simple program to output some date in a table. The last column of the table shows the prices of various items. I'd like all...
    2. printf sprintf
      What is the difference. The only I see is that printf can take a filehandle? But what use would that be. Paul Kraus ----------------------- PEL...
    3. printf in Lingo?
      Before I trundle off to write my own, I wanted to ask here if anyone had already written a full-featured Lingo port of C printf(). Christopher...
    4. Funny behaviour of perfstat_disk()...
      On a customers machine, the function call: ndisks = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0); has started returning bogus data, i.e....
    5. Funny Behaviour -- Probably Easily Answered
      I am trying to gather data from various textboxes in a webform. Upon loading, the form populates itself with values from a database. When the...
  3. #2

    Default Re: funny printf behaviour in need of explanation

    Dov Levenglick <stam_doar@hotmail.com> wrote:
    > i am attaching a snippet of code and two results that it produces
    > [... on] SunOS 5.8
    > result #1:
    > Result: 12345678 78563412
    > result #2:
    > Result: 12345678 ffffffff
    Using perl 5.005_03, as provided with Solaris 8, I too get these
    results. However, running against 5.8.0 (compiled using gcc 3.3 from
    sunfreeware.com) I get this regardless of whether the debugging printf
    is used or not:

    Result: 12345678 ffffffff

    I know this doesn't answer your actual question, but maybe installing
    5.8.0 (alongside 5.005_03) may be an option for you.

    Chris
    --
    @s=split(//,"Je,\nhn ersloak rcet thuarP");$k=$l=@s;for(;$k;$k--){$i=($i+1)%$l
    until$s[$i];$c=$s[$i];print$c;undef$s[$i];$i=($i+(ord$c))%$l}
    news@roaima.freeserve.co.uk Guest

  4. #3

    Default Re: funny printf behaviour in need of explanation

    >
    > Using perl 5.005_03, as provided with Solaris 8, I too get these
    > results. However, running against 5.8.0 (compiled using gcc 3.3 from
    > sunfreeware.com) I get this regardless of whether the debugging printf
    > is used or not:
    >
    >
    > I know this doesn't answer your actual question, but maybe installing
    > 5.8.0 (alongside 5.005_03) may be an option for you.
    >
    as a matter of fact this can point to a problem with PERL in versions
    previous to 5.8.0, since the behaviour that you observed is the
    expected one.

    if anyone else, however, has insights to the cause of this peculiar
    behaviour, i would love to hear about it.
    Dov Levenglick Guest

  5. #4

    Default Re: funny printf behaviour in need of explanation

    [email]stam_doar@hotmail.com[/email] (Dov Levenglick) wrote in message news:<8ebf3385.0307010520.5ff0e1e4@posting.google. com>...
    > Hi,
    > i am attaching a snippet of code and two results that it produces. the
    > first result is when the printf line is not commented out while the
    > second result is when the line is commented. the code was run on a
    > UNIX station running SunOS 5.8
    >
    > result #1:
    > big_endian: 123456 78
    > big_endian: 1234 7856
    > big_endian: 12 785634
    > big_endian: 0 78563412
    > Result: 12345678 78563412
    >
    > result #2:
    > Result: 12345678 ffffffff
    >
    > i am aware that $a reaches the value of 0 only after many divisions by
    > 256 (since divisions are in floating point context) and therefore
    > $result reaches Infinity (hence ffffffff), my question is how and why
    > does the printf change the context of $a from floating point to
    > integer.
    >
    > btw, i am aware that "use integer" or $a=int($a/256)" would avoid the
    > problem as well, therefore please dont offer them as an answer to my
    > question.
    >
    > my $a = 0x12345678;
    > $b = &big_endian($a);
    > printf ("Result: %x %x\n",$a,$b);
    >
    You should not be using $a and $b as assignable variables. In Perl 5
    they are reserved for use by the sort function.

    Jim Keenan
    James E Keenan Guest

  6. #5

    Default Re: funny printf behaviour in need of explanation

    > You should not be using $a and $b as assignable variables. In Perl 5
    > they are reserved for use by the sort function.
    >
    thank you for pointing that out i wasn't aware of this. be that as it
    may, it doesn't solve the problem that i raised in the printf
    behaviour.
    Dov Levenglick Guest

  7. #6

    Default Re: funny printf behaviour in need of explanation

    > The DWIM (Do What I Mean) philosophy runs strong in perl, but that means
    > using heuristics, which can sometimes produce surprising results. As
    > mjd wrote, "Of course, this is a heuristic, which is a fancy way of
    > saying that it doesn't work."
    >
    > Hope this helps,
    > Greg
    thanks a lot greg. i wasnt aware of the module that you used for
    debugging, but you learn something new every day :)
    Dov Levenglick Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139