473,387 Members | 1,553 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,387 software developers and data experts.

why plus can be minus. Another of Microsoft's trick?

What could be a simpler snippet than this

#include <iostream>
using std::cout;
using std::endl;

int main() {

long nx = 200L;
long ny = 300L;
long gridSize = 0L;
long var = 0L;

gridSize = nx*ny;
var = gridSize*gridSize;
cout<<gridSize<<" "<<var<<endl;

system("pause");
return 0;
}

When executed, the above streams out 60000 for gridSize and -694967296 for
var !!!

Tried on a few machine, still same. What's going on here.

Jul 22 '05 #1
5 1093
claude uq wrote:
long nx = 200L;
long ny = 300L;
long gridSize = 0L;
long var = 0L;

gridSize = nx*ny;
var = gridSize*gridSize;
cout<<gridSize<<" "<<var<<endl;

system("pause");
return 0;
}

When executed, the above streams out 60000 for gridSize and -694967296 for
var !!!

Tried on a few machine, still same. What's going on here.


Hit <Logo+R>calc<Enter> to raise the MS Windows Calculator. Select
View->Scientific. Enter 60000. Hit the square button (x^2). The result
(formatted for clarity) is 3 600 000 000.

Now switch to Hex mode with the radio button "Hex". Enter 7fffffff - which
is the maximum value of a signed 32-bit long integer. Switch the Mode back
to Dec. The number converts to 2 147 483 647. That's less than 3.6 billion.

Blaming MS is fun, but you over-flowed the long. The system ran out of bits
to store the entire number, and simply shoved the lowest 32 into a register.
The highest bit was set, so the number appears negative.

C++ code runs very lean and efficient because it passes the question what to
do at integer overflow time back to the programmer. Most of the time the
answer to this question is nothing, because we either count natural numbers
of whole things in user-comprehensible units, or we use 'double' to store
the huge numbers.

--
Phlip
Jul 22 '05 #2

"Phlip" <ph*******@yahoo.com> wrote in message
news:LD*****************@newssvr33.news.prodigy.co m...
claude uq wrote:
long nx = 200L;
long ny = 300L;
long gridSize = 0L;
long var = 0L;

gridSize = nx*ny;
var = gridSize*gridSize;
cout<<gridSize<<" "<<var<<endl;

system("pause");
return 0;
}

When executed, the above streams out 60000 for gridSize and -694967296 for var !!!

Tried on a few machine, still same. What's going on here.
Hit <Logo+R>calc<Enter> to raise the MS Windows Calculator. Select
View->Scientific. Enter 60000. Hit the square button (x^2). The result
(formatted for clarity) is 3 600 000 000.

Now switch to Hex mode with the radio button "Hex". Enter 7fffffff - which
is the maximum value of a signed 32-bit long integer. Switch the Mode back
to Dec. The number converts to 2 147 483 647. That's less than 3.6

billion.
Blaming MS is fun, but you over-flowed the long. The system ran out of bits to store the entire number, and simply shoved the lowest 32 into a register. The highest bit was set, so the number appears negative.

C++ code runs very lean and efficient because it passes the question what to do at integer overflow time back to the programmer. Most of the time the
answer to this question is nothing, because we either count natural numbers of whole things in user-comprehensible units, or we use 'double' to store
the huge numbers.

--
Phlip


Well, thanks for this. I was aware that the type long has a range of about
+- 2 billions .

In my stupide head, 60000 square was below 2 billions. Mea Culpa and yes,
sorry Bill:-)
Jul 22 '05 #3
> Well, thanks for this. I was aware that the type long has a range of about
+- 2 billions .

In my stupide head, 60000 square was below 2 billions. Mea Culpa and yes,
sorry Bill:-)


What about "unsigned long"? It's like 4bil something max... And as I
can see you don't need negative values...

cmad
Jul 22 '05 #4
"Chris Mantoulidis" <cm****@yahoo.com> wrote in message
news:a8**************************@posting.google.c om
Well, thanks for this. I was aware that the type long has a range
of about +- 2 billions .

In my stupide head, 60000 square was below 2 billions. Mea Culpa
and yes, sorry Bill:-)


What about "unsigned long"? It's like 4bil something max... And as I
can see you don't need negative values...

cmad

The supplied code used long, not unsigned long. If you change the longs to
unsigned long, then the code does indeed work.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #5
John Carson wrote:
The supplied code used long, not unsigned long. If you change the longs to
unsigned long, then the code does indeed work.


But it would still overflow my sane subset. Instead of numbers that might
get anywhere near their limits I would either use a 'double' or a BigInt
(Google for the latter - it's not bundled with C++).

--
Phlip
Jul 22 '05 #6

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

Similar topics

3
by: Carlos Ribeiro | last post by:
I was checking the Prolog recipe in the Cookbook: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303057 It's a clever implementation that explores some aspects of Python that I wasn't...
6
by: RobG | last post by:
I am writing a script to move an absolutely positioned element on a page by a factor using style.top & style.left. The amount to move by is always some fraction, so I was tossing up between...
3
by: Tomas R | last post by:
Really weird. The ordinary plus/minus works, but not the ones on the numpad?! What I've bound to the datagrid is a DataView with a few columns having numeric values with datatype Int32. (Comming...
0
by: giannik | last post by:
How can I change the plus/minus icon that appears in treeview control to set my custom images to display? Any third party controls that provide this feature?
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
1
by: jmdolinger | last post by:
Hi all, Has anyone ever seen the following Javascript problem (in IE). I'm trying to set up a hash of rating values to integers (which I'll then use to sort an array of ratings). It looks like...
2
by: Marc | last post by:
I took a sample json string from: http://www.json.org/example.html {"web-app": { "servlet": .servlet-name); the minus in 'web-app' is giving problems. The toJSONString and parseJSON functions...
4
by: tamnguyet | last post by:
I have n days and a date, I want a date plus(or minus) to n days.How can I do that in javascript
2
by: pbd22 | last post by:
Hi. Can somebody tell me how to prevent a postback when I click on the little "plus" sign next to the treenode's root folder? It postbacks and I have to restart a video stream coming to the...
4
by: michael | last post by:
Hello everybody, I've created for one of my classes a exception class derivated from System.Exception. This exception class is public and lies within my class as inner class. When I throw...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.