Scientific Notation

Reply
Contributor
Posts: 31

Scientific Notation

Hello all;

How can I convert  the following number

    5039220013916026846870204011588016812266185989865191407731082739149737501542867149897589000557841

     6168937269198370425013463695536280705318595740583841262257709320903463659424481514349542590076222

     1723313516671432465293726789169859417220321898386948547503076332140696583782648654162369257874235

     6286709130989273095836110164802229755436763344720101376000000000000000000000000000000000000000000

     00000000

to  the scientific notation 5.039220013916026846870204011588e+395  to use it in the same program in anther statement ?

thanks

Trusted Advisor
Posts: 1,204

Re: Scientific Notation

Hi,

I don't think this is possible in SAS. As a reference please see the attached link....

Regards,

SAS(R) 9.2 Companion for Windows, Second Edition

Super Contributor
Posts: 644

Re: Scientific Notation

To work with numbers with a very large number of significant digits (as in 5.039220013916026846870204011588e+395) you need to use Fortran or some other application that allows arbitrarily large numbers.  SAS, in common with most applications such as MS Excel, uses the operating system's floating point number storage mechanism which in the case of Windows and Unix allows a maximum of 8 bytes, some of which are used for the sign and the exponent.  In practice this allows for 15 digits accuracy (16 for integers provided the initial digit is not 9).  zOS allows another digit or so because it uses fewer bits for the exponent.

Oracle (apparently) allows 12 bytes to floating point numbers which allows 23 digits accuracy.

You can treat these numbers as character strings, compute the length to determine the exponent, substring the first n characters for the numerical result, and insert a decimal to make it look like the result you want.  But you cannot use the result in calculations in SAS.  In the format you specify you cannot even import the data in exponential format because the total width of the informat is limited to 32 characters which includes the exponent, decimal point, signs, and 'e'.

In practical terms it is hard to see where a precision of even 32 digits would be required.

Richard

Trusted Advisor
Posts: 1,300

Re: Scientific Notation

SAS can work with additional datatypes such as bigint, float, etc... within the bounds of PROC DS2, but the general sentiment of your statements is accurate.  If you are going to be working consistently within this context, SAS is not currently the appropriate place.

Super Contributor
Posts: 644

Re: Scientific Notation

@FriedEgg

I'm very grateful to you for drawing my attention to Proc DS2, which is a very recent addition to SAS.  I looked at the definition of Float which implies Double allows precision of over 25 significant digits (though the definition is hazy on this point, and the word 'approximate'  in the definition is a bit of a worry).  So 32 digit precision might be possible within Proc DS2 and Proc FedSQL.  But Bigint is limited to 19 digit precision, so I doubt the OP could use these techniques on his data.

My question still is, what is the OP doing with with such large numbers?

The age of the universe is currently reckoned at 13.8 billion years or 4.36e20 seconds.

The observable universe diameter is 8.948e26 meters and even converting that to Angstrom or Planck units will not require 100 significant digits.

The number of atoms in the observable universe is estimated to be between 1e79 and 1e83, counting the total number of particles would only add a couple of orders of magnitude.

Richard

Trusted Advisor
Posts: 1,300

Re: Scientific Notation

Cryptography is one of areas where numbers of this size are prevalent, to give you a real-world example as you are seeking.  Of course, I cannot speak to the OP's purpose.

RSA-2048 = 2519590847565789349402718324004839857142928212620403202777713783604366202070

  7595556264018525880784406918290641249515082189298559149176184502808489120072

  8449926873928072877767359714183472702618963750149718246911650776133798590957

  0009733045974880842840179742910064245869181719511874612151517265463228221686

  9987549182422433637259085141865462043576798423387184774447920739934236584823

  8242811981638150106748104516603773060562016196762561338441436038339044149526

  3443219011465754445417842402092461651572335077870774981712577246796292638635

  6373289912154831438167899885040445364023527381951378636564391212010397122822

  120720357

Trusted Advisor
Posts: 1,300

Re: Scientific Notation

Let's take a big step out of the box here and solve this...

filename cp temp;

proc groovy classpath=cp;

submit parseonly;

import java.math.BigDecimal;

  import java.text.DecimalFormat;

  class SciNote {

     static String bigSci(String bigint) {

        BigDecimal val = new BigDecimal( bigint + ".0" );

  DecimalFormat df = new DecimalFormat("0.000000000000000000000000000000E0");

return df.format(val).replace("E","e+");

  }     }

endsubmit;

quit;

options set=classpath "%sysfunc(pathname(cp,f))";

data test;

length scinote $ 100;

declare javaobj j('SciNote');

bigint=  "503922001391602684687020401158801681226618598986519140773108273914973750154286714989758900"

||       "055784161689372691983704250134636955362807053185957405838412622577093209034636594244815143"

||       "495425900762221723313516671432465293726789169859417220321898386948547503076332140696583782"

||       "648654162369257874235628670913098927309583611016480222975543676334472010137600000000000000"

||       "000000000000000000000000000000000000"

;

j.callStaticStringMethod('bigSci',bigint,scinote);

put scinote=;

run;

From the LOG:

scinote=5.039220013916026846870204011588e+395

Contributor
Posts: 31

Re: Scientific Notation

When I run the program, the following massage was appeared:

filename cp temp;

  proc groovy classpath=cp;

ERROR: Procedure GROOVY not found.

submit parseonly;

     ------

     14

ERROR: A link must be established by executing the SIGNON command before you can communicate with

       PARSEONLY.

WARNING 14-169: Assuming the symbol RSUBMIT was misspelled as submit.

ERROR: Remote submit to PARSEONLY cancelled.

what can I do?

anther question can I convert the number to the scientific notation in Proc iml???

Thanks for your help

Trusted Advisor
Posts: 1,300

Re: Scientific Notation

PROC GROOVY is new in 9.3, so you would require that version or newer to run it.  Alternatively, you can still run my example by first creating the Java class outside of SAS.  You can find an example I wrote on a different subject for how to go about doing it:

As for using IML, in SAS/IML Studio the IMLPlus language will allow you to run Java code, which you could use as I've illustrated to perform your operation.

You could also process it manually as a array of integers, which is I believe how the BigInteger class in Java works with these numbers.

Contributor
Posts: 31

Re: Scientific Notation

How can I process it manually as a array of integers??? 

Note that I use SAS 9.2

Thanks

SAS Super FREQ
Posts: 3,482

Re: Scientific Notation

The "big question" (ha ha) is what you intend to do with this number. Factor it? Divide by it. Unless you are a researcher in number theory, numbers of this size usually indicate that you should change to a log scale. Statisticians use quantities like log-likelihood and log-determinants because numerical computations that overflow on the original scale succeed on a log scale.  Number of this size also arise in combinatorial computations when you try to compute a formula that includes factorials.  There are better ways to compute these quantities, so why don't you provide a little background about what you are tryng to accomplish and how this number came up?

Contributor
Posts: 31

Re: Scientific Notation

Hi Mr. Rick;

At first I would like to thank you for your cooperation with me.  

Here I tried to compute the values of "xi" to get it's roots,but when i computed the "xi" I need to compute a big factorial and here the large number appeared and the program stop because of the storage space. So i need to convert the large number to scientific notation to reduce the space.


proc iml;

start BigFactorial(n);

start LogFact(n);

return( sum(log(1:n)) );

finish;

numDigits = ceil(logfact(n)/log(10));

f = j(1,numDigits,0);              

f[1] = 1;                         

do i = 2 to n;                     

   carry = 0;

   digits = ceil(logfact(i)/log(10));

   do j = 1 to digits ;            

     g = i*f + carry;           

     digit = mod(g, 10);

     f = digit;                 

     carry = (g - digit)/10;      

   end;

end;

return( f[,ncol(f):1] );          

finish;

i={10 9 8 7 6 5 4 3 2 1 0};

do j=1 to 11;

m=100;

n=5;

n=int(n);

alpha=((m*(n-1))-2)/2;

f = BigFactorial(10+alpha); 

Fact10alpha = rowcat(char(f,1));

f = BigFactorial(alpha+i);

Factalphaij = rowcat(char(f,1));

xi= (Fact10alpha*((-1)**i))/(fact(10-i)*Factalphaij*fact(i));

xivector= xivector//xi;

end;

r = polyroot(xivector);

print alpha;

print xivector[format=E32.14];

print r;

quit;



Trusted Advisor
Posts: 1,300

Re: Scientific Notation

What is the purpose of expressing the value in scientific notation?

Contributor
Posts: 31

Re: Scientific Notation

When I run the program with this large number the program stop for the following error:

xi= (Fact10alpha*((-1)**i))/(fact(10-i)*Factalphaij*fact(i));

xivector= xivector//xi;

  end;

ERROR: (execution) Character argument should be numeric.

operation : * at line 1692 column 17

operands  : FACT10ALPHA, _TEM1003

FACT10ALPHA      1 row       1 col     (character, size 396)

503922001391602684687020401158801681226618598986519140773108273914973750154286714989758900055784161689

372691983704250134636955362807053185957405838412622577093209034636594244815143495425900762221723313516

671432465293726789169859417220321898386948547503076332140696583782648654162369257874235628670913098927

309583611016480222975543676334472010137600000000000000000000000000000000000000000000000000

_TEM1003      1 row       1 col     (numeric)

         1

statement : ASSIGN at line 1692 column 1



but when I used the scientific notation this error doesn't appear.

Super User
Posts: 6,948

Re: Scientific Notation

"Scientific Notation" is just a format through which a SAS number (with all its 8-byte limitations) is displayed.

fact10alpha is not numeric, but a character variable that CANNOT be used as a number value in a computation, because with such a big string the automatic conversion (that SAS usually tries) fails.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 25 replies
  • 1631 views
  • 5 likes
  • 6 in conversation