Table of Contents

6.1.2.10 Math

Note

This section describes the implementation of MS-VBAL §6.1.2.10 Math.

The Math module is represented in the language core by the interface IStdMathModule.

The Math module's declarations follow MS-VBAL's own Function Declaration blocks (MS-VBAL §6.1.2.10), declaration by declaration, including their return types and their Optional parameters.

The specification says little more of these functions than what each one computes: the type of most results, the errors, and the whole of Rnd and Randomize are left to the implementation. RD-VBA does what MS-VBA does, recorded in Excel (64-bit, en-US), and the notes below say what that is.

An argument declared Double is Let-coerced to Double by the call, before the function sees it: Null is error 94, a string that is not a number is error 13, and Empty is 0. An object, given to any parameter, is read as its simple data value - what its default member returns - as for the Conversion module (RD-VBAL §6.1.2.3 An object argument): Abs of an object whose default member returns Null is Null, and Nothing is error 91.

6.1.2.10.1 Public Functions

Note

This section describes the implementation of MS-VBAL §6.1.2.10.1 Public Functions.

§ Member Notes
6.1.2.10.1.1 Abs The result has the type of the argument: a Date stays a Date, and a Boolean is an Integer (Abs(True) is 1). The smallest value of a type has no absolute value of that type, and is error 6: of a Long variable that holds -2147483648 too (recorded in Excel, oracle\s60 A01, A03). MS-VBA evaluates a constant expression (MS-VBAL §5.6.16.1) when it compiles the code, and answers Abs of the smallest Long in one with that same negative Long: Abs(CLng(-2147483647) - 1), Abs(&H80000000), Abs(K) of a Const (A01, A02). A Variant that holds the smallest Long has the absolute value the Double 2147483648, as a Variant's arithmetic widens (A03). RD-VBA does what MS-VBA does. The call itself is of the argument's declared type when that is a number, as MS-VBA types it where the specification declares As Variant: Abs(n) * 7000 of an Integer is an Integer product, which overflows (RDC11018).
6.1.2.10.1.2 Atn
6.1.2.10.1.3 Cos An angle of 263 radians or more, either sign, is error 5. Otherwise the result is the platform's, within a unit of the last place of the true value as MS-VBA's is, and MS-VBA's to the last bit for all but about one argument in forty (recorded in Excel, oracle\s43: Cos(1) is one).
6.1.2.10.1.4 Exp "An error occurs" is error 6: the result would be greater than the largest Double.
6.1.2.10.1.5 Log A number not greater than zero is error 5.
6.1.2.10.1.6 Rnd MS-VBA's generator, so that a seeded sequence is MS-VBA's: a 24-bit linear congruential generator (×&H43FD43FD, +&HC39EC3, modulo 224) whose state is the last number it generated. A negative number seeds it with the bits of the Single it is. Its state is the session's (ISessionRandomNumberGenerator), and starts where a new MS-VBA process does: the first Rnd is 0.7055475. An End resets it with the rest of the session. The argument is Let-coerced to Single.
6.1.2.10.1.7 Round A tie rounds to even. A Double is scaled to the digits it keeps, rounded, and scaled back, so a tie is a tie as the scaled Double is: Round(2.345, 2) is 2.35, and Round(1.005, 2) is 1. A Currency and a Decimal round as the decimal numbers they are (Round(CCur(2.345), 2) is 2.34). The result has the type of the argument, a Boolean as an Integer; a String is Let-coerced to Double. A negative number of digits is error 5, Null included. More than 22 digits is error 5 for anything rounded as a Double (a Single, a Date, a String); a whole number, a Currency and a Decimal take any number of digits. A Currency rounded past the largest one is error 6. Round(-0.5) is a negative zero, -0.
6.1.2.10.1.8 Sgn An Integer, of any number, Date, Boolean or numeric String. Null is error 94.
6.1.2.10.1.9 Sin As Cos.
6.1.2.10.1.10 Sqr A negative number is error 5; the square root of 0 is 0.
6.1.2.10.1.11 Tan As Cos. The tangent is MS-VBA's: the sine over the cosine, divided in 80 bits; the platform's own tangent differs from it in the last bit for one argument in three.

6.1.2.10.2 Public Subroutines

Note

This section describes the implementation of MS-VBAL §6.1.2.10.2 Public Subroutines.

§ Member Notes
6.1.2.10.2.1 Randomize The argument is Let-coerced to Double, and the high half of its bits is folded into the upper two bytes of the generator's state; the low byte stays what it was, so what follows depends on where the generator was. An omitted argument seeds it from the session's clock (Timer). The specification seeds from the timer for Empty too; MS-VBA Let-coerces Empty like any other argument, to 0, and RD-VBA does what MS-VBA does.