Ask a Question related to PERL Miscellaneous, Design and Development.
-
Dov Levenglick #1
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
-
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... -
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... -
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... -
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.... -
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... -
news@roaima.freeserve.co.uk #2
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 78563412Using perl 5.005_03, as provided with Solaris 8, I too get these> result #2:
> Result: 12345678 ffffffff
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
-
Dov Levenglick #3
Re: funny printf behaviour in need of explanation
>
as a matter of fact this can point to a problem with PERL in versions> 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.
>
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
-
James E Keenan #4
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>...
You should not be using $a and $b as assignable variables. In Perl 5> 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);
>
they are reserved for use by the sort function.
Jim Keenan
James E Keenan Guest
-
Dov Levenglick #5
Re: funny printf behaviour in need of explanation
> You should not be using $a and $b as assignable variables. In Perl 5
thank you for pointing that out i wasn't aware of this. be that as it> they are reserved for use by the sort function.
>
may, it doesn't solve the problem that i raised in the printf
behaviour.
Dov Levenglick Guest
-
Dov Levenglick #6
Re: funny printf behaviour in need of explanation
> The DWIM (Do What I Mean) philosophy runs strong in perl, but that means
thanks a lot greg. i wasnt aware of the module that you used for> 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
debugging, but you learn something new every day :)
Dov Levenglick Guest



Reply With Quote

