funny printf behaviour in need of explanation

D

Dov Levenglick

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);
}
 
N

news

Dov Levenglick said:
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
 
D

Dov Levenglick

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.
 
J

James E Keenan

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
 
D

Dov Levenglick

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.
 
D

Dov Levenglick

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 :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top