Difference between revisions of "Mathematical Operations"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
imported>Clippy
Line 95: Line 95:
 
<center>'''Note: To convert from degrees to radians use: radians = degrees * (3.14159 / 180)'''</center>
 
<center>'''Note: To convert from degrees to radians use: radians = degrees * (3.14159 / 180)'''</center>
  
 +
 +
<center>[[#TOC|Return to Top]]</center>
 +
 +
==Derived Mathematical Functions==
 +
 +
{{TextStart}}'' ''
 +
FUNCTION SEC (x)  'Secant
 +
IF COS(x) <> 0 THEN SEC = 1 / COS(x) ELSE BEEP
 +
END FUNCTION
 +
 +
FUNCTION CSC (x)  'CoSecant
 +
IF SIN(x) <> 0 THEN CSC = 1 / SIN(x) ELSE BEEP
 +
END FUNCTION
 +
 +
FUNCTION COT (x)  'CoTangent
 +
IF TAN(x) <> 0 THEN COT = 1 / TAN(x) ELSE BEEP
 +
END FUNCTION
 +
 +
FUNCTION ARCSIN (x)  'Inverse Sine         
 +
IF 1 - (x * x) >= 0 AND SQR(1 - (x * x)) <> 0 THEN ARCSIN = ATN(x / SQR(1 - (x * x))) ELSE BEEP
 +
END FUNCTION
 +
 +
FUNCTION ARCCOS (x)  ' Inverse Cosine         
 +
IF 1 - (x * x) >= 0 AND SQR(1 - (x * x)) <> 0 THEN ARCCOS = (ATN(1) * 2) - ATN(x / SQR(1 - (x * x)) ELSE BEEP
 +
END FUNCTION
 +
 +
FUNCTION ARCSEC (x)  ' Inverse Secant       
 +
IF (x * x) - 1 >= 0 AND SQR((x * x) - 1) <> 0 THEN ARCSEC = ATN(x / SQR((x * x) - 1)) ELSE BEEP
 +
END FUNCTION
 +
 +
FUNCTION ARCCSC (x)  ' Inverse CoSecant
 +
IF (x * x) - 1 >= 0 AND SQR((x * x) - 1) + (SGN(x) - 1 * ATN(1) *  2) <> 0 THEN ARCCSC = ATN(x / SQR((x * x) - 1)) + (SGN(x) - 1 * ATN(1) *  2) ELSE BEEP
 +
END FUNCTION
 +
 +
FUNCTION ARCCOT (x)  ' Inverse CoTangent
 +
ARCCOT = ATN(x) + ATN(1) * 2
 +
END FUNCTION
 +
 +
FUNCTION SINH (x)  ' Hyperbolic Sine
 +
SINH = (EXP(x) - EXP(-x)) / 2
 +
END FUNCTION
 +
 +
FUNCTION COSH (x)  ' Hyperbolic CoSine
 +
COSH = (EXP(x) + EXP(-x)) / 2
 +
END FUNCTION
 +
 +
FUNCTION TANH (x)  ' Hyperbolic Tangent
 +
IF EXP(2 * x) + 1 <> 0 THEN TANH = (EXP(2 * x) - 1) / (EXP(2 * x) + 1)  ELSE BEEP ' or SINH(x) / COSH(x)
 +
END FUNCTION
 +
 +
FUNCTION SECH (x)  ' Hyperbolic Secant
 +
IF (EXP(x) + EXP(-x)) <> 0 THEN SECH = 2 / (EXP(x) + EXP(-x))  ELSE BEEP  'or (COSH(x)) ^ -1
 +
END FUNCTION
 +
 +
FUNCTION CSCH (x)  ' Hyperbolic CoSecant
 +
IF (EXP(x) - EXP(-x)) <> 0 THEN CSCH = 2 / (EXP(x) - EXP(-x)) ELSE BEEP  'or (SINH(x)) ^ -1
 +
END FUNCTION
 +
 +
FUNCTION COTH (x)  ' Hyperbolic CoTangent
 +
IF EXP(2 * x) - 1 <> 0 THEN COTH = (EXP(2 * x) + 1) / (EXP(2 * x) - 1) ELSE BEEP  'or COSH(x) / SINH(x)
 +
END FUNCTION
 +
 +
FUNCTION ARSINH (x)  ' Inverse Hyperbolic Sine
 +
IF (x * x) + 1 >= 0 AND x + SQR((x * x) + 1) > 0 THEN ARSINH = LOG(x + SQR((x * x) + 1)) ELSE BEEP
 +
END FUNCTION
 +
 +
FUNCTION ARCOSH (x)  ' Inverse Hyperbolic CoSine
 +
IF x >= 1 AND (x * x) - 1 >= 0 AND x + SQR((x * x) - 1) > 0 THEN ARCOSH = LOG(x + SQR((x * x) - 1)) ELSE BEEP
 +
END FUNCTION
 +
 +
FUNCTION ARTANH (x)  ' Inverse Hyperbolic Tangent
 +
IF x < 1 AND (1 + x) / (1 - x) > 0 THEN ARTANH = LOG((1 + x) / (1 - x)) / 2 ELSE BEEP
 +
END FUNCTION
 +
 +
FUNCTION ARSECH (x)  ' Inverse Hyperbolic Secant
 +
IF x > 0 AND x <= 1 AND 1 - (x * x) >= 0 AND (SQR(1 - (x * x)) + 1) / x > 0 THEN ARSECH = LOG((SQR(1 - (x * x)) + 1) / x) ELSE BEEP 
 +
END FUNCTION
 +
 +
FUNCTION ARCSCH (x)  ' Inverse Hyperbolic CoSecant
 +
IF x <> 0 AND (x * x) + 1 >= 0 AND SQR((x * x) + 1) + (1 / x) > 0 THEN ARCSCH = LOG(SQR((x * x) + 1) + (1 / x)) ELSE BEEP
 +
END FUNCTION
 +
 +
FUNCTION ARCOTH (x)  ' Inverse Hyperbolic CoTangent
 +
IF x > 1 THEN ARCOTH = LOG((x + 1) / (x - 1)) / 2 ELSE BEEP
 +
END FUNCTION '' ''
 +
{{TextEnd}}
 +
{{WhiteStart}}
 +
'''Useful Function Relationships:'''
 +
 +
                                  COSH(-x) = COSH(x)
 +
                                  SINH(-x) = -SINH(x)
 +
                                   
 +
                                  SECH(-x) = SECH(x)
 +
                                  CSCH(-x) = -CSCH(x)
 +
                                  TANH(-x) = -TANH(x)
 +
                                  COTH(-x) = -COTH(x)
 +
 +
'''Inverse Function Relatonships:'''
 +
 +
                              ARSECH(x) = ARCOSH(x) ^ -1
 +
                              ARCSCH(x) = ARSINH(x) ^ -1
 +
                              ARCOTH(x) = ARTANH(x) ^ -1
 +
 +
'''Hyperbolic sine and cosine satisfy the Pythagorean trig. identity:'''
 +
 +
                          (COSH(x) ^ 2) - (SINH(x) ^ 2) = 1
 +
 +
{{WhiteEnd}}
  
 
<center>[[#TOC|Return to Top]]</center>
 
<center>[[#TOC|Return to Top]]</center>

Revision as of 22:14, 28 September 2010


Mathematical Symbols

Most of the BASIC math operators are ones that require no introduction. The addition, subtraction, multplication and division operators are ones commonly used as shown below:


Symbol Procedure Example Usage
+ Addition c = a + b
- Subtraction c = a - b
- Negation c = - a
* Multiplication c = a * b
/ Division c = a / b


BASIC can also use two other operators for INTEGER division. Integer division returns only whole number values. MOD remainder division returns a value only if an integer division cannot divide a number exactly. Returns 0 if exactly divisible.

Symbol Procedure Example Usage
\ Integer division c = a \ b
MOD Remainder division c = a MOD b
It is an error to divide by zero or to take the remainder modulo zero.


There is also an operator for exponential calculations. The exponential operator is used to raise a number's value to a designated exponent of itself. In QB the exponential return values are DOUBLE values. The SQR function can return a number's Square Root. For other roots the exponential operator can be used with fractions such as (1 / 3) designating the cube root of a number.

Note that the fraction should be parenthesized in order for it to be treated as a fraction rather than a division operation!


Symbol Procedure Example Usage
^ Exponent c = SQR(a ^ 2 + b ^ 2)
Return to Top

Basic's Order of Operations

When a normal calculation is made, BASIC works from left to right, but it does certain calculations in the following order:

  1. Exponential and exponential Root calculations including SQR.
  2. Negation (Note that this means that - 3 ^ 2 is treated as -(3 ^ 2) and not as (-3) ^ 2.)
  3. Multiplication and Division calculations
  4. Addition and Subtraction calculations

Sometimes a calculation may need BASIC to do them in another order or the calculation will return bad results. BASIC allows the programmer to decide the order of operations by using parenthesis around parts of the equation. BASIC will do those calculations first and the others from left to right in the normal operation order.

Basic's Mathematical Functions

Function Description
ABS(n) returns the absolute (positive) value of n (ABS(-5) = 5)
ATN(angle) returns the arctangent of an angle in radians. (π = 4 * ATN(1))
COS(angle) returns the cosine of an angle in radians. (horizontal component)
EXP(n) returns ex, where e is 2.718281828...
LOG(n) returns the natural logarithm of n (n > 0)
SGN(n) returns -1 if n < 0, 0 if n = 0, 1 if n > 0 (SGN(-5) = -1)
SIN(angle) returns the sine of an angle in radians. (vertical component)
SQR(n) returns the square root of a number. It is an error to pass SQR a negative value.
TAN(angle) returns the tangent of an angle in radians


Note: To convert from degrees to radians use: radians = degrees * (3.14159 / 180)


Return to Top

Derived Mathematical Functions

FUNCTION SEC (x) 'Secant IF COS(x) <> 0 THEN SEC = 1 / COS(x) ELSE BEEP END FUNCTION FUNCTION CSC (x) 'CoSecant IF SIN(x) <> 0 THEN CSC = 1 / SIN(x) ELSE BEEP END FUNCTION FUNCTION COT (x) 'CoTangent IF TAN(x) <> 0 THEN COT = 1 / TAN(x) ELSE BEEP END FUNCTION FUNCTION ARCSIN (x) 'Inverse Sine IF 1 - (x * x) >= 0 AND SQR(1 - (x * x)) <> 0 THEN ARCSIN = ATN(x / SQR(1 - (x * x))) ELSE BEEP END FUNCTION FUNCTION ARCCOS (x) ' Inverse Cosine IF 1 - (x * x) >= 0 AND SQR(1 - (x * x)) <> 0 THEN ARCCOS = (ATN(1) * 2) - ATN(x / SQR(1 - (x * x)) ELSE BEEP END FUNCTION FUNCTION ARCSEC (x) ' Inverse Secant IF (x * x) - 1 >= 0 AND SQR((x * x) - 1) <> 0 THEN ARCSEC = ATN(x / SQR((x * x) - 1)) ELSE BEEP END FUNCTION FUNCTION ARCCSC (x) ' Inverse CoSecant IF (x * x) - 1 >= 0 AND SQR((x * x) - 1) + (SGN(x) - 1 * ATN(1) * 2) <> 0 THEN ARCCSC = ATN(x / SQR((x * x) - 1)) + (SGN(x) - 1 * ATN(1) * 2) ELSE BEEP END FUNCTION FUNCTION ARCCOT (x) ' Inverse CoTangent ARCCOT = ATN(x) + ATN(1) * 2 END FUNCTION FUNCTION SINH (x) ' Hyperbolic Sine SINH = (EXP(x) - EXP(-x)) / 2 END FUNCTION FUNCTION COSH (x) ' Hyperbolic CoSine COSH = (EXP(x) + EXP(-x)) / 2 END FUNCTION FUNCTION TANH (x) ' Hyperbolic Tangent IF EXP(2 * x) + 1 <> 0 THEN TANH = (EXP(2 * x) - 1) / (EXP(2 * x) + 1) ELSE BEEP ' or SINH(x) / COSH(x) END FUNCTION FUNCTION SECH (x) ' Hyperbolic Secant IF (EXP(x) + EXP(-x)) <> 0 THEN SECH = 2 / (EXP(x) + EXP(-x)) ELSE BEEP 'or (COSH(x)) ^ -1 END FUNCTION FUNCTION CSCH (x) ' Hyperbolic CoSecant IF (EXP(x) - EXP(-x)) <> 0 THEN CSCH = 2 / (EXP(x) - EXP(-x)) ELSE BEEP 'or (SINH(x)) ^ -1 END FUNCTION FUNCTION COTH (x) ' Hyperbolic CoTangent IF EXP(2 * x) - 1 <> 0 THEN COTH = (EXP(2 * x) + 1) / (EXP(2 * x) - 1) ELSE BEEP 'or COSH(x) / SINH(x) END FUNCTION FUNCTION ARSINH (x) ' Inverse Hyperbolic Sine IF (x * x) + 1 >= 0 AND x + SQR((x * x) + 1) > 0 THEN ARSINH = LOG(x + SQR((x * x) + 1)) ELSE BEEP END FUNCTION FUNCTION ARCOSH (x) ' Inverse Hyperbolic CoSine IF x >= 1 AND (x * x) - 1 >= 0 AND x + SQR((x * x) - 1) > 0 THEN ARCOSH = LOG(x + SQR((x * x) - 1)) ELSE BEEP END FUNCTION FUNCTION ARTANH (x) ' Inverse Hyperbolic Tangent IF x < 1 AND (1 + x) / (1 - x) > 0 THEN ARTANH = LOG((1 + x) / (1 - x)) / 2 ELSE BEEP END FUNCTION FUNCTION ARSECH (x) ' Inverse Hyperbolic Secant IF x > 0 AND x <= 1 AND 1 - (x * x) >= 0 AND (SQR(1 - (x * x)) + 1) / x > 0 THEN ARSECH = LOG((SQR(1 - (x * x)) + 1) / x) ELSE BEEP END FUNCTION FUNCTION ARCSCH (x) ' Inverse Hyperbolic CoSecant IF x <> 0 AND (x * x) + 1 >= 0 AND SQR((x * x) + 1) + (1 / x) > 0 THEN ARCSCH = LOG(SQR((x * x) + 1) + (1 / x)) ELSE BEEP END FUNCTION FUNCTION ARCOTH (x) ' Inverse Hyperbolic CoTangent IF x > 1 THEN ARCOTH = LOG((x + 1) / (x - 1)) / 2 ELSE BEEP END FUNCTION

Useful Function Relationships: COSH(-x) = COSH(x) SINH(-x) = -SINH(x) SECH(-x) = SECH(x) CSCH(-x) = -CSCH(x) TANH(-x) = -TANH(x) COTH(-x) = -COTH(x) Inverse Function Relatonships: ARSECH(x) = ARCOSH(x) ^ -1 ARCSCH(x) = ARSINH(x) ^ -1 ARCOTH(x) = ARTANH(x) ^ -1 Hyperbolic sine and cosine satisfy the Pythagorean trig. identity: (COSH(x) ^ 2) - (SINH(x) ^ 2) = 1

Return to Top

Signed and Unsigned Numerical Values

Negative (signed) numerical values can affect calculations when using any of the BASIC operators. SQR cannot use negative values! There may be times that a calculation error is made using those negative values. The SGN function returns the sign of a value as -1 for negative, 0 for zero and 1 for unsigned positive values. ABS always returns an unsigned value.

  • SGN(n) returns the value's sign as -1, 0 or 1
  • ABS(n) changes negative values to positive ones
  • QB64: _UNSIGNED in a DIM, AS or _DEFINE statement for only positive values.

Mathematical Logical operators

The following logical operators compare numerical values using bitwise operations. The two numbers are compared by the number's Binary bits on and the result of the operation determines the value returned in decimal form. NOT checks one value and returns the opposite. It returns 0 if a value is not 0 and -1 if it is 0. See Binary for more on bitwise operations.


Truth table of the 6 BASIC Logical Operators


The results of the bitwise logical operations, where A and B are operands, and T and F indicate that a bit is set or not set:
Operands Operations
A B NOT B A AND B A OR B A XOR B A EQV B A IMP B
T T F T T F T T
T F T F T T F F
F T F F T T F T
F F T F F F T T
Relational Operations return negative one (-1, all bits set) and zero (0, no bits set) for true and false, respectively.
This allows relational tests to be inverted and combined using the bitwise logical operations.


BASIC can accept any + or - value that is not 0 to be True when used in an evaluation.


Return to Top

Basic's Rounding Functions

Rounding is used when the program needs a certain number value or type. There are 4 INTEGER or LONG Integer functions and one function each for closest SINGLE and closest DOUBLE numerical types. Closest functions use "bankers" rounding which rounds up if the decimal point value is over one half. Variable types should match the return value.
Name Description
INT(n) rounds down to lower Integer value whether positive or negative
FIX(n) rounds positive values lower and negative to a less negative Integer value
CINT(n) rounds to closest Integer. Rounds up for decimal point values over one half.
CLNG(n) rounds Integer or Long values to closest value like CINT.(values over 32767)
CSNG(n) rounds Single values to closest last decimal point value.
CDBL(n) rounds Double values to closest last decimal point value.
_ROUND rounds to closest numerical value in QB64 only.
Note: Each of the above functions define the value's type in addition to rounding the values.
Return to Top


Base Number Systems

Comparing the Base Numbering Systems Decimal (base 10) Binary (base 2) Hexadecimal (base 16) Octal (base 8) &B &H HEX$(n) &O OCT$(n) 0 0000 0 0 1 0001 1 1 2 0010 2 2 3 0011 3 3 4 0100 4 4 5 0101 5 5 6 0110 6 6 7 0111 7 7 -- maxed 8 1000 8 10 maxed-- 9 1001 9 11 10 1010 A 12 11 1011 B 13 12 1100 C 14 13 1101 D 15 14 1110 E 16 15 ------------- 1111 <--- Match ---> F ---------------- 17 -- max 2 16 10000 10 20 When the Decimal value is 15, the other 2 base systems are all maxed out! The Binary values can be compared to all of the HEX value digit values so it is possible to convert between the two quite easily. To convert a HEX value to Binary just add the 4 binary digits for each HEX digit place so: F A C E &HFACE = 1111 + 1010 + 1100 + 1101 = &B1111101011001101 To convert a Binary value to HEX you just need to divide the number into sections of four digits starting from the right(LSB) end. If one has less than 4 digits on the left end you could add the leading zeros like below: &B101011100010001001 = 0010 1011 1000 1000 1001 hexadecimal = 2 + B + 8 + 8 + 9 = &H2B889 See the Decimal to Binary conversion function that uses HEX$ on the &H page.


The OCT$ string function return can be converted to a decimal value using VAL("&O" + OCT$(n)).


The HEX$ string function return can be converted to a decimal value using VAL("&H" + HEX$(n)).


Return to Top

References


Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page