Need calculations without scientific notation

Status
Not open for further replies.

prateek_k_chd

Member level 5
Joined
Sep 25, 2009
Messages
87
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,288
Visit site
Activity points
1,988
Hello Gents & Ladies,

I need to calculate x=2^n
where n goes from 0 to 319 in interval of 1.

I tried MATLAB etc but they all start converting the result to exponential form whereas I need the result to be accurate upto unit position.

Can any wise person suggest a way/software to calculate this ( and preferably do it for all values of n so that I don't have to manually calculate for each) ?

Your time & thoughts are much appreciated.
 

I don't know about Matlab, but if you use Excel you can format your numbers any way you'd like.
 

Excel does not do multiple precision arithmetic which is needed here.

Save the following in a file on a unix like box, make it executable and it should do what you want:
Code:
#!/usr/bin/bc
for(i=0; i < 319; i++){
        2^i
}
quit

Note that bc splits lines longer then 70 characters with a '\', easy to sort if needed.

man bc for more details on the language.

Regards, Dan.
 
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
Just to echo what Dan has said, this is not trivial in general and you'll need arbitrary precision arithmetic.

Having said that... you mention you want precision up to the unit position, but you don't specify that this has to be base 10. Of course, if you could tolerate a base 2 representation, then this is very straightforward (just a case of bit-shifting by 1 as n increases).
 

This can be done by a program which calculates all digits of a number, and holds an array with sufficient places for the entire number.

2^319 works out to a decimal number with 96 or 97 digits. The array will need that amount of places.

You'll need to write code (or use someone else's) to do addition, column by column. You'll need to add a carry when it occurs. Etc.
 

Barry : I tried Excel, but, as Dan said, it too doesn't give the complete number
Dan : I shall try this. Thanks a lot !!
Weetabixharry : I should've mentioned that base 10 arithmetic was needed. Base 2 won't do, unfortunately.
Brad : I was just thinking of that when Dan posted that code. Shall try it. Thanks !
 

Note you will need a machine with bc installed (It is fairly standard and at least on linux is a trivial install if it is missing) and you may need to tweak the path at the top of the file to match the output of 'which bc', but it works here and is trivial.

Also, read the man page if you want to make that line length limit go away (There is an environment variable for maximum line length).

Regards, Dan.
 

That <319 should of course be <320, all programs contain bugs.....

Regards, Dan.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…