-
Notifications
You must be signed in to change notification settings - Fork 8
Math Library API
Convenience class for calculating a numerical average.
EXAMPLE
uses Math/Averager
local avg = Averager<<Int32>>()
avg.add( 48 )
avg.add( 49 )
avg.add( 51 )
avg.add( 52 )
@trace avg.sum # 200
@trace avg.count # 4
@trace avg.average # 50.0
| Name | Type | Description |
|---|---|---|
| count | Int32 | |
| sum | $DataType |
| Signature | Return Type | Description |
|---|---|---|
| average() | $DataType | |
| description() | String | |
| to<<Object>>() | Boxed<<Averager<<$DataType>>>> | |
| to<<String>>() | String | |
| add( value:$DataType ) | ||
| operator==( other:Averager<<$DataType>> ) | Logical |
| Signature | Return Type | Description |
|---|---|---|
| create( axis:XYZ, angle:Degrees ) | AxisAngle | |
| create( axis:XYZ, angle:Radians ) | AxisAngle |
| Name | Type | Description |
|---|---|---|
| angle | Radians | |
| axis | XYZ |
| Signature | Return Type | Description |
|---|---|---|
| description() | String | |
| to<<Object>>() | Boxed<<AxisAngle>> | |
| to<<String>>() | String | |
| operator==( other:AxisAngle ) | Logical |
Tracks the best (value,score) pair among all considered pairs.
EXAMPLE
# Find the index of the smallest real number in a list.
uses Math/Best
local numbers = [3.1, 0.2, 5.8, -1.0, 4.0]
local best = Best<<Int32,Real>>( (a,b) => a < b )
# The comparison function (a,b) should return true when score 'a' is better than score 'b'.
forEach (value at i in numbers)
best.consider( i, value )
endForEach
println "Lowest: numbers[$] = $" (best.value,best.score)
# Lowest: numbers[3] = -1.0
| Signature | Return Type | Description |
|---|---|---|
| create( better_fn:Function($ScoreType,$ScoreType)->Logical ) | Best<<$DataType,$ScoreType>> |
| Name | Type | Description |
|---|---|---|
| better_fn | Function($ScoreType,$ScoreType)->Logical | |
| exists | Logical | |
| score | $ScoreType | |
| value | $DataType |
| Signature | Return Type | Description |
|---|---|---|
| description() | String | |
| to<<Logical>>() | Logical | |
| to<<Object>>() | Boxed<<Best<<$DataType,$ScoreType>>>> | |
| to<<String>>() | String | |
| consider( candidate_value:$DataType, candidate_score:$ScoreType ) | Logical | Returns 'true' if a new best value is stored. |
| operator==( other:Best<<$DataType,$ScoreType>> ) | Logical |
Tracks the best value among all considered values.
EXAMPLE
# Find the smallest real number in a list.
uses Math/Best
local numbers = [3.1, 0.2, 5.8, -1.0, 4.0]
local best = Best<<Real>>( (a,b) => a < b )
forEach (value in numbers)
best.consider( value )
endForEach
println "Lowest: $" (best.value)
# Lowest: numbers[3] = -1.0
| Signature | Return Type | Description |
|---|---|---|
| create( better_fn:Function($DataType,$DataType)->Logical ) | Best<<$DataType>> |
| Name | Type | Description |
|---|---|---|
| better_fn | Function($DataType,$DataType)->Logical | |
| exists | Logical | |
| value | $DataType |
| Signature | Return Type | Description |
|---|---|---|
| description() | String | |
| to<<Logical>>() | Logical | |
| to<<Object>>() | Boxed<<Best<<$DataType>>>> | |
| to<<String>>() | String | |
| consider( candidate_value:$DataType ) | Logical | Returns 'true' if a new best value is stored. |
| operator==( other:Best<<$DataType>> ) | Logical |
extends Object
Allows integers of any size to be represented and manipulated. BigInt operations are significantly slower than operations on primitive Int32/Int64 types so BigInt objects should only be used when you require integers with more than 64 bits.
BigInt objects are immutable; operations produce new BigInt objects as side effects instead of altering operands.
BigInt numbers are modeled using sign-magnitude; they are not 2's complement and the sign cannot be directly changed through a bitwise operation (only incidentally, as when a negative number is AND'd with zero to produce a zero result).
BigInt values are stored as a series of unsigned 16-bit Int32 values. Their total bit size (in multiples of 16) is only what's necessary to represent a number's magnitude.
Similar to Java's BigInt class.
Examples:
println( BigInt(123456) ^ BigInt("1000101",2) )
println( "2^100 = $" (BigInt(2) ^ 100) )
| Name | Type | Description |
|---|---|---|
| i64_limit | BigInt | |
| one | BigInt | |
| setup_complete | Logical | |
| ten | BigInt | |
| ten_e144 | BigInt | |
| ten_e18 | BigInt | |
| ten_e36 | BigInt | |
| ten_e72 | BigInt | |
| values | BigInt[] | |
| zero | BigInt |
| Signature | Return Type | Description |
|---|---|---|
| init_class() | ||
| create( n:Int32 ) | BigInt | |
| create( n:Int64 ) | BigInt | Creates a BigInt object out of an Int64 value. If 'n' is 0..15 then a pre-defined BigInt object will be returned instead. |
| Name | Type | Description |
|---|---|---|
| data | Int32[] | |
| sign_flag | Int32 | 1 or -1 |
| Signature | Return Type | Description |
|---|---|---|
| init( other:BigInt ) | ||
| init( value:Int64, new_object:Logical ) | ||
| init( value:String, [base=10:Int32] ) | Initializes this BigInt to a value specified as a string of digits in a given base. 'base' can be 2, 10, or 16. | |
| bits() | Int32 | |
| is_negative() | Logical | |
| is_valid_int32() | Logical | |
| is_valid_int64() | Logical | |
| is_zero() | Logical | |
| normalized() | BigInt | Removes excess zero values on the most signficant end and changes a negative zero to a positive zero. |
| operator-() | BigInt | |
| sign() | Int32 | Returns -1, 0, or 1. |
| sqrt() | BigInt | |
| to<<Int32>>() | Int32 | |
| to<<Int64>>() | Int64 | |
| to<<String>>() | String | |
| divide_and_mod( n:BigInt ) | BigIntDivideAndModResult | |
| divide_and_mod( n:Int64 ) | BigIntDivideAndModResult | |
| format( fmt:String ) | String |
fmt - #: right-justify in this many spaces; 0 fill if begins with 0 - ,: put comma separators |
| operator%( n:BigInt ) | BigInt | |
| operator%( n:Int64 ) | BigInt | |
| operator&( n:BigInt ) | BigInt | |
| operator&( n:Int64 ) | BigInt | |
| operator*( n:BigInt ) | BigInt | |
| operator*( n:Int64 ) | BigInt | |
| operator+( n:BigInt ) | BigInt | |
| operator+( n:Int64 ) | BigInt | |
| operator-( n:BigInt ) | BigInt | |
| operator-( n:Int64 ) | BigInt | |
| operator/( n:BigInt ) | BigInt | |
| operator/( n:Int64 ) | BigInt | |
| operator:<<:( bits:Int32 ) | BigInt | |
| operator:>>:( bits:Int32 ) | BigInt | |
| operator:>>>:( bits:Int32 ) | BigInt | |
| operator<>( n:Int64 ) | Int32 | |
| operator<>( other:BigInt ) | Int32 | |
| operator==( n:Int64 ) | Logical | |
| operator==( other:BigInt ) | Logical | |
| operator^( n:BigInt ) | BigInt | |
| operator^( n:Int64 ) | BigInt | |
| operator|( n:BigInt ) | BigInt | |
| operator|( n:Int64 ) | BigInt | |
| operator~( n:BigInt ) | BigInt | |
| operator~( n:Int64 ) | BigInt | |
| to<<String>>( &binary, &hex ) | String | |
| to<<String>>( base:Int32 ) | String |
| Signature | Return Type | Description |
|---|---|---|
| create( division:BigInt, modulo:BigInt ) | BigIntDivideAndModResult |
| Name | Type | Description |
|---|---|---|
| division | BigInt | |
| modulo | BigInt |
| Signature | Return Type | Description |
|---|---|---|
| description() | String | |
| to<<Object>>() | Boxed<<BigIntDivideAndModResult>> | |
| to<<String>>() | String | |
| operator==( other:BigIntDivideAndModResult ) | Logical |
extends Object
| Signature | Return Type | Description |
|---|---|---|
| create( degrees:Radians ) | Degrees | |
| create( value:Real64 ) | Degrees | |
| operator?( degrees:Degrees ) | Logical |
| Name | Type | Description |
|---|---|---|
| value | Real64 |
| Signature | Return Type | Description |
|---|---|---|
| cos() | Real64 | |
| description() | String | |
| floor() | Degrees | |
| operator-() | Degrees | |
| sin() | Real64 | |
| tan() | Real64 | |
| to<<Object>>() | Boxed<<Degrees>> | |
| to<<Radians>>() | Radians | |
| to<<Real64>>() | Real64 | |
| to<<String>>() | String | |
| clamped( min:Degrees, max:Degrees ) | Degrees | |
| delta_to( other:Degrees ) | Degrees | Returns the smallest number of degrees necessary to get from this angle to the specified other angle. For example, Degrees(270).delta_to(Degrees(0)) yields Degrees(90), since turning 90 degrees will get you to 0 degrees "faster" than turning -270 degrees. |
| operator%( degrees:Real64 ) | Degrees | |
| operator%( other:Degrees ) | Degrees | |
| operator*( degrees:Real64 ) | Degrees | |
| operator*( other:Degrees ) | Degrees | |
| operator+( degrees:Real64 ) | Degrees | |
| operator+( other:Degrees ) | Degrees | |
| operator-( degrees:Real64 ) | Degrees | |
| operator-( other:Degrees ) | Degrees | |
| operator/( degrees:Real64 ) | Degrees | |
| operator/( other:Degrees ) | Degrees | |
| operator<>( other:Degrees ) | Real64 | |
| operator<>( other_value:Real64 ) | Real64 | |
| operator==( other:Degrees ) | Logical | |
| operator==( other_value:Real64 ) | Logical | |
| operator^( degrees:Real64 ) | Degrees | |
| operator^( other:Degrees ) | Degrees |
extends Object
| Signature | Return Type | Description |
|---|---|---|
| create() | Matrix | |
| identity() | Matrix | |
| create( r0c0:Real64, r1c0:Real64, r2c0:Real64, r3c0:Real64, r0c1:Real64, r1c1:Real64, r2c1:Real64, r3c1:Real64, r0c2:Real64, r1c2:Real64, r2c2:Real64, r3c2:Real64, r0c3:Real64, r1c3:Real64, r2c3:Real64, r3c3:Real64 ) | Matrix | |
| decimal_digit_count( value:Real64 ) | Int32 | |
| look_at( origin:XYZ, target:XYZ, up:XYZ ) | Matrix | |
| perspective( fov_y:Radians, aspect_ratio:Real64, z_near:Real64, z_far:Real64 ) | Matrix | Returns a right-handed projection matrix (OpenGL is also right-hand). |
| perspective( left:Real64, top:Real64, right:Real64, bottom:Real64, near:Real64, far:Real64 ) | Matrix | Based on https://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml |
| rotate( degrees:DegreesXYZ ) | Matrix | |
| rotate( radians:RadiansXYZ ) | Matrix | |
| rotate( theta:Radians, axis:XYZ ) | Matrix | |
| rotate_x( theta:Radians ) | Matrix | |
| rotate_y( theta:Radians ) | Matrix | |
| rotate_z( theta:Radians ) | Matrix | |
| scale( k:Real64 ) | Matrix | |
| scale( k:Real64, origin:XY ) | Matrix | |
| scale( k:XY ) | Matrix | |
| scale( k:XY, origin:XY ) | Matrix | |
| scale( k:XYZ ) | Matrix | |
| scale( k:XYZ, origin:XYZ ) | Matrix | |
| shear( x_yz:XY, y_xz:XY, z_xy:XY ) | Matrix | x_yz: amount that x is sheared proportional to changes in y and z y_xz: amount that y is sheared proportional to changes in x and z z_xy: amount that z is sheared proportional to changes in x and y |
| shear( xy:XY ) | Matrix | |
| transform( position:XYZ, size:XY, anchor:Anchor, rotation:Radians ) | Matrix | Returns a transformation matrix that will position a [0,0 1x1] coordinate box in 2DX space - everything is in 2D except for the Z coordinate that is applied at the end to move the image closer to (z > 0) or farther away from (z < 0) the camera. |
| transform( size:XY, anchor:Anchor ) | Matrix |
Performs a partial 2D transform for size and anchor. The typical full tranform set for a [0,0 1x1] box in the order they'd be pushed on a transform stack: translate( position ) rotate_z( rotation ) scale( size ) # peformed by this method translate( -anchor.position ) # peformed by this method Use transform(position,size,anchor,rotation) to perform all transforms at once in 2DX space. Use this method combined with other transforms if other transformations are needed, such as rotation around the x or y axis. |
| translate( delta:XY ) | Matrix | |
| translate( delta:XYZ ) | Matrix | |
| whole_digit_count( value:Real64 ) | Int32 |
| Name | Type | Description |
|---|---|---|
| r0c0 | Real64 | |
| r0c1 | Real64 | |
| r0c2 | Real64 | |
| r0c3 | Real64 | |
| r1c0 | Real64 | |
| r1c1 | Real64 | |
| r1c2 | Real64 | |
| r1c3 | Real64 | |
| r2c0 | Real64 | |
| r2c1 | Real64 | |
| r2c2 | Real64 | |
| r2c3 | Real64 | |
| r3c0 | Real64 | |
| r3c1 | Real64 | |
| r3c2 | Real64 | |
| r3c3 | Real64 |
| Signature | Return Type | Description |
|---|---|---|
| count() | Int32 | |
| description() | String | |
| to<<Matrix32>>() | Matrix32 | |
| to<<Object>>() | Boxed<<Matrix>> | |
| to<<String>>() | String | |
| get( index:Int32 ) | Real64 | |
| operator*( other:Matrix ) | Matrix | |
| operator*( v:XY ) | XY | |
| operator*( v:XYZ ) | XYZW | |
| operator*( v:XYZW ) | XYZW | |
| operator==( other:Matrix ) | Logical | |
| set( index:Int32, value:Real64 ) |
| Signature | Return Type | Description |
|---|---|---|
| create( r0c0:Real32, r1c0:Real32, r2c0:Real32, r3c0:Real32, r0c1:Real32, r1c1:Real32, r2c1:Real32, r3c1:Real32, r0c2:Real32, r1c2:Real32, r2c2:Real32, r3c2:Real32, r0c3:Real32, r1c3:Real32, r2c3:Real32, r3c3:Real32 ) | Matrix32 |
| Name | Type | Description |
|---|---|---|
| r0c0 | Real32 | |
| r0c1 | Real32 | |
| r0c2 | Real32 | |
| r0c3 | Real32 | |
| r1c0 | Real32 | |
| r1c1 | Real32 | |
| r1c2 | Real32 | |
| r1c3 | Real32 | |
| r2c0 | Real32 | |
| r2c1 | Real32 | |
| r2c2 | Real32 | |
| r2c3 | Real32 | |
| r3c0 | Real32 | |
| r3c1 | Real32 | |
| r3c2 | Real32 | |
| r3c3 | Real32 |
| Signature | Return Type | Description |
|---|---|---|
| description() | String | |
| to<<Matrix>>() | Matrix | |
| to<<Object>>() | Boxed<<Matrix32>> | |
| to<<String>>() | String | |
| operator==( other:Matrix32 ) | Logical |
Adapted from the Iirlicht Quaternion by Nikolaus Gebhardt
| Signature | Return Type | Description |
|---|---|---|
| identity() | Quaternion | |
| create( [x=0:Real64], [y=0:Real64], [z=0:Real64], [w=1.0:Real64] ) | Quaternion | |
| create( aa:AxisAngle ) | Quaternion | |
| create( from:XYZ, to:XYZ ) | Quaternion | |
| create( m:Matrix ) | Quaternion | |
| create( rotation:RadiansXYZ ) | Quaternion |
| Name | Type | Description |
|---|---|---|
| w | Real64 | |
| x | Real64 | |
| y | Real64 | |
| z | Real64 |
| Signature | Return Type | Description |
|---|---|---|
| description() | String | |
| inverted() | Quaternion | |
| is_identity() | Logical | |
| normalized() | Quaternion | |
| to<<AxisAngle>>() | AxisAngle | |
| to<<Matrix>>() | Matrix | |
| to<<Object>>() | Boxed<<Quaternion>> | |
| to<<RadiansXYZ>>() | RadiansXYZ | |
| to<<String>>() | String | |
| dot( other:Quaternion ) | Real64 | |
| linear_tween( target:Quaternion, t:Real64 ) | Quaternion | |
| matches( other:Quaternion, tolerance:Real64 ) | Logical | |
| operator*( n:Real64 ) | Quaternion | |
| operator*( other:Quaternion ) | Quaternion | |
| operator*( v:XYZ ) | XYZ | |
| operator+( other:Quaternion ) | Quaternion | |
| operator/( n:Real64 ) | Quaternion | |
| operator==( other:Quaternion ) | Logical | |
| rotated( by_angles:RadiansXYZ ) | Quaternion | |
| to<<Matrix>>( handle:XYZ, translate:XYZ ) | Matrix | |
| tween( target:Quaternion, t:Real64, [threshold=0.05:Real64] ) | Quaternion |
| Signature | Return Type | Description |
|---|---|---|
| create( degrees:Degrees ) | Radians | |
| create( value:Real64 ) | Radians | |
| operator?( radians:Radians ) | Logical |
| Name | Type | Description |
|---|---|---|
| value | Real64 |
| Signature | Return Type | Description |
|---|---|---|
| cos() | Real64 | |
| description() | String | |
| operator-() | Radians | |
| sin() | Real64 | |
| tan() | Real64 | |
| to<<Degrees>>() | Degrees | |
| to<<Object>>() | Boxed<<Radians>> | |
| to<<Real64>>() | Real64 | |
| to<<String>>() | String | |
| clamped( min:Radians, max:Radians ) | Radians | |
| delta_to( other:Radians ) | Radians | Returns the smallest number of radians necessary to get from this angle to the specified other angle. For example, Radians(pi*3/2).delta_to(Radians(0)) yields Radians(pi/2), since turning pi/2 radians will get you to 0 radians "faster" than turning -pi*3/2 radians. |
| operator%( other:Radians ) | Radians | |
| operator%( radians:Real64 ) | Radians | |
| operator*( other:Radians ) | Radians | |
| operator*( radians:Real64 ) | Radians | |
| operator+( other:Radians ) | Radians | |
| operator+( radians:Real64 ) | Radians | |
| operator-( other:Radians ) | Radians | |
| operator-( radians:Real64 ) | Radians | |
| operator/( other:Radians ) | Radians | |
| operator/( radians:Real64 ) | Radians | |
| operator<>( other:Degrees ) | Real64 | |
| operator<>( other_value:Real64 ) | Real64 | |
| operator==( other:Degrees ) | Logical | |
| operator==( other:Radians ) | Logical | |
| operator==( other_value:Real64 ) | Logical | |
| operator^( other:Radians ) | Radians | |
| operator^( radians:Real64 ) | Radians |
extends Object
This random number generator produces evenly distributed pseudo-random values.
You may create a Random object with a 'seed' value; generators created with the same seed always return the same sequence of random numbers. If a seed is not specified then the current System.time_ms is used.
Underneath, the Random operates on a 48-bit integer using the linear congruence formula with the "drand48" parameters:
next = (a * prev + c) mod (2^48)
Where a=0x5DEECE66D, c=11, and bits 31:0 of 'next' are retrieved with each read call.
| Name | Type | Description |
|---|---|---|
| state | Int64 |
| Signature | Return Type | Description |
|---|---|---|
| init() | ||
| init( seed:Int64 ) | ||
| byte() | Byte | Returns a random byte between 0 and 255. |
| color() | Color | Returns a color with randomized (r,g,b) values and an alpha of 1.0. |
| degrees() | Degrees | |
| gaussian() | Real64 | Returns a random number from a normal distribution of avg=0, stddev=1 68.2% chance of being between -1..1 95.45% chance of being between -2..2 99.73% chance of being -3..3 99.994% chance of being -4..4 Real number precision limits the results to [-8.5,8.5] From: https://stackoverflow.com/a/218600/135791 |
| int32() | Int32 | Returns a normalized, evenly distributed random integer in the range [0,2^31-1] inclusive. |
| int64() | Int64 | Returns a normalized, evenly distributed random integer in the range [0,2^63-1] inclusive. |
| logical() | Logical | Returns either "true" or "false" with equal likelihood. |
| radians() | Radians | |
| real() | Real64 | Returns a random number 0 < n < 1.0 |
| real32() | Real32 | Returns a random number 0 < n < 1.0 |
| real64() | Real64 | Returns a random number 0 < n < 1.0 |
| xyz() | XYZ | Creates a XYZ compound with randomized (x,y,z) values, each in the range (0.0,1.0) exclusive. |
| xyzw() | XYZW | Creates a XYZW compound with randomized (x,y,z,w) values, each in the range (0.0,1.0) exclusive. |
| chance( p:Real64 ) | Logical |
Has a proportional chance 'p' to return true (0 < p < 1.0). Example: Random.chance(0.75) # returns true 75% of the time statistically speaking. |
| int32( limit:Int32 ) | Int32 |
Returns an evenly distributed random integer in the range [0,limit) - includes 0 but does not include limit. Example: println( random_Int32(100) ) #prints a number 0..99 |
| int32( low:Int32, high:Int32 ) | Int32 | Returns a random integer between low and high, inclusive. |
| int_xy( limit:Int32 ) | IntXY | |
| int_xy( limit:IntXY ) | IntXY | |
| real( limit:Real64 ) | Real64 | Returns a random number 0 < n < limit |
| real( low:Real64, high:Real64 ) | Real64 | Returns a random number low < n < high |
| real32( limit:Real32 ) | Real32 | Returns a random number 0 < n < limit |
| real32( low:Real32, high:Real32 ) | Real32 | Returns a random number low < n < high |
| real64( limit:Real64 ) | Real64 | Returns a random number 0 < n < limit |
| real64( low:Real64, high:Real64 ) | Real64 | Returns a random number low < n < high |
| reset( _state:Int64 ) | ||
| xy( limit:Real64 ) | XY | |
| xy( limit:XY ) | XY | |
| xyz( low:Real64, high:Real64 ) | XYZ | Returns a XYZ compound with an evenly distributed random real number in the range (low,high) exclusive (does not include low or high). for each of the (x,y,z) values. |
| xyzw( low:Real64, high:Real64 ) | XYZW | Returns a XYZW compound with an evenly distributed random real number in the range (low,high) exclusive (does not include low or high). for each of the (x,y,z,w) values. |