Topic: Is shell cleverer at arithmetic than C/C++?
Author: haberg@REMOVE.matematik.su.se (Hans Aberg)
Date: 1999/03/30 Raw View
In article <7ddahg$8sk$1@nnrp1.dejanews.com>, Alex Vinokur
<alexander.vinokur@telrad.co.il> wrote:
>.. perform calculations with very long integers in shell.
>An example is shown below.
>
>
>There is no C/C++ languages feature
>that enables to perform such calculations.
>
>Is is feasible to develop some *very long integers* feature in C/C++?
>(For example, to define arithmetic operations for basic_string<int>)
>Is it worth doing it?
There was a very long thread here some time ago about just that: Some of
the fellows involved with developing the C++ standard felt it was too few
general purpose users of multiprecision arithemtic to extend C++ in that
respect, or even in the respect of improving C++ for aiding the
development of efficient such libraries. Attached to this discussion was
also a long thread whether such extensions would actually help the
performance of multiprecision arithemtic code; this discussion is
complicated by the fact that one might develop hybrids between C/C++ "int"
and multiprecion integers, and the needed implementation may differ if one
say develops a dynamic language using C++ or if one merely is needs
multiprecision arithmetic in an otherwise rather static program.
However, if one extends C++ with a portable assembler language (see for
example C-- by Simon Peyton Jones
<http://research.microsoft.com/~simonpj>), then that would be sufficient
to develop many different types of libraries, including good
multiprecision arithmetic.
In the interim, I guess the UNIX program you mentioned is written in C, so
if you are not out for highly optimized code performance, there should be
some program or library out there that can do the job.
Hans Aberg * Anti-spam: Remove "REMOVE." from email address.
* Email: Hans Aberg <haberg@REMOVE.member.ams.org>
* Home Page: <http://www.matematik.su.se/~haberg/>
* AMS member listing: <http://www.ams.org/cml/>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alex Vinokur <alexander.vinokur@telrad.co.il>
Date: 1999/03/25 Raw View
Hi,
In shell (csh, sh) there is the bc command :
arbitrary precision arithmetic language.
It enables to perform calculations with very long integers in shell.
An example is shown below.
There is no C/C++ languages feature
that enables to perform such calculations.
Is is feasible to develop some *very long integers* feature in C/C++?
(For example, to define arithmetic operations for basic_string<int>)
Is it worth doing it?
P.S. It is a question of improving C/C++/STL themselves
(not of using extended precision number classes
which are out of C/C++/STL).
Thanks,
Alex
//================
Here's the example.
//================
//#########################################################
//########################### 1 ###########################
//#########################################################
//----------- File arith.script : BEGIN -------------------
// File contains bc functions
define a (x, y) {
z=x+y;
return (z);
}
define m (x, y) {
z=x*y;
return (z);
}
//----------- File arith.script : END ---------------------
//#########################################################
//########################### 2 ###########################
//#########################################################
//----------- File doIt.script : BEGIN --------------------
// File contains very short csh-program of calculation
// with very long integers
#!/bin/csh -f
alias add 'set s = (\!*); echo "a ($s[1], $s[2])" | bc arith.script'
alias mult 'set s = (\!*); echo "m ($s[1], $s[2])" | bc arith.script'
set x =
12345678901234567890123456789012345678901234567890123456789012345678901234567
89012345678901234567890123456789012345678901234567890123456789012345678901234
5678901234567890123456789012345678901234567890123456789012345678901234567890
set y =
98765432109876543210987654321098765432109876543210987654321098765432109876543
21098765432109876543210987654321098765432109876543210987654321098765432109876
5432109876543210987654321098765432109876543210987654321098765432109876543210
echo ""
echo "----------------------------------"
echo $x
echo +
echo $y
echo =
add $x $y
echo ""
echo "----------------------------------"
echo $x
echo '*'
echo $y
echo =
mult $x $y
//----------- File doIt.script : END ----------------------
//#########################################################
//########################### 3 ###########################
//#########################################################
//----------- Command Line : BEGIN ------------------------
doIt.script | fold -70
//----------- Command Line : END --------------------------
//#########################################################
//########################### 4 ###########################
//#########################################################
//----------- Results of the running : BEGIN --------------
----------------------------------
1234567890123456789012345678901234567890123456789012345678901234567890
1234567890123456789012345678901234567890123456789012345678901234567890
1234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890
+
9876543210987654321098765432109876543210987654321098765432109876543210
9876543210987654321098765432109876543210987654321098765432109876543210
9876543210987654321098765432109876543210987654321098765432109876543210
98765432109876543210
=
11111111101111111110111111111011111111101111111110111111111011111111\
10111111111011111111101111111110111111111011111111101111111110111111\
11101111111110111111111011111111101111111110111111111011111111101111\
111110111111111011111111100
----------------------------------
1234567890123456789012345678901234567890123456789012345678901234567890
1234567890123456789012345678901234567890123456789012345678901234567890
1234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890
*
9876543210987654321098765432109876543210987654321098765432109876543210
9876543210987654321098765432109876543210987654321098765432109876543210
9876543210987654321098765432109876543210987654321098765432109876543210
98765432109876543210
=
12193263113702179522618503273386678859451150739156363359236761164455\
78859929879010821520013565005212604785842385307116351013564846349641\
80575979271268846212448009449776913427830902591068411383935373250876\
39053633592437475842096958588629754734034443360920591124843773791359\
54884702347203149109891782798506325068602347185735406186461057765434\
85749122236092059012360920580111263525898643499378616064616736777929\
5611949397448712086533622923332237463801111263526900
//----------- Results of the running : END ----------------
//#########################################################
//################## END of INFORMATION ###################
//#########################################################
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]