05-03-2022, 10:29 PM
(This post was last modified: 05-03-2022, 10:31 PM by TarotRedhand.)
Just a few things I created back in the late 1990s. First a few constants that you may or may not find useful -
Then there is this library of a number maths routines. First the documentation (comments also included in the code) -
Finally the code -
Hope you have a use for these.
TR
Code: (Select All)
REM ******************************************************************
REM * A number of maths constants that have been pre-calculated in *
REM * order to save execution time. *
REM ******************************************************************
CONST PI# = 3.14159265358979323846 'PI
CONST PITimes2# = 6.28318530717959 'PI * 2
CONST PITimes3# = 9.42477796076938 'PI * 3
CONST PITimes4# = 12.5663706143592 'PI * 4
CONST PITimes5# = 15.707963267949 'PI * 5
CONST PITimes6# = 18.8495559215388 'PI * 6
CONST PIDividedBy2# = 1.57079632679489661923 'PI / 2
CONST PIDividedBy3# = 1.04719755119659774615 'PI / 3
CONST PIDividedBy4# = 0.78539816339744830962 'PI / 4
CONST PIDividedBy5# = 0.628318530717959 'PI / 5
CONST PIDividedBy6# = 0.52359877559829887308 'PI / 6
CONST PISquared# = 9.86960440108936 'PI * PI
CONST PICubed# = 31.0062766802998 'PI * PI * PI
CONST SQROfPI# = 1.77245385090552 'SQR(PI)
CONST ReciprocalOfPI# = 0.31830988618379067154 '1 / PI
CONST TwoDividedByPI# = 0.63661977236758134308 '2 / PI
CONST ThreeDividedByPI# = 0.954929658551372 '3 / PI
CONST FourDividedByPI# = 1.27323954473516 '4 / PI
CONST FiveDividedByPI# = 1.59154943091895 '5 / PI
CONST SixDividedByPI# = 1.90985931710274 '6 / PI
CONST FourThirdsOfPI# = 4.18879020478639 '4 / 3 * PI
CONST TheConstantE# = 2.71828182845905
CONST SquareOfE# = 7.38905609893065 'E * E
CONST SQROfE# = 1.64872127070013 'SQR(E)
CONST ReciprocalOfE# = 0.367879441171442 '1 / E
CONST LOG3DividedBy2# = 0.54930614433405484570 'LOG(3) / 2
CONST SQROfPnt5# = 0.70710678118654752440 'SQR(0.5)
CONST SQROf2# = 1.41421356227309504880 'SQR(2)
CONST SQROf3# = 1.73205080756887729353 'SQR(3)
CONST TwoMinusSQROf3# = 0.26794919243112270647 '2 - SQR(3)
CONST TwelthRootOf2# = 1.059463094 '12th root of 2
CONST FeigenbaumConstant# = 4.6692016090
REM ******************************************************************
REM * Distance travelled by light in 1 second, minute, hour, day, *
REM * year. Measured in Miles (approximately). *
REM ******************************************************************
CONST LightSecondInMiles# = 186271.0
CONST LightMinuteInMiles# = 11176260.0
CONST LightHourInMiles# = 670575600.0
CONST LightDayInMiles# = 16093814400.0
CONST LightYearInMiles# = 5797796637600.0
REM ******************************************************************
REM * Distance travelled by light in 1 second, minute, hour, day, *
REM * year. Measured in KiloMeters (approximately). *
REM ******************************************************************
CONST LightSecondInKiloMeters# = 299774.0
CONST LightMinuteInKiloMeters# = 17986440.0
CONST LightHourInKiloMeters# = 1079186400.0
CONST LightDayInKiloMeters# = 25900473600.0
CONST LightYearInKiloMeters# = 9330645614400.0
Then there is this library of a number maths routines. First the documentation (comments also included in the code) -
Code: (Select All)
REM ******************************************************************
REM * The following routines are entirely devoted to logarithms for *
REM * bases other than e, which is the base used by QB. The base *
REM * used in a particular routine is denoted by the number which *
REM * suffixes Log in the name of the function, with the exception *
REM * of LogX where the user supplies the base. *
REM ******************************************************************
FUNCTION Log2#(x AS DOUBLE)
FUNCTION Log8#(x AS DOUBLE)
FUNCTION Log10#(x AS DOUBLE)
FUNCTION Log16#(x AS DOUBLE)
FUNCTION LogX#(x AS DOUBLE, LogBase AS DOUBLE)
FUNCTION AntiLog2#(x AS DOUBLE)
FUNCTION AntiLog8#(x AS DOUBLE)
FUNCTION AntiLog10#(x AS DOUBLE)
FUNCTION AntiLog16#(x AS DOUBLE)
FUNCTION AntiLogX#(x AS DOUBLE, LogBase AS DOUBLE)
REM ******************************************************************
REM * Finds the y'th root of x. *
REM ******************************************************************
FUNCTION Root#(x AS DOUBLE, y AS DOUBLE)
REM ******************************************************************
REM * Raise x to the power of y. *
REM ******************************************************************
FUNCTION Power#(x AS DOUBLE, y AS DOUBLE)
REM ******************************************************************
REM * Returns x' = x * (x - 1) * (x - 2) ... * 1. Non-recursive! *
REM ******************************************************************
FUNCTION Factorial#(x AS DOUBLE)
REM ******************************************************************
REM * Returns the arithmetic mean (average) and the sample standard *
REM * deviation of the values held in the array In. *
REM ******************************************************************
SUB MeanDeviation(Mean AS DOUBLE, StandardDeviation AS DOUBLE, In#())
REM ******************************************************************
REM * Returns a random number that is constrained to have a *
REM * greater probability of falling within the centre of upper and *
REM * lower bounds. *
REM ******************************************************************
FUNCTION GaussianRnd#(DesiredMean#, DesiredDeviation#)
Finally the code -
Code: (Select All)
OPTION BASE 0
REM ******************************************************************
REM * Pre-calculated constants for use by the logarithmic functions *
REM ******************************************************************
CONST LOGOf2# = 0.6931471805599453094172321 'LOG(2)
CONST LOGOf8# = 2.07944154167984 'LOG(8)
CONST LOGOf10# = 2.30258509299405 'LOG(10)
CONST LOGOf16# = 2.77258872223978 'LOG(16)
CONST Log2E# = 1.4426950408889634074 '1/LOG(2)
CONST Log8E# = 0.480898346962988 '1/LOG(8)
CONST Log10E# = 0.43429448190325182765 '1/LOG(10.0)
CONST Log16E# = 0.360673760222241 '1/LOG(16)
REM ******************************************************************
REM * The following routines are entirely devoted to logarithms for *
REM * bases other than e, which is the base used by QB. The base *
REM * used in a particular routine is denoted by the number which *
REM * suffixes Log in the name of the function, with the exception *
REM * of LogX where the user supplies the base. *
REM ******************************************************************
FUNCTION Log2#(x AS DOUBLE)
IF x < 0.0 THEN
Log2# = 0.0
ELSE
Log2# = LOG(x) * Log2E#
END IF
END FUNCTION
FUNCTION Log8#(x AS DOUBLE)
IF x < 0.0 THEN
Log8# = 0.0
ELSE
Log8# = Log(x) * Log8E#
END IF
END FUNCTION
FUNCTION Log10#(x AS DOUBLE)
IF x < 0.0 THEN
Log10# = 0.0
ELSE
Log10# = Log(x) * Log10E#
END IF
END FUNCTION
FUNCTION Log16#(x AS DOUBLE)
IF x < 0.0 THEN
Log16# = 0.0
ELSE
Log16# = Log(x) * Log16E#
END IF
END FUNCTION
FUNCTION LogX#(x AS DOUBLE, LogBase AS DOUBLE)
IF ((x < 0.0) OR (LogBase <= 0.0)) THEN
LogX# = 0.0
ELSE
LogX# = Log(x) / Log(LogBase)
END IF
END FUNCTION
FUNCTION AntiLog2#(x AS DOUBLE)
AntiLog2# = EXP(LOGOf2# * x)
END FUNCTION
FUNCTION AntiLog8#(x AS DOUBLE)
AntiLog8# = EXP(LOGOf8# * x)
END FUNCTION
FUNCTION AntiLog10#(x AS DOUBLE)
AntiLog10# = EXP(LOGOf10# * x)
END FUNCTION
FUNCTION AntiLog16#(x AS DOUBLE)
AntiLog16# = EXP(LOGOf16# * x)
END FUNCTION
FUNCTION AntiLogX#(x AS DOUBLE, LogBase AS DOUBLE)
IF ((x <= 0.0) OR (LogBase <= 0.0)) THEN
AntiLogX# = 0.0
ELSE
AntiLogX# = EXP(Log(LogBase) * x)
END IF
END FUNCTION
REM ******************************************************************
REM * Finds the y'th root of x. *
REM ******************************************************************
FUNCTION Root#(x AS DOUBLE, y AS DOUBLE)
IF ((x <= 0.0) OR (y = 0.0)) THEN
Root# = 0.0
ELSE
Root# = EXP(LOG(x) / y)
END IF
END FUNCTION
REM ******************************************************************
REM * Raise x to the power of y. *
REM ******************************************************************
FUNCTION Power#(x AS DOUBLE, y AS DOUBLE)
IF ((x <= 0.0) OR (y = 0.0)) THEN
Power# = 0.0
ELSE
Power# = EXP(LOG(x) * y)
END IF
END FUNCTION
REM ******************************************************************
REM * Returns x' = x * (x - 1) * (x - 2) ... * 1. Non-recursive! *
REM ******************************************************************
FUNCTION Factorial#(x AS DOUBLE)
WorkingValue# = 2.0
ReturnValue# = 1.0
DO WHILE (WorkingValue# <= x)
ReturnValue# = ReturnValue# * WorkingValue#
WorkingValue# = WorkingValue# + 1.0
LOOP
Factorial# = ReturnValue#
END FUNCTION
REM ******************************************************************
REM * Returns the arithmetic mean (average) and the sample standard *
REM * deviation of the values held in the array In. *
REM ******************************************************************
SUB MeanDeviation(Mean AS DOUBLE, StandardDeviation AS DOUBLE, In#())
Sum# = 0.0#
SumOfSquares# = 0.0#
StartIndex% = LBOUND(In#)
NumberOfValues% = UBOUND(In#)
FOR Index% = StartIndex% TO NumberOfValues%
Sum# = Sum# + In#(Index%)
SumOfSquares# = SumOfSquares# + In#(Index%) * In#(Index%)
NEXT Index%
Mean = Sum# / NumberOfValues
StandardDeviation = SQR(SumOfSquares# - Sum# * Sum# / NumberOfValues) / (NumberOfValues - 1.0)
END SUB
REM ******************************************************************
REM * Returns a random number that is constrained to have a *
REM * greater probability of falling within the centre of upper and *
REM * lower bounds. *
REM ******************************************************************
FUNCTION GaussianRnd#(DesiredMean#, DesiredDeviation#)
RandomSum# = 0#
FOR x% = 1 TO 12
RandomSum# = RandomSum# + RND
NEXT x%
GausianRnd# = (RandomSum# - 6#) * DesiredDeviation# + DesiredMean#
END FUNCTION
Hope you have a use for these.
TR