PrevNextUpHome SophiaFramework UNIVERSE 5.3

20.2. Mathematical Operations

20.2.1. Random Number Class

There are three types of classes for generating a random number.

Table 20.1. Random number class

Class Name Generating method
SFXBrewRandom Class which generates a random number by using the BREW random number generation function.
SFXLCGRandom Class which generates a random number by using the Linear Congruential Generator method(LCG method).
SFXMTRandom Class which generates a random number by using the Mersenne Twister method(MT method).

The SFXBrewRandom class generates a random number using the random number generation function of BREW API.

This class internally calls the BREW API GETRAND function to generate a random number.

For each numeric data type such as Bool, Float32, Float 64, SInt08, SInt16, SInt32, UInt08, UInt16 or UInt32, there is the corresponding random number generation functions

For the minimum and the maximum of random number that each function can generate, see Macro for Maximum and Minimum Values.

Example 20.2. How to use the SFXBrewRandom class

// generate a random number between SINT32_MINIMUM and SINT32_MAXIMUM
SInt32 n1 = SFXBrewRandom::GetSInt32(); 

// generate a random number between 0 and UINT08_MAXIMUM
UInt08 n2 = SFXBrewRandom::GetUInt08(); 

// generate a random number of Float64
Float64 n3 = SFXBrewRandom::GetFloat64(); 

// generate a random number of Bool type
Bool b = SFXBrewRandom::GetBool();       
// this method generates a random numbers of higher quality than the below
// Bool b = static_cast<Bool>(SFXBrewRandom::GetUInt32() % 2);

The SFXLCGRandom class generates a 48-bit random number using the same algorithm as the drand48 function of UNIX.

The same sequence of random numbers will be generated from the same seed.

Example 20.3. How to use the SFXLCGRandom class

// if no seed is set, the elapsed time after the power is turned ON will become the seed
SFXLCGRandom random;

// generate a random number between SINT32_MINIMUM and SINT32_MAXIMUM
SInt32 n1 = random.GetSInt32(); 

// set the seed
random.Randomize(3456);         

// generate a random number between 0 and UINT08_MAXIMUM
UInt08 n2 = random.GetUInt08(); 

The SFXMTRandom class generates a random number using the Mersenne Twister algorithm.

The same sequence of random numbers will be generated from the same seed.

[Caution] If stack is insufficient, ...

The SFXMTRandom class consumes about 2.5 kilo bytes of memory. If stack is insufficient, the SFXMTRandom instance will be created on heap.

Example 20.4. How to use the SFXMTRandom class

// if no seed is set, the elapsed time after the power is turned ON will become the seed
SFXMTRandom random;

// generate a random number between SINT32_MINIMUM and SINT32_MAXIMUM
SInt32 n1 = random.GetSInt32(); 

// set the seed
random.Randomize(3456);         

// generate a random number between 0 and UINT08_MAXIMUM
UInt08 n2 = random.GetUInt08(); 
[Note] Note
Methods of the SFXMTRandom class are the same with those of the SFXLCGRandom class.

20.2.2. Trigonometric Class

SFXTrigonometric is the class that performs trigonometric operations by using the trigonometric table.

Example 20.5. How to use SFXTrigonometric class

SFXTrigonometric table;

// initialize trigonometric table
// this processing usually takes time
table.Initialize();

SInt32 i;
for (i = 0; i < 1000; ++i) { 

    // quick trigonometric calculation
    Float64 n1 = table.Sin(i);
    Float64 n2 = table.Cos(i);
    Float64 n3 = table.Tan(i);

   // process n1, n2, n3 here

}

The conversion functions between radian and degree are available in the SFXTrigonometric class.

Example 20.6. Conversion functions between radian and degree

Float64 n1 = SFXTrigonometric::DegreeToRadian(180);    // n1 = 3.1415... 
Float64 n2 = SFXTrigonometric::RadianToDegree(6.2831); // n2 = 359.99... 

20.2.3. Floating Point Arithmetic Operations

In SophiaFramework UNIVERSE, floating point arithmetic operations that can not be used in the standard BREW and mathematical functions defined in math.h of each compiler such as ARM RealView Compilation Tools for BREW V1.2 / V3.0 are available.

[Tip] Including the "math.h" file

You don't have to include the math.h file since it has already been included in the SFCCondition.h.hpp file.

For instance, mathematical functions available in ARM RealView Compilation Tools for BREW 1.2 are as follows:

Example 20.7. Mathematical functions of RealView Compilation Tools for BREW V1.2

// macro (concrete value of macro depends on kinds of compiler)
HUGE_VAL
INFINITY
NAN


// functions
Float64 acos(Float64)
Float64 asin(Float64)
Float64 atan(Float64)
Float64 atan2(Float64, Float64)
Float64 cos(Float64)
Float64 sin(Float64)
Void __use_accurate_range_reduction(Void) // cannot be used in VC++
Float64 tan(Float64)
Float64 cosh(Float64)
Float64 sinh(Float64)
Float64 tanh(Float64)
Float64 exp(Float64)
Float64 frexp(Float64, int*)
Float64 ldexp(Float64, int)
Float64 log(Float64)
Float64 log10(Float64)
Float64 modf(Float64, Float64Ptr)
Float64 pow(Float64, Float64)
Float64 sqrt(Float64)
Float64 _sqrt(Float64)               // in VC++, use sqrt(Float64)
Float32 _sqrtf(Float32)              // in VC++, use sqrtf(Float64)
Float64 ceil(Float64)
Float64 fabs(Float64)
Float32 _fabsf(Float32)              // in VC++, use fabsf(Float32)
Float32 fabsf(Float32)
Float64 floor(Float64)
Float64 fmod(Float64, Float64)
Float64 acosh(Float64)               // cannot be used in VC++
Float64 asinh(Float64)               // cannot be used in VC++
Float64 atanh(Float64)               // cannot be used in VC++
Float64 cbrt(Float64)                // cannot be used in VC++
Float64 copysign(Float64, Float64)   // in VC++, include float.h 
                                     // and use _copysign(Float64, Float64)
Float64 erf(Float64)                 // cannot be used in VC++
Float64 erfc(Float64)                // cannot be used in VC++
Float64 expm1(Float64)               // cannot be used in VC++
Float64 finite(Float64)              // in VC++, include float.h
                                     // and use _finite(Float64)
Float64 gamma(Float64)               // cannot be used in VC++
Float64 gamma_r(Float64, int*)       // cannot be used in VC++
Float64 hypot(Float64, Float64)
int ilogb(Float64)                   // cannot be used in VC++
Float64 isnan(Float64)               // in VC++, include float.h
                                     // and use _isnan(Float64)
Float64 j0(Float64)                  // in VC++, use _j0(Float64)
Float64 j1(Float64)                  // in VC++, use _j1(Float64)
Float64 jn(int, Float64)             // in VC++, use _jn(int, Float64)
Float64 lgamma(Float64)              // cannot be used in VC++
Float64 lgamma_r(Float64, int*)      // cannot be used in VC++
Float64 log1p(Float64)               // cannot be used in VC++
Float64 logb(Float64)                // cannot be used in VC++
Float64 nextafter(Float64, Float64)  // in VC++, include float.h
                                     // and use _nextafter(Float64, Float64)
Float64 remainder(Float64, Float64)  // cannot be used in VC++
Float64 rint(Float64)                // cannot be used in VC++
Float64 scalb(Float64, Float64)      // in VC++, include float.h
                                     // and use _scalb(Float64, SInt32)
Float64 scalbn(Float64, int)         // in VC++, include float.h
                                     // and use _sclab(Float64, SInt32)
Float64 significand(Float64)         // cannot be used in VC++
Float64 y0(Float64)                  // in VC++, use _y0(Float64)
Float64 y1(Float64)                  // in VC++, use _y1(Float64)
Float64 yn(int, Float64)             // in VC++, use _yn(int, Float64)

// functions used in C++ environment
Float64 abs(Float64)
Float32 abs(Float32)
Float32 acos(Float32)
Float32 asin(Float32)
Float32 atan(Float32)
Float32 atan2(Float32, Float32)
Float32 ceil(Float32)
Float32 cos(Float32)
Float32 cosh(Float32)
Float32 exp(Float32)
Float32 fabs(Float32)
Float32 floor(Float32)
Float32 fmod(Float32, Float32)
Float32 frexp(Float32, int*)
Float32 ldexp(Float32, int)
Float32 log(Float32)
Float32 log10(Float32)
Float32 modf(Float32, Float32Ptr)
Float32 pow(Float32, Float32)
Float32 pow(Float32, int)
Float32 sin(Float32)
Float32 sinh(Float32)
Float32 sqrt(Float32)
Float32 _sqrt(Float32)                //in VC++, use sqrt(Float32)
Float32 tan(Float32)
Float32 tanh(Float32)
Float64 pow(Float64, int)