• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is my multiplication going negative?  RSS feed

 
Greg Zobel
Greenhorn
Posts: 23
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy! folks,

I'm working on the problem found here: http://www.codeabbey.com/index/task_view/modular-calculator

How is my answer going negative when the only operations are addition and multiplication?

My code is



testData is

Results are

Answer = 7533
Answer * 7 = 52731
Answer * 8039 = 423904509
Answer * 51 = 21619129959
Answer + 4 = 21619129963
Answer + 6076 = 21619136039
Answer + 2 = 21619136041
Answer * 886 = 19154554532326
Answer * 2 = 38309109064652
Answer * 3483 = 133430626872182916
Answer * 12 = 1601167522466194992
Answer * 4 = 6404670089864779968
Answer + 32 = 6404670089864780000
Answer * 7890 = 7214991142652323776
Answer + 9483 = 7214991142652333259
Answer * 9304 = 575907008250311112
Answer * 935 = 3517474576463892856
Answer * 52 = -1558762760973087648
...
Answer + 6859 = -5982303519648207657
Answer % 7872 = -7017
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Zobel wrote:
How is my answer going negative when the only operations are addition and multiplication?


A long variable is a 64 bit variable, and the largest number that it can hold (supporting twos complement format) is 9223372036854775807. If you go larger than that, the variable will overflow... ie. adding one to the largest possible number will yield the smallest (largest negative) possible number.

Henry
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Greg Zobel wrote:
How is my answer going negative when the only operations are addition and multiplication?


A long variable is a 64 bit variable, and the largest number that it can hold (supporting twos complement format) is 9223372036854775807. If you go larger than that, the variable will overflow... ie. adding one to the largest possible number will yield the smallest (largest negative) possible number.

Henry


Additionally, you may have a look at the class java.lang.BigInteger which has no theoretical limit on the size of the integer it can represent. Similarly, java.math.BigDecimal is used in accounting software because it has no theoretical limit on the precision as well as on size while double and float have limits on the precision that cause the books not to balance in some cases.
 
Greg Zobel
Greenhorn
Posts: 23
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is how I made it work:

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Zobel wrote:So this is how I made it work:


Thanks for returning to report on your solution ... you earned a cow.

Henry
 
Greg Zobel
Greenhorn
Posts: 23
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Greg Zobel wrote:So this is how I made it work:


Thanks for returning to report on your solution ... you earned a cow.

Henry


Thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!