473,395 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Apparently inconsistent arithmetic calculation

(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
Jan 27 '07 #1
2 2576
robert maas, see http://tinyurl.com/uh3t wrote:
$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
Jan 27 '07 #2
From: Joe Smith <j...@inwap.com>
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
Jan 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gil | last post by:
I run a webserver on Win2k Server and program in vb.net. I am trying to perform a calculation that results in two weeks of data given any selected date that starts with the Sunday of the...
3
by: Madan | last post by:
Hi all, I had problem regarding float/double arithmetic only with + and - operations, which gives inaccurate precisions. I would like to know how the arithmetic operations are internally handled...
10
by: Larry Woods | last post by:
Look at this code: Dim i As Integer = 0 Dim j As Integer = 5 If (j / i = 7) Or i = 0 Then MessageBox.Show("In If") Else MessageBox.Show("In Else") End If
3
by: John C Kirk | last post by:
I've come across an odd situation, where doing a floating point division produces different results for the same numbers. Basically, there are 4 ways to run this application: A) Debug build,...
8
by: Lionel | last post by:
Hi all, I'm facing a strang issue doing calculation on Double values. I read some thread on the newsgroups warning about type translations (eg Single to Double), but here I'm doing no conversion...
7
by: Hallvard B Furuseth | last post by:
I'm trying to clean up a program which does arithmetic on text file positions, and also reads text files in binary mode. I can't easily get rid of it all, so I'm wondering which of the following...
3
by: Rahul Babbar | last post by:
Hi All, When could be the possible reasons that could make a database inconsistent? I was told by somebody that it could become inconsistent if you " force all the applications to a close on...
0
by: johnthawk | last post by:
In the below code setting cell to inconsistent sets entire column inconsistent (renderer).However, I need a third state off | on | inconsistent . How can I change one path->cell? Any help...
10
by: Why Tea | last post by:
I understood that the CPU registers determine the size of the machine. But what is the correct way to code an arithmetic operation to avoid wrap-around when the code is to be run on both 32-bit and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.