David, Ducasse, Goran - Re: Query about Long Number Calcs and Shareability

Colin Putney cputney at wiresong.ca
Fri Feb 20 18:42:27 UTC 2004


On Feb 20, 2004, at 12:43 PM, Dr Strange wrote:

> Guys, thanks for the feedback!  Hey this list is responsive, <relief>.
>
> >I can't tell from your original question ....
>
> Suppose we have the number
> 14229499582749302182940392839403938400058748399920384875773202816267485 
> 94937267261
> (82 characters long), and we want to divide this by 47, and get an  
> answer correct to 4 decimal places.....?

Well, sort of. If you do that division, you'll actually get a Fraction  
object, which is exact, and can be used in further calculations.  
Fractions can be converted to Floats, with the expected loss of  
precision. Offhand I don't see any methods for converting Fractions to  
base 10 strings, but I doubt it would be hard to write one.

> Ducasse mentions 1000 and 2000 length number strings could we actually  
> perform operations on these and get accurate results to four decimal  
> places with out rounding off? Could you give an example of how this  
> might be done in code (roughly)? Is it quite the acrobatic feat or  
> just normal Squeak math operations inline? Call me dense..... (I'm  
> sometimes a bit thick / slow - when first trying to viusalize  
> something.....) but I can't yet see how this would be done ....

Actually Stéphane gave the classic example of large number support in  
Squeak. He sent the message #factorial to the SmallInteger 1000, which  
resulted in a LargeInteger with 2568 digits. The following snippet of  
code does the same thing, then converts the result to a (decimal)  
string and returns the number characters:

1000 factorial asString size

A similar operation on 2000 gives you a LargeInteger of 5736 digits.

This sort of thing is possible in Squeak because there are no "math  
operations," just objects and messages. For example adding 3 and 4 to  
get 7 is really sending the #+ message to 3, with a parameter of 4.  
142294995827493021829403928394039384000587483999203848757732028162674859 
4937267261 and 47 have different internal representations, but they  
both respond to #+ and similar messages in appropriate ways.

So, to answer your question, the example you give is very simple with  
normal Squeak math messages. The code would be:

142294995827493021829403928394039384000587483999203848757732028162674859 
4937267261 / 47

You would get an exact answer, which would be usable for further  
calculations. You might have to do a bit of work to convert that answer  
into a string of numerals with a given accuracy.

> I'm working with number theory.... yeah I know, why would I choose VB,  
> Delphi... loooong story (nopun intended), my data set and current code  
> is immense so I can't port it all in a short period of time, yet....  
> But perhaps I can create a small applet in Squeak, compile a small  
> utility to exe and then shell out from my current app and get the num  
> string generated to text by the squeak applet and pull the value in,  
> cumbersome, but if it works in the mean time?

That would be doable. Personally I'd opt for a "math server" in Squeak,  
that you'd communicate with via XML-RPC or some such.

> You guys mentioned Smalltalk can generate to .DLL?  Could I design a  
> small utility in Squeak and then easily port the code to Smalltalk and  
> then generate the DLL callable to my current app?  If so that would be  
> GREAT!

Squeak is a specific implementation of Smalltalk, so porting to another  
implementation should be trivial. (That's not always the case, but the  
math stuff is pretty standard.)

Hope this helps,

Colin




More information about the Squeak-dev mailing list