I am trying to solve Pi till n number of digits in JavaScript with this formula:
#!/usr/bin/env js60
function calculatePi(n) {
var q = t = k = 1
var m = x = 3
var n = n + 1
var r = 0
str = ''
while (str.length < n) {
if (4 * q + r - t < m * t) {
str += m
var rr = r
r = 10 * (r - m * t)
m = Math.floor((10 * (3 * q + rr)) / t - 10 * m)
q = 10 * q
}
else {
m = Math.floor(((q * (7 * k + 2)) + (r * x)) / (t * x))
r = ((2 * q) + r) * x
t = t * x
q = q * k
k = k + 1
x = x + 2
}
}
return str.slice(0, 1) + '.' + str.slice(1)
}
print(calculatePi(19))
Here's how it works in a language with arbitrary length integer support.
But in JavaScript the code generate correct values till the 18 decimal places, and after that the number gets really big to work with. Worse, if the function is given a large number like 10000, it will run in a infinite loop.
When I am trying to write a big number with an n appended to it (as suggested here):
var a = 1000000000000000000000000000n
I get:
typein:1:8 SyntaxError: identifier starts immediately after numeric literal:
typein:1:8 var a = 1000000000000000000000000000n
typein:1:8 ........^
How can I represent an arbitrary length integer in JavaScript?
Thanks to the comments, changing the JS engine from SpiderMonkey to Node solved the issue. The final code looked like:
#!/usr/bin/env node
function calculatePi(n) {
var one = 1n, two = 2n, three = 3n, seven = 7n, ten = 10n
var q = t = k = one
var m = x = three
var n = BigInt(n) + one
var r = 0n
str = ''
while (str.length < n) {
if (4n * q + r - t < m * t) {
str += m
var rr = r
r = ten * (r - m * t)
m = (ten * (three * q + rr)) / t - ten * m
q *= ten
}
else {
t *= x
m = (q * (seven * k + two) + (r * x)) / t
r = ((two * q) + r) * x
q *= k
k += one
x += two
}
}
return str.slice(0, 1) + '.' + str.slice(1)
}
console.log(calculatePi(5000))
Now it can solve any digits using some system's memory (around 40 MiB for 5,000 digits).
The code removed Math.floor() function because BigInt calculation are Integers. A floating point number with arbitrary precision is not going to be calculated here.
//Data
var M = 83 * (10 ^ (-5));
var R = 2077;
var Th = 1087;
var Tk = 372;
var Tr = 667;
var a = 90;
var Vclc = 9.135 * (10 ^ (-6));
var Vcle = 7.733 * (10 ^ (-6));
var Vswe = 6.105 * (10 ^ (-5));
var Vswc = 6.294 * (10 ^ (-5));
var Vk = 5.606 * (10 ^ (-6));
var Vr = 19.308 * (10 ^ (-6));
var Vh = 5.606 * (10 ^ (-6));
var ρ = ((2 * Vclc + 2 * Vk + Vswc) / (2 * Tk)) + ((Vr * Math.log(Th / Tk)) / (Th - Tk)) + ((2 * Vcle + 2 * Vh + Vswe) / (2 * Th));
console.log(ρ);
console.log(ρ < Number.MIN_VALUE);
When I run this code in CodePen, I get in the console: -1.4626799636897467 and true, which I think means there is an underflow. However, the value of ρ should be something like k*10^-10, where k is constant around 2000. Since the smallest positive number that can be represented in Javascript is 5e-324, which is way smaller than my value, why do I get an underflow? If it's not an underflow I don't understand how a sum of positive numbers can result in a negative number...
Note: I used information provided here for the check of underflow:How to detect numeric overflow/underflow in JavaScript arithmetic operations? and JavaScript MIN_VALUE Property
^ is bitwise XOR, not exponentiation.
You can use exponential notation to enter literal powers of 10. xxxEyy is xxx * 10^yy
//Data
var M = 83e-5;
var R = 2077;
var Th = 1087;
var Tk = 372;
var Tr = 667;
var a = 90;
var Vclc = 9.135e-6;
var Vcle = 7.733e-6;
var Vswe = 6.105e-5;
var Vswc = 6.294e-5;
var Vk = 5.606e-6;
var Vr = 19.308e-6;
var Vh = 5.606e-6;
var ρ = ((2 * Vclc + 2 * Vk + Vswc) / (2 * Tk)) + ((Vr * Math.log(Th / Tk)) / (Th - Tk)) + ((2 * Vcle + 2 * Vh + Vswe) / (2 * Th));
console.log(ρ);
console.log(ρ < Number.MIN_VALUE);
The reason why you get true in your code is because Number.MIN_VALUE is the smallest positive number, but your result is negative. If you want to test for underflow, you need to check whether the absolute value of p is lower than Number.MIN_VALUE.
But since numbers smaller than this can't be represented in Javascript floating point, the test will never actually be true for anything other than 0.
I created following generator for Czech tax number (IČO). I know that there is certainly better way how to code that in javascript. I am beginner and I would like to see how to write my code properly. The number is created with special formula and it has 8didgits, tha last digit is based on modulo11 as you can see below in code.
Thanks for your replies.
//Generation of single random numbers as variables
var a = Math.floor(Math.random() * 10);
var b = Math.floor(Math.random() * 10);
var c = Math.floor(Math.random() * 10);
var d = Math.floor(Math.random() * 10);
var e = Math.floor(Math.random() * 10);
var f = Math.floor(Math.random() * 10);
var g = Math.floor(Math.random() * 10);
//Formula for tax number
var formula = a * 8 + b * 7 + c * 6 + d * 5 + e * 4 + f * 3 + g * 2;
var modulo11 = formula % 11;
if (modulo11 === 0) {
var h = 1;
} else if (modulo11 === 1) {
var h = 0;
} else {
var h = 11 - modulo11;
};
//Completing tax number
var identificationNumber = "" + a + b + c + d + e + f + g + h;
//displaying number in console
console.log(identificationNumber);
Take benefit from Array data structure to store a, b,...g.
Then "map" this array by (8- indexOfItem) * item
👉🏼 so , for the 1ˢᵗ item which has index = 0 , we will have (8 - 0) * a -➡ 8* a.
👉🏼 for the 2ⁿᵈ item ➡ (8 -1) * b ➡ 7 *b
👉🏼....so on.
Then use "reduce" to calculate the sum .
Then use "join" instead of ""+ a +b + ....+ g+ h
function getH(modulo11) {
if (modulo11 === 0) return 1;
if (modulo11 === 1) return 0;
return 11 - modulo11;
}
//Generation of single random numbers as variables
const numbers= Array.from({length: 7},(v, k) =>Math.floor(Math.random() * 10))
//Formula for tax number
const formula= numbers.map((n, i) => (8 - i) * n).reduce((total, next) => total+ next , 0)// alternative of sum : a * 8 + b * 7 + c * 6 + d * 5 + e * 4 + f * 3 + g * 2
const h= getH(formula % 11);
//Completing tax number
const identificationNumber = [...numbers, h].join('');
//displaying number in console
console.log(identificationNumber);
I'm generating a random number with the code below:
Math.floor((Math.random() * 9999) * 7);
Some of the results I'm getting:
45130,
2611,
34509,
36658
How would I get results like this(with 2 letters included):
TT45130,
PO2611,
KL34509,
GH36658
Side question:
What is the range of numbers that Math.random() carries? Can I set a specific range of values? Not necessary to answer but just curious.
You can use a function like below to get a random uppercase character:
function getRandomUppercaseChar() {
var r = Math.floor(Math.random() * 26);
return String.fromCharCode(65 + r);
}
So to generate a code as you specified with a two-letter prefix:
function generateCode() {
var prefix = new Array(2).fill().map(() => getRandomUppercaseChar()).join(""),
integer = Math.floor((Math.random() * 9999) * 7);
return prefix + integer;
}
NOTE: The above generateCode function uses modern ES6 and ES5 javascript, which is perfectly fine in a modern environment (such as Node.js or a current browser). However, if you wanted greater compatibility (for example, to ensure that it works in old browsers), you could rewrite it like so:
function generateCode() {
var integer = Math.floor((Math.random() * 9999) * 7);
for (var i = 0, prefix = ""; i < 2; ++i)
prefix += getRandomUppercaseChar();
return prefix + integer;
}
Try the simpler answer
var randomNumber = function () {
return Math.floor((Math.random() * 9999) * 7);
}
var randomChar = function () {
return String.fromCharCode(64 + Math.floor((Math.random() * 26)+1));
}
console.log(randomChar()+randomChar()+randomNumber());
//Sample outputs
HB10527 DR25496 IJ12394
Or you can use Number#toString for this purpose with radix = 36.
function getRChar() {
return (Math.random() * 26 + 10 | 0).toString(36).toUpperCase();
}
var s = getRChar() + getRChar() + Math.floor((Math.random() * 9999) * 7);
document.write(s);
If you need to generate a random string with JS, the most common way is to define an alphabet and pick random indices from that:
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var numbers = "0123456789";
var randomString = "";
// Pick two random chars
for (var i = 0; i < 2; i++) {
var rand = Math.floor(Math.random()*alphabet.length);
randomString = randomString + alphabet.charAt(rand);
}
// Pick four random digits
for (var i = 0; i < 4; i++) {
var rand = Math.floor(Math.random()*numbers.length);
randomString = randomString + numbers.charAt(rand);
}
// randomString now contains the string you want
Sample strings:
OJ8225
YL5053
BD7911
ES0159
You could use String.fromCharCode() with a random integer between 65 and 90 to get an uppercase letter, i.e.
String.fromCharCode(Math.random() * 26 + 65) + String.fromCharCode(Math.random() * 26 + 65) + Math.floor((Math.random() * 9999) * 7);
gives med the results: "SH21248", "BY42401", "TD35918".
If you want to guarantee that the string always has the same length, you could also use
String.fromCharCode(Math.random() * 26 + 65) + String.fromCharCode(Math.random() * 26 + 65) + Math.floor(Math.random() * 59993 + 10000);
Math.random() always returns a number between 0 and 1, but never 0 or 1 exactly.
An array of the alphabet, a random number is generated to get a random letter, repeated to get a second random letter and then joined to the random number generated as in your code:
var alphabet=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
var ranletter1 = alphabet[Math.floor(Math.random() * alphabet.length)];
var ranletter2 = alphabet[Math.floor(Math.random() * alphabet.length)];
var ranNum = Math.floor((Math.random() * 9999) * 7);
var ranCode = ranletter1 + ranletter2+ ranNum;
function sum() {
a = Number(document.getElementById("rate_ts").value);
b = Number(document.getElementById("discount").value);
c = a - (Number(a) * Number(b) / 100);
d = Number(document.getElementById("ocharge").value) + c + Number(document.getElementById("pay_amt").value);
tax = (Number(a) * Number(12.36) / 100);
e = tax + d;
document.getElementById("net_amt").value = e;
}
this code is not working right......i want to add tax and for that i have given 12.36 but it is not returning anything..plz help
parseFloat()
Summary
The parseFloat() function parses a string argument and returns a
floating point number.
$('#three').click(function (e) {
a = parseFloat(document.getElementById("rate_ts").value);
b = parseFloat(document.getElementById("discount").value);
console.log(a+' | '+b);
});
WORKING DEMO
Try This,
function sum() {
a = parseFloat(document.getElementById("rate_ts").value);
b = parseFloat(document.getElementById("discount").value);
c = a - (a * b / 100);
d = parseFloat(document.getElementById("ocharge").value) + c + parseFloat(document.getElementById("pay_amt").value);
tax = a * 12.36 / 100;
e = tax + d;
document.getElementById("net_amt").value = e.toFixed(2);
}
If you want only two decimal point value then use number.toFixed(2)
Working Example