Multi-Precision Arithmetic by C++ with no use of assembler
SN library Copyright (C) 1999-2018 K.Tsuru

Reference of class functions part 1

Here I shall explain the elementary functions of classes which are useful to make programs. The private or protected functions and not so useful ones are not listed.


1. SNManager class's member functions
This class manages the errors and the various maximum figures of derived classes. Except "SetEffFig()" a derived class's object 'a' can call the functions below such as "a.FuncName()" or "SNManager::FuncName()" (static member functions only).

enumerating constants
enum SNManager::NumberType { MIN_SIZE = 0, INTEGER = 1, REAL = 2, DEC_RDX = 3, BIN_RDX = 4, BIN_INT = 5, BIN_DEC = 6, NUMBER_TYPE = 7, UNKNOWN = 8};

PreferSpeed
function  If you prefer to speed rather than precision, set this value ON by a statement a.PreferSpeed(ON);.
The default is OFF. An object of "RealSize class" can also call this function.
form
static void SNManager::PreferSpeed(int p); It changes the value.
static bool SNManager::PreferSpeed(); It retruns present value.

OutPutFile
function It provides a method which change the output stream.
form
static FILE* SNManager::OutPutFile(const char* fname, int ovw = NO);
comment
fname : file name
If fname == NULL, "fclose()" and reset stream = stdout, where "FILE* stream" is a private member.
If the file could not be opened, close program by "exit()" function.
1. When ovw == NO (default), the program asks whether you permit to overwrite on the existing file or not. If you chose NO, it asks whether you would continue the program or not.
2. If ovw == YES, the program overwrites without warning. This provides a method to keep data on a disk which causes an abnormal termination by an error.
3. It returns "FILE* stream".

FPuts
function "x.FPuts(s)" outputs a literal 's' to present stream. You can output in an optional format using two steps
1. sprintf(buff, fmt,...); etc.
2. a.FPuts(buff);
form static int SNManager::FPuts(const char* s);

FPutc
function "x.FPutc(c)" outputs a character 'c' to present stream.
form static int SNManager::FPutc(int c);

OutPutStream, FileStream
function It provides a method which changes the output stream into "stderr" etc.
If ostr == NULL, reset stream = stdout.
form
static void SNManager::OutPutStream(FILE* ostr);
static FILE* SNManager::FileStream(); It returns present output stream.

enumerating constants and messages
function
It gives the definition for the kind of error.
form 

enum SNManager::SNErrorFlag {
NO_ERR = 0, OVERFLOW_ERR, UNDERFLOW_ERR, DIVIDED_BY_ZERO, SIGN_ERR, NOT_CONVERGE, DOMAIN_ERR, OUT_OF_RANGE, RADIX_ERR, UNDEC_VALUE, SET_EFF_FIG, SYNTAX_ERR, FFT_ERR, TOO_LONG, TOO_LARGE_EXP, TLOSS_ERR, VERIFY, FATAL };
Comment Corresponding messages are as follows :
 0. SN Library Ver.2.XX by K.Tsuru / GNU Library General Public License Version 2 //This is for Version() given below.
 1. Overflow
 2. Underflow
 3. Divided by 0
 4. Sign
 5. Series did not converge
 6. Domain // Mathematical error
 7. Out of range // Not mathematical error but argument of function was out of range.
 8. Radix
 9. Undecided number operation in substitution/output
10. Usage of SetEffFig()
11. Syntax
12. FFT
13. Given number was too long
14. Exponent is too large/small
15. Effective figures lost
16. Verification
17. Fatal

SNError
function "x .SNError();" returns the kind of error by the value shown above. It resets "NO_ERR" after returning a value.
form inline static SNErrorFlag SNManager::SNError();

FFTVerify
function It designates to verify the correctness of FFT multiplication routine or not by setting v ON(=1) or OFF(=0).
form
inline static void SNManager::FFTVerify(int v); It changes the value.
inline static bool SNManager::FFTVerify(); It returns the present value.
comment
It confirms that the resultant double value of FFT multiplication is close to an integral one within an error. See the file "slfllfft.cpp" for details.  

Verify
function It designates to verify or not in the division routine, etc. by setting v ON(=1) or OFF(=0).
form
inline static void SNManager::Verify(int v); It changes the value.
inline static bool SNManager::Verify(); It returns the present value.
comment
After the division "r = a/b" and "q = a%b" the equation "a = r*b+q" is verified.
In the "r = Sqrt(x)" function it is confirmed that the value "r*r-x" is very close to zero or not. These verifications make slow progressing. If "PreferSpeed() == ON", it sets "Verify()==OFF". The default is "Verify()==OFF". If you had a doubtful result, please set this value "ON". See also "PreferSpeed()".

Version
function "x .Version();" shows the version number shown above.
form inline static void SNManager::Version ();

ReadNumber, CloseReadNumber
function It provides a method to read multi-precision number (s) from the file "fname".
form
static const char* SNManager::ReadNumber(const char* fname, uint size = 0, int del = 0);
static void SNManager::CloseReadNumber();
comment
It opens a file "fname" which includes text data of number (s) and read onto the static memory "readBuff" and return the constant pointer of "readBuff".
If you would like to receive as a literal

SDouble x;
const char* num;
num = x.ReadNumber(fname, x.MaxSize());

For a direct substitution

SLong a;
a = a.ReadNumber(fname);
a.CloseReadNumber();

The memory is allocated on a static area, then previous data will be replaced by new one.
size: Reading real number (s), give a reasonable figures in the unit 4 (e.g. "MaxSize()" for "SDouble"). When size == 0, all figures will be read. This maybe causes an out of memory error when the file is too long on the poor memory system.
del : Give a non-zero value if you want to delete the file after reading.
The function "CloseReadNumber()" closes file and free the memory of "readBuff".

Caution about making file
Comments which begins at an alphabet character can be inserted. '/', '(' and ')' are used in the representation of fraction, then cannot be used under some condition. Only the text file in a decimal system is allowed within the "UINT_MAX-1" digits.
When firstly you give a file name to "fname", next set "fname" NULL, continuous data reading can be done. Reaching to the end of file it returns NULL.

Example 1
T[1]= comment
1 interprets a first data
T[2]=
2 second data

Example 2
12345
67890
interprets one data "1234567890"

Example 3
12345, 67890
separate two data "12345" and "67890"

Click here for a sample program : outputs a table of factorial to a file.
In order to use the array of "SLong" object you can use "Block" template class.
See a sample program "smpbern.cpp" and "block.h".

MemFreeFunc
function It entries a function that free the memory of static objects.
form static Stack <void (*)()> SNManager::MemFreeFunc;
comment  "Stack" template class defined in "stack.h" is used in it.
usage
void freemem(){....}
x.MemFreeFunc.Entry(freemem); //give a function pointer
Where "Entry()" is a member function of "Stack" class.
See also the function "Pi()" in "sdcpi.cpp".

SNMemFree
function It executes all functions registered by "MemFreeFunc.Entry()".
form static void SNManager::SNMemFree();

EffFigures
function It returns the present significant figures in the unit of four(=DFIGURES).
form static unsigned int SNManager::EffFigures();

FFTSwitch
function It provides a method which change the parameter of FFT multiplication.
form static void SNManager::FFTSwitch(unsigned int fft_min_sz);
comment
If fft_min_sz == 0, do not use. 
If fft_min_sz == 16, use greater than or equal to 16.
default : fftMinSize = 64(decimal radix)/128(binary radix)
If 0 < fft_min_sz < 2*minArraySize(=8), set fftMinSize = 8.

ShowMessage
function It designates to show message "Evaluating Pi()", etc or not in the routine which takes much time. The default is "ShowMessage() == OFF".by setting v ON(=1) or OFF(=0).
form
inline static bool
SNManager::ShowMessage(); // It returns a present value.
inline static void
SNManager::ShowMessage(int sw); // It sets by sw ON(=1) or OFF(=0).
static void
SNManager::ShowMessage(const char* msg); // It outputs a literal msg to "stderr".

MaxArraySize
function x.MaxArraySize() or SNManager::MaxArraySize() returns the present value of SNManager::maxArraySize.
form inline static uint SNManager::MaxArraySize();

SetMaxArraySize
function It enlarges the upper limit of figures.
form static void SNManager::SetMaxArraySize(uint s );
comment
If s is greater than defaultMaxArraySize, it changes the value of SNManager::maxArraySize. I advise  you not to use this function on a poor memory.

[Other related function]

SNInfo
function It shows the information about sizes, etc. This is not a member function.
form void SNInfo();