Solved

Using bc to perform calculation of LARGE Numbers

Posted on 2002-06-30
12
289 Views
Last Modified: 2013-12-06
I was told that: "each UNIX comes with a small program called bc (precision calculator), depending on the (math)libs installed on the system, bc can do the calculation of 4^(10^9)"

Could you please tell me what math libs are needed?
How do I refer to them?
And in general what is the exact command that I need to use to perform that calculation?


Thanks!
0
Comment
Question by:ody13
  • 5
  • 4
  • 3
12 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7121232
echo "4^(10^9)" | bc

but I sugest to try following first:
echo "2^(10^6)" | bc

be prepared for a huge output, but you already know this ;-)

If bc is compiled with a libm which is not sufficient for huge numbers, you get an error message.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7121421
another suggestion:
perl -MMath::BigInt=:constant -le 'print (4**(10**9))'
0
 

Author Comment

by:ody13
ID: 7124937
I am getting errors with both suggestions.

Anything else I can do?

Thanks
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7125453
2^(10^)  produces errors too?, then you have insufficient libs and/or programs.
To track down this, please post results of:
  uname -a
  echo 1 | bc -v
  # errors produced by suggested commands
0
 
LVL 7

Expert Comment

by:waynezhu
ID: 7128096
Even for 2^10^6, it may take long time to calculate.

While ago, I was asking my students to calculate
2^10^5 using bc. The trick is to avoid
large exponent.
Below shows an example.
The output is piped to wc, so that you can see how
many lines and digits:

echo "(2^1000)^100" |  bc | wc
443     443   30988

Good luck.


0
 

Author Comment

by:ody13
ID: 7128187
ahoffmann,

I am not sure what you mean. 2^10 gives the correct result.

I typed:
uname -a   THEN PRESSED ENTER AND THEN TYPED
echo 1 | bc -v   THEN PRESSED ENTER AND I DID NOT GET ANY ERRORS

Up until 2^9999 I get a result, but after that I get the following error:
2^10000
exp too big
empty stack
save:args

Any thoughts of what is going on?

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ody13
ID: 7128196
Hey waynezhu,

thanks for your insight. However, I am not familiar with wc, what is that and where and how can I see the result?

ODY
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7128262
wc  - word count
it just count words, lines and characters, that's all.

About bc's "exp too big":
  does uname -a not give you anything?
  ich "echo 1|bc" does not produce a string, please simply call:
      bc

it should print a version number. You can exit bc by using the    quit    command.

Sounds that you need another bc.
0
 

Author Comment

by:ody13
ID: 7128303
Ok,

now I understand,  I thought that I had to type these commands while being in bc.

Here is the output:
/prod/users/usm/u58554> uname -a
HP-UX groucho B.11.00 U 9000/800 47704516 unlimited-user license
/prod/users/usm/u58554> echo 1 | bc -v
bc: illegal option -- v
/prod/users/usm/u58554> echo 1|bc
1
/prod/users/usm/u58554>


However, I managed to do 2^(10^6) by writing instead (2^1000)^1000.

Is there anyway that I can have this output get printed on a file?
0
 
LVL 7

Accepted Solution

by:
waynezhu earned 100 total points
ID: 7128503
try either

echo "(2^1000)^100" | bc |tee 2to100000.txt

or

echo "(2^1000)^100" | bc > 2to100000.txt
0
 

Author Comment

by:ody13
ID: 7130784
Thanks!

BTW, are you familiar with Maple at all?
0
 
LVL 7

Expert Comment

by:waynezhu
ID: 7130830
No.
However 5-6 years ago, people (including myself) in my group were using Maple V, Mathlab, and Mathematica along with freeware such as xmgr, gnuplot, etc  
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AIX print queues constantly going down 11 490
Error while installing rpm 1 77
Virtualizing TAPE on dual VIOS 3 79
cron job says it ran, no results 25 115
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now