Connecting Tech Pros Worldwide Help | Site Map

Apparently inconsistent arithmetic calculation

  #1  
Old January 27th, 2007, 06:25 PM
robert maas, see http://tinyurl.com/uh3t
Guest
 
Posts: n/a
(second attempt)
I'm just a beginner at perl. Currently I'm comparing how integers are
supported in several different programming languages:
<http://www.rawbw.com/~rem/HelloPlus/CookBook/CookTop.html#int>
I wrote a test program in perl, but it gives inconsistent results.
Here's the program source:

#!/usr/bin/perl
$a = 'Hello';
$b = 'world.';
$all = "$a $b";
print "$all\n"; # Print a message
$n = 987654321;
print "n = $n\n";
$k = 10000000000000000000000000000000000000000000000000 00000000000000000000;
$n = $n * $k;
print "n = $n\n";
$k = $k * $k;
$n = $n * $k;
print "n = $n\n";
$n1 = $n * $k;
print "n1 = $n1\n";
$k = 10000000000000000000000000000000000000000000000000 00000000000000000000;
$n1 = $n * $k;
print "n1 = $n1\n";

and here's the program output:

Hello world.
n = 987654321
n = 9.87654321e+77
n = 9.87654321e+215
n1 = Inf
n1 = 9.87654321e+284

Notice the two printings of n1. The first time I multiply the value
of n times the value of k that I've been using all along, and the
result is infinity. The second time, I re-assign the value of k to
identical value as it had before, and do the same multiplication as
before, but now the result isn't infinity. What's going on??? This
doesn't make sense to me.

If it makes any difference:
perl -v
This is perl, v5.8.0 built for i386-freebsd
  #2  
Old January 27th, 2007, 11:15 PM
Joe Smith
Guest
 
Posts: n/a

re: Apparently inconsistent arithmetic calculation


robert maas, see http://tinyurl.com/uh3t wrote:
Quote:
$n = 987654321;
print "n = $n\n";
$k = 10000000000000000000000000000000000000000000000000 00000000000000000000;
$n = $n * $k;
print "n = $n\n";
$k = $k * $k;
$n = $n * $k;
print "n = $n\n";
$n1 = $n * $k;
print "n1 = $n1\n";
$k = 10000000000000000000000000000000000000000000000000 00000000000000000000;
$n1 = $n * $k;
print "n1 = $n1\n";
>
and here's the program output:
>
Hello world.
n = 987654321
n = 9.87654321e+77
n = 9.87654321e+215
n1 = Inf
n1 = 9.87654321e+284
>
Notice the two printings of n1. The first time I multiply the value
of n times the value of k that I've been using all along,
No, you were not using the same value of k all along.
It's obvious if you print out the value of k along with n1.
The first n1 is based on k**2 (1e+138).
The second n1 is based on k (1e+69).

-Joe
  #3  
Old January 28th, 2007, 12:05 AM
robert maas, see http://tinyurl.com/uh3t
Guest
 
Posts: n/a

re: Apparently inconsistent arithmetic calculation


From: Joe Smith <j...@inwap.com>
Quote:
The first n1 is based on k**2 (1e+138).
The second n1 is based on k (1e+69).
Oops. Thanks for pointing that out. Now back to experimentation...
new program source, being more careful:

$a = 'Hello';
$b = 'world.';
$all = "$a $b";
print "$all\n"; # Print a message
$n = 987654321;
print "n = $n\n";
$k1 = 10000000000000000000000000000000000000000000000000 00000000000000000000;
$n = $n * $k1;
print "n = $n\n";
$k2 = $k1 * $k1;
$n = $n * $k2;
print "n = $n\n";
$n2 = $n * $k2;
print "n2 = $n2\n";
$n1 = $n * $k1;
print "n1 = $n1\n";
$k3 = 100000000000000000000000;
$n3 = $n1 * $k3;
print "n3 = $n3\n";
$k4 = 1000000000000000000000000;
$n4 = $n1 * $k4;
print "n4 = $n4\n";
$n4 = $n3 * 10;
print "n4 = $n4\n";

New output, looks fine, thanks!

n = 987654321
n = 9.87654321e+77
n = 9.87654321e+215
n2 = Inf
n1 = 9.87654321e+284
n3 = 9.87654321e+307
n4 = Inf
n4 = Inf
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 04:15 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM