Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

display output larger than 65536

52 views
Skip to first unread message

itoney...@gmail.com

unread,
Dec 16, 2014, 4:34:23 PM12/16/14
to
I can't get rice to display larger than 65,536. See:

vector <int> squares;

int main()
{
signed long long int rice = 1;
for (int i = 0; i < 15; ++i)
{
squares.push_back(rice);
cout << squares[i] << endl;
if (rice == 1)
rice = 2;
else
rice = rice * rice;
}
}

Mark Blair

unread,
Dec 16, 2014, 4:40:04 PM12/16/14
to
On 12/16/2014 3:34 PM, itoney...@gmail.com wrote:
> I can't get rice to display larger than 65,536. See:
>
> vector <int> squares;

Should be:
vector <long long int> squares;

mathewji

unread,
Dec 16, 2014, 4:58:06 PM12/16/14
to
Made the change. I can't get rice to display larger than 4,294,967,296. See:

vector <signed long long int> squares;

Victor Bazarov

unread,
Dec 16, 2014, 5:07:09 PM12/16/14
to
Which means that your implementation cannot store the square of that
number in a 'long long'. Are you surprised? Why? It requires more
than 64 bits, which is what your 'long long' has, probably.

Use Google to search for "arbitrary precision integer" and use some
library, there are probably open source ones out there.

V
--
I do not respond to top-posted replies, please don't ask

Barry Schwarz

unread,
Dec 16, 2014, 5:39:57 PM12/16/14
to
On Tue, 16 Dec 2014 13:34:13 -0800 (PST), itoney...@gmail.com
wrote:
Changing squares to vector<long long> will postpone the error but not
solve the problem. To see why, take a piece of paper and write down
the value of rice for the first few values of i. (Hint: make life
easy on yourself and write the values as powers of 2.) How soon will
rice exceed the value of LLONG_MAX?

--
Remove del for email

Öö Tiib

unread,
Dec 16, 2014, 6:47:52 PM12/16/14
to
Guaranteed available range for 'int' is -32767 to 32767 (beaten at 6th iteration of your loop) and for 'long long' it is -9223372036854775807
to 9223372036854775807 (beaten at 8th iteration) you will also run out
of limits of 'double' at 12th iteration (despite C++ does not specify
it). So there you are.

C++ does not contain any arbitrary precision numeric types but you can
search web for "arbitrary precision C++". You will find several open
source implementations of various quality. Most can deal with
multiplying values that have millions of numbers.

Öö Tiib

unread,
Dec 16, 2014, 6:59:31 PM12/16/14
to
On Wednesday, 17 December 2014 01:47:52 UTC+2, Öö Tiib wrote:
> C++ does not contain any arbitrary precision numeric types but you can
> search web for "arbitrary precision C++". You will find several open
> source implementations of various quality. Most can deal with
> multiplying values that have millions of numbers.

Meant "millions of digits". Digit is "number" in Estonian so I mix
the words up now and then.

David Harmon

unread,
Dec 16, 2014, 7:59:53 PM12/16/14
to
On Tue, 16 Dec 2014 13:57:56 -0800 (PST) in comp.lang.c++, mathewji
<itoney...@gmail.com> wrote,
>Made the change. I can't get rice to display larger than 4,294,967,296. See:
>vector <signed long long int> squares;

And what is the lesson you have learned from all this?
How many bits long would a integer variable have to be
to hold your final answer?

Nobody

unread,
Dec 17, 2014, 12:38:35 AM12/17/14
to
On Tue, 16 Dec 2014 17:06:59 -0500, Victor Bazarov wrote:

> Use Google to search for "arbitrary precision integer" and use some
> library, there are probably open source ones out there.

Boost.Multiprecision:

http://www.boost.org/doc/libs/1_57_0/libs/multiprecision/doc/html/index.html

Jorgen Grahn

unread,
Dec 17, 2014, 1:59:54 AM12/17/14
to
On Tue, 2014-12-16, 嘱 Tiib wrote:
> On Wednesday, 17 December 2014 01:47:52 UTC+2, 嘱 Tiib wrote:
>> C++ does not contain any arbitrary precision numeric types but you can
>> search web for "arbitrary precision C++". You will find several open
>> source implementations of various quality. Most can deal with
>> multiplying values that have millions of numbers.
>
> Meant "millions of digits". Digit is "number" in Estonian so I mix
> the words up now and then.

We have distinct words for them in Sweden ("tal", "siffra"), but
I still mix them up ...

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Paul N

unread,
Dec 17, 2014, 10:15:39 AM12/17/14
to
I believe the answer you are actually searching for is "several times the world's entire annual rice harvest, so not a bad reward for inventing chess".

Victor Bazarov

unread,
Dec 17, 2014, 10:49:56 AM12/17/14
to
Just to make sure I understand the reference... In that tale the number
of [rice] kernels was doubled every iteration, not squared, yes?

red floyd

unread,
Dec 17, 2014, 12:46:46 PM12/17/14
to
I believe your problem lies in your loop index.
You only go up to 2**15. Why would you expect to see anything else?


Victor Bazarov

unread,
Dec 17, 2014, 1:27:57 PM12/17/14
to
Check again. Hint: he's not multiplying by 2... :-)

Scott Lurndal

unread,
Dec 17, 2014, 2:17:51 PM12/17/14
to
Problem #1: Storing a "signed long long int" in a vector of "int" will
lose precision.

Problem #2: 2^16 squared is 2^32, which won't fit in a signed "int", so
problem #1 bites such that squares[6] gets zero instead of 2^32.

Problem #3: 2^32 squared is larger than a 'signed long long int' and thus
overflow occurs on the 8th loop iteration.

Problem #4: The problem statement likely requested the squares of the first
16 positive integers, in which case "push_back((i+1)*(i+1)" was probably the
intended code and the rice variable is unnecessary.

Jorgen Grahn

unread,
Dec 17, 2014, 3:59:16 PM12/17/14
to

Vincenzo Mercuri

unread,
Dec 17, 2014, 4:44:50 PM12/17/14
to
Boost.Multiprecision provides the header-only type 'cpp_int'
for multiprecision integers:

#include <iostream>
#include <vector>
#include <boost/multiprecision/cpp_int.hpp>

using namespace boost::multiprecision;

std::vector<cpp_int> squares;

int main()
{
cpp_int rice = 1;
for(int i = 0; i < 15; ++i)
{
squares.push_back(rice);
std::cout << squares[i] << std::endl;
if(rice == 1)
rice = 2;
else
rice *= rice;
}
}

(sorry, I didn't wrap the output lines on purpose)

$./main
1
2
4
16
256
65536
4294967296
18446744073709551616
340282366920938463463374607431768211456
115792089237316195423570985008687907853269984665640564039457584007913129639936
13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596230656
1044388881413152506691752710716624382579964249047383780384233483283953907971557456848826811934997558340890106714439262837987573438185793607263236087851365277945956976543709998340361590134383718314428070011855946226376318839397712745672334684344586617496807908705803704071284048740118609114467977783598029006686938976881787785946905630190260940599579453432823469303026696443059025015972399867714215541693835559885291486318237914434496734087811872639496475100189041349008417061675093668333850551032972088269550769983616369411933015213796825837188091833656751221318492846368125550225998300412344784862595674492194617023806505913245610825731835380087608622102834270197698202313169017678006675195485079921636419370285375124784014907159135459982790513399611551794271106831134090584272884279791554849782954323534517065223269061394905987693002122963395687782878948440616007412945674919823050571642377154816321380631045902916136926708342856440730447899971901781465763473223850267253059899795996090799469201
774624817718449867455659250178329070473119433165550807568221846571746373296884912819520317457002440926616910874148385078411929804522981857338977648103126085903001302413467189726673216491511131602920781738033436090243804708340403154190336
1090748135619415929462984244733782862448264161996232692431832786189721331849119295216264234525201987223957291796157025273109870820177184063610979765077554799078906298842192989538609825228048205159696851613591638196771886542609324560121290553901886301017900252535799917200010079600026535836800905297805880952350501630195475653911005312364560014847426035293551245843928918752768696279344088055617515694349945406677825140814900616105920256438504578013326493565836047242407382442812245131517757519164899226365743722432277368075027627883045206501792761700945699168497257879683851737049996900961120515655050115561271491492515342105748966629547032786321505730828430221664970324396138635251626409516168005427623435996308921691446181187406395310665404885739434832877428167407495370993511868756359970390117021823616749458620969857006263612082706715408157066575137281027022310927564910276759160520878304632411049364568754920967322982459184763427383790272448438018526977764941072715611580434690827459339991961
41424274141059911742606055648376375631452761136265862838336862115799363802087853767554533678991569423443395566631507008721353547025567031200413072549583450835743965382893607708097855057891296790735278005493562156109079584517295411597292747987752773856000820411855893000477774872776185381351049384058186159865221160596030835640594182118971403786872621948149872760365361629885617482241303348543878532402475141941718301228107820972930353737280457437209522870362277636394529086980625842235514850757103961938744962986680818876966281577815307939317909314364834076173858181956300299442279075495506128881830843007964869323217915876591803556521615711540299212027615560787310793747746684152836298770869945015203123186259420308569383894465706134623670423402682110295895495119708707654618662279629453645162075650935101890602377382153953277620867697858973196633030889330466516943618507835064156833694453005143749131129883436726523859540490427345592872394952522718461740436785475461047437701976802557660588103807
7270707717942221977090385438585844095492116099852538903974655703943973086090930596963360767529964938414598185705963754561497355827813623833288906309004288017321424808663962671333528009232758350873059614118723781422101460198615747386855096896089189180441339558524822867541113212638793675567650340362970031930023397828465318547238244232028015189689660418822976000815437610652254270163595650875433851147123214227266605403581781469090806576468950587661997186505665475715792896

--
Vincenzo Mercuri

red floyd

unread,
Dec 17, 2014, 5:13:45 PM12/17/14
to
Oops. You're right, he's doing 2**(2n)



red floyd

unread,
Dec 17, 2014, 5:15:57 PM12/17/14
to
Oops. 2**(2**n)


0 new messages