math.js
math in Javascript
// finding absolute value of a number
var number = -8;
let output = Math.abs(number);
console.log(output);
var num =6;
nums = Math.abs(num);
console.log(nums);
// finding ceil(khali upre di tanbo) of a number
var mynumber = 9.59;
let myceil = Math.ceil(mynumber);
console.log(myceil);
// finding floor (kacher nichor shonka tanbo) of a number
var yournumber = 9.59;
let yourfloor = Math.floor(yournumber);
console.log(yourfloor);
// finding round of a number
var myround = 9.59;
let round = Math.round(myround)
console.log(round);
//finding a random number
var outputs = Math.random()*8;
outputs = Math.floor(outputs)
console.log(outputs);
// making it on a for loop
for( let i=0; i<=20; i++){
var outputs = Math.random()*8;
outputs = Math.floor(outputs)
console.log(outputs);
}
// github - redowanrafy707
4.43
7
function sum(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
function divide(a, b) {
if (b === 0) {
console.log("can't divide by zero");
}
if (a === 0 && b === 0) {
console.log("undetermined");
}
//or this without any IFs
return a / b;
}
Thank you!
7
0
4.22
9
// functions and constants
math.round(math.e, 3) // 2.718
math.atan2(3, -3) / math.pi // 0.75
math.log(10000, 10) // 4
math.sqrt(-4) // 2i
math.derivative('x^2 + x', 'x') // 2*x+1
math.pow([[-1, 2], [3, 1]], 2)
// [[7, 0], [0, 7]]
// expressions
math.evaluate('1.2 * (2 + 4.5)') // 7.8
math.evaluate('12.7 cm to inch') // 5 inch
math.evaluate('sin(45 deg) ^ 2') // 0.5
math.evaluate('9 / 3 + 2i') // 3 + 2i
math.evaluate('det([-1, 2; 3, 1])') // -7
// chaining
math.chain(3)
.add(4)
.multiply(2)
.done() // 14
Thank you!
9
0
3.83
6
JS Numbers and Math:
Number Properties:
MAX_VALUE
The maximum numeric value representable in JavaScript
MIN_VALUE
Smallest positive numeric value representable in JavaScript
NaN
The “Not-a-Number” value
NEGATIVE_INFINITY
The negative Infinity value
POSITIVE_INFINITY
Positive Infinity value
Number Methods:
toExponential()
Returns a string with a rounded number written as exponential notation
toFixed()
Returns the string of a number with a specified number of decimals
toPrecision()
String of a number written with a specified length
toString()
Returns a number as a string
valueOf()
Returns a number as a number
Math Properties:
E Euler’s number
LN2 The natural logarithm of 2
LN10 Natural logarithm of 10
LOG2E Base 2 logarithm of E
LOG10E Base 10 logarithm of E
PI The number PI
SQRT1_2 Square root of 1/2
SQRT2 The square root of 2
Math Methods:
abs(x)
Returns the absolute (positive) value of x
acos(x)
The arccosine of x, in radians
asin(x)
Arcsine of x, in radians
atan(x)
The arctangent of x as a numeric value
atan2(y,x)
Arctangent of the quotient of its arguments
ceil(x)
Value of x rounded up to its nearest integer
cos(x)
The cosine of x (x is in radians)
exp(x)
Value of Ex
floor(x)
The value of x rounded down to its nearest integer
log(x)
The natural logarithm (base E) of x
max(x,y,z,...,n)
Returns the number with the highest value
min(x,y,z,...,n)
Same for the number with the lowest value
pow(x,y)
X to the power of y
random()
Returns a random number between 0 and 1
round(x)
The value of x rounded to its nearest integer
sin(x)
The sine of x (x is in radians)
sqrt(x)
Square root of x
tan(x)
The tangent of an angle
Thank you!
6
0
4
5
Math.ceil(4.9); // returns 5
Math.ceil(4.7); // returns 5
Math.ceil(4.4); // returns 5
Math.ceil(4.2); // returns 5
Math.ceil(-4.2); // returns -4
Thank you!
5
0
3.43
7
//Math in JS is similar and different to the one in school
//Like: "+", "-" signs are the same. But "X" and "÷" are replaced with "*" and "/" respectivly
//You can do math both with variables and normal numbers
________________________________________________________________________________
Normal Number Examples
4+4 = 8;
4-4 = 0;
4*4 = 16;
4/4 = 1;
________________________________________________________________________________
Variable Examples
num1 = 0;
num2 = 0;
ans = 0;
num1 = 4;
num2 = 64
num1 + num2 = 68
num1 - num2 = 60
num1 * num2 = 24
num1 / num2 = 256;
num1 + num2 = ans;
num1 - num2 = ans;
num1 * num2 = ans;
num1 / num2 = ans;
________________________________________________________________________________
Now, you can use the document.getElementByID("").innerHTML = ans; to set the answer
//I hope this post helped you!
Thank you!
7
0
Are there any code examples left?
New code examples in category Javascript
-
Javascript 2023-04-21 22:52:36
-
Javascript 2022-03-27 22:25:20 javascript download string as file
-
Javascript 2022-03-27 21:40:22 sort numbers in array javascript
-
Javascript 2022-03-27 21:20:04 compare two arrays and return the difference javascript
-
Javascript 2022-03-27 21:15:02 javascript regex french phone number
-
Javascript 2022-03-27 21:05:03 cypress custom error message
-
Javascript 2022-03-27 21:00:05 create element javascript with id
-
Javascript 2022-03-27 20:40:57 feather icons react
-
Javascript 2022-03-27 20:20:01 how to make graphql request in axios
-
Javascript 2022-03-27 20:15:07 bootstrap validator password and confirm password