A library for math, conversion, arrays, objects and other miscellaneous functions/uses.
Installation
[ Content ]
Documentation & Content
A sorted and cleaner version of the documentation can be found here. https://tjksec.github.io/Mathf/docs/ All functions and methods
- sqrd
- pwr
- toNotationStr
- toNotation
- fromNotation
- cm2ft
- ft2cm
- gall2litre
- litre2gall
- radians
- degrees
- PI
- isPwrTwo
- decimalToBin
- txtToAsc
- ascToTxt
- logBase
- E
- toFahrenheit
- toKelvin
- random
- seed
- parityArray
- median
- range
- mean
- parity
- toJSON
- add
- multiply
- expand
- aprox
- arrClampValue
- arrLock
- toObject
- flatten
- lose
- break
- isVacant
- isolate
- v
- k
- holdsKey
- holdsVal
- tally
- flip
- randomArr
- insert
- init
- app
- grabVal
- grabKey
Usage and Information
Mathf has no dependency's. It can be used by calling the object Mathf with your desired function and parameter. Here are some examples of each of the different methods...
sqrd
Squares the parameter.
Mathf // 64Mathf // 4Mathf // 10000
pwr
First parameter to the power of the second.
Mathf // 512Mathf // 49Mathf // 815730721
toNotationStr
[ STILL UNDER DEVELOPMENT ]
Will turn your given parameter into a scientific notation but will be in the form of a string. Note: The number between the parentheses is the power of the number beside it, for example 10(7) would be 10 to the power of 7.
Your parameter must be surrounded with quotes for this to work properly.
Mathf // 7.1312321 * 10(7)Mathf // 9.73621 * 10(-4)Mathf // 8.88888 * 10(5) /* Known Issues */Mathf // will return incorrect value.Mathf // will also return incorrect value.
toNotation
This will only return the float of the parameter in scientific notation form, without including the tenth power required to revert it back to its original self.
Again, your parameter must be surrounded with quotes for this to work properly.
Mathf // 9.73621Mathf // 3.4324343223545646Mathf // 8.88888
fromNotation
[ COMING SOON || FIXING BUGS ]
cm2ft
Converts parameter (cm) into (ft)
Mathf // 0.32808400000000004Mathf // 1.000000032Mathf // 32.7755916
ft2cm
Converts parameter (ft) into (cm)
Mathf // 304.8Mathf // 929.0304Mathf // 30449.52
gall2litre
Converts parameter (gallon) into (litre)
Mathf // 37.8541Mathf // 115.3792968Mathf // 3781.6245900000004
litre2gall
Converts parameter (litre) into (gallon)
Mathf // 2.6417200000000003Mathf // 8.051962560000002Mathf // 263.907828
radians
Converts parameter (degree) into (radian)
Mathf // 0.17453300000000002Mathf // 0.531976584Mathf // 1.5707970000000002
degree
Converts parameter (radian) into (degree)
Mathf // 572.958Mathf // 1746.375984Mathf // 5156.622
PI
Value of PI
MathfPI // 3.14159265359
clamp
Clamps a given parameter between a minimum and maximum int.
Mathf // 5Mathf // 3Mathf // 1
isPwrTwo
Checks if a parameter is a power of two, if it is, it will return true
.
Mathf // trueMathf // trueMathf // false
decimalToBin
Converts decimal to binary.
Mathf // 1100011Mathf // 1000110111111Mathf // 10011
txtToAsc
Converts text to ascii. This will only convert letters, using numbers or symbols will not work.
Mathf // 72,101,108,108,111Mathf // 77,97,116,104,102,32,105,115,32,116,104,101,32,98,101,115,116Mathf // 100,117,116,121,97,115,118,100,116,97,103,119,98
ascToTxt
Converts ascii to text.
Mathf // HelloMathf // Mathf is the bestMathf // dutyasvdtagwb
logBase
Get base (parameter) logarithm of (parameter). Logarithms in javascript arent always perfectly accurate but are only usually slightly off.
Mathf // 2.9999999999999996 (Note: Would be 3 if js Logarithms were perfect)Mathf // 0.5283208335737187Mathf // 0.6309297535714574
E
The value of E.
MathfE // 2.7182818284590452353602875
toFahrenheit
Converts Celsius to Fahrenheit
Mathf // 212Mathf // 158Mathf // 86
toKelvin
Converts Celsius to Kelvin
Mathf // 373Mathf // 343Mathf // 303
random
Chooses a random number between parameter one and parameter two.
Mathf // 3Mathf // 10Mathf // 33
seed
Generates random seed of numbers with length set to given parameter.
Mathf // 74Mathf // 4828821837Mathf // 818258243146673822531264199559
parityArray
Returns true
if the amount of items in an array is an equal number.
Mathf // falseMathf // trueMathf // true
median
Calculates the median of a numerical array.
Mathf // 3Mathf // 3.5Mathf // 2
range
Calculates the range of a numerical array.
Mathf // 5Mathf // 79Mathf // 8423
avg
Calculates the arithmetic mean/average of a numerical array.
Mathf // 5.8Mathf // 2Mathf // 18.333333333333332
parity
Returns true
if a number is even and false
if it isn't.
Mathf // trueMathf // falseMathf // true
toJSON
Converts array or object to JSON standards.
Mathf // {"one":"two"}Mathf // ["hello","hi"]Mathf // ["1","2","3"]
add
Adds all numerical values in an array together.
Mathf // 6Mathf // 9Mathf // 32
multiply
Multiplies all numerical values in an array by each other.
Mathf // 27Mathf // 49Mathf // 2916
expand
Converts given integer into an array of 1's.
Mathf // [1,1,1,1,1,1,1,1,1,1]Mathf // [1,1,1,1,1]Mathf // [1,1,1]
aprox
Compares two numerical values to see if they are similar.
Mathf // trueMathf // falseMathf // true
arrClampValue
Clamps the values of each numerical value in an array between (min) and (max).
Mathf // [3,3,3,4,5,6,6,6]Mathf // [2,999,999,999,0]Mathf // [1,2,3,4,5]
arrLock
Returns all array items within the maximum limit given.
Mathf // [1,2,3]Mathf // [99,88,342,-234]Mathf // []
toObject
Converts every two array items into an objects key and value. If the array is not equal
there will not be a value for one of the keys so it will return null
.
const newObj = Mathf; return newObjKey0 // { Val0 }
const newObj = Mathf; return newObjHello // { Bye }
const newObj = Mathf // null
flatten
This will conjoin mulitple arrays within an array.
Mathf // [1,2,3,4,"one","two",3]Mathf // ["hello",38127649]Mathf // [10,11,12,13,19,20]
lose
This will remove the given key/s from an object.
/* We will create an object template to demonstrate use. */ { thisname = name thisage = age thisdepartment = department};
/* We'll create default object... */const original = "John"21"Software Engineer";const x = "John"21"Software Engineer"; /* and our object with our key/s removed.. */const flitered = Mathf;
return ` | | ` // 21 | John | Software Engineer return ` | | ` // undefined | undefined | Software Engineer
break
This will break an array into smaller arrays with the capacity of parameter.
Mathf // [ [1,2], [3,4], [5,6], [7] ] Mathf // [ [1,2,3,4,5,6,7] ] Mathf // [ [1,2,3,4], [5,6,-20] ]
isVacant
This will return true
if the array is empty
Mathf // trueMathf // falseMathf
isOccupied
This will return true
if the array is not empty
Mathf // falseMathf // trueMathf
isolate
This will remove any duplicates from an array
Mathf // [1,2,3,4,55,5]Mathf // [a,b]Mathf "a"1"b""c"
v
This will return all values from an object and stores them inside an array.
Mathf // ["O-N-E", "T-W-O"]Mathf // [1,2,3,4]Mathf // ["(sayhi) => {alert(sayhi)}","Two",3]
k
This will return all keys from an object and stores them inside an array.
Mathf // ["One", "Two"]Mathf // ["One","Two","Three","Four"]Mathf // ["A","One","Three"]
holdsKey
This will return true if the given key exists within an object.
Mathf // trueMathf // falseMathf // false
tally
This will return the amount of items within an array.
Mathf // 4Mathf // 3Mathf // 8
flip
This will reverse/flip an array and return it.
Mathf // [6,5,4,3,2,1]Mathf // ["three", "two", "one"]Mathf // [44,44,44,3,2,1,1,1]
randomArr
This will pick a random array element and return it.
Mathf // [1]Mathf // [5]Mathf // ["one"]
insert (arr, pos, item)
This will insert a new item into an array with its position as (parameter).
Mathf // [1,2,3,"will be placed after given pos",4,5]Mathf // [1,2,3,4,5,10]Mathf // ["zero", "one", "two"]
init (arr)
This returns the inital item in an array.
Mathf // [1]Mathf // [5]Mathf // ["one"]
app (arr, item)
This will append a given item to the end of an array.
Mathf // [1,2,3,4,5,6]Mathf // ["one","two","three"]Mathf // [1,"a","b",2,"h e l l o"]
grabVal (obj, key)
This will return the value of the given key.
Mathf // 1Mathf // 2Mathf // 3
grabKey (obj, val)
This will return the key of a given value.
Mathf // OneMathf // TwoMathf // Three