posted 2 years ago

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

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

java.time.PastBedtimeException: Get some sleep and try again tomorrow.

posted 2 years ago

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

- 1

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

posted 2 years ago

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.

- 1

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.

I hadn't been here for fifteen years

posted 2 years ago

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

Henry

Greg Zobel wrote:So this is how I made it work:

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

Henry