Floating point number in JS - javascript

I'm writing a small webpage that will enable students to answer questions and get feedback on their answers.
Part of this detection checks for common errors to give them guidance. Specifically I want to check if their answer is a power of ten out from the actual answer.
If the answer was 3.93E-6, this condition should activate if they type 3.93E2, 3.93E-9, 3.93 etc.
The obvious way to me to test this is to do something like this:
var correct = 3.93E-6;
var entry = 3.93E-2; //really comes from an input box.
if (!(entry / correct)%10) {
alert ("power of ten error");
}
However, this doesn't work as error/correct doesn't work for large/small numbers.
How can I fix this?
Live code at: http://bradshawenterprises.com/test.html

var num = 12.4123;
var numString = num.toExponential()
// numString = "1.24123e+1"
This normalizes the number, but you have to parse it manually. (Like on how accurate the result has to be…)

Here's one way to see if two numbers are off by approximately a power of ten:
var correct = 3.93E-6;
var entry = 3.93E-2;
var epsilon = .01;
var log10_ratio = Math.log(correct/entry)/Math.log(10);
if (Math.abs(Math.round(log10_ratio) - log10_ratio) < epsilon) {
alert ("power of ten error");
}

If they're required to enter the answer with the "E" notation, why not just check if all the stuff before the "E" is the same in both the student's answer, and the correct answer.
Of course you might also want to give them an idea of how many decimal places they should keep, otherwise 1.2E5 and 1.21E7 wouldn't trigger the "power of ten error"

Related

How to Unify Kotlin and Long.js Division Results

I have an issue,
I have a number that I wish to divide by 49 as shown in the code below:
// in Javascript:
let referenceNumber= 3487039819743582477;
let division = referenceNumber /49;
//I am using https://github.com/dcodeIO/long.js
//Now when I do the following with the Long.js library:
let longValue = Long.fromValue(division); //Long.js gave me 71164077953950662 as the result
//Now, in Kotlin:
val longValue = division.toDouble().toLong(); //Kotlin gave me 71164077953950664 as result
// As you can see, there is a difference of 2 between the two programming languages.
Which is the correct value here and How do I rectify this? I want both languages to give me exact value all the time after division and conversion to long
Thank you.
The problem is that you are doing toDouble(), which adds imprecision.
println(3487039819743582477 / 49) prints 71164077953950662.
If you want to know about Double and imprecision, check out https://floating-point-gui.de/.

Is there a way to get more precise result when dividing Big Number?

I need to operate on Big Numbers but I need the results to be precise, like below:
const BN = require('bn.js');
var a = 11060622312717714974
var b = 1570481433500000000000;
console.log(a/b); //0.0070428227146040415
When I use Big Number I get only 0, no precision:
var aBN = new BN('11060622312717714974');
var bBN = new BN('1570481433500000000000');
console.log(aBN.div(bBN).toString()); //0
Is it possible to get precise result with this library?
Not familiar with the library, but having a look at the README.md file, it says:
Note: decimals are not supported in this library.
So, according to that, the answer would be no.
See https://github.com/indutny/bn.js/#usage.
Precise is a funny word in computer science and JavaScript is not exception.
Notice the input compared to the output of this:
console.log( 11060622312717714974 )
you don't always get what you want.
And there are situations like this:
console.log( .1 + .2 );
console.log( (.1 + .2) === .3 );
So, probably not what you wanted to hear, but knowing this up front you can maybe work around it.
Anyway, checkout https://github.com/MikeMcl/bignumber.js/

Javascript coding

I am going nuts trying to write a JavaScript code, which requests from the user any sequence of numbers until such user enters 'N'.
If this happens, a message should pop up indicating the user that he reached the end of that sequence and then a new message pops up showing 'all' the numbers the user entered PLUS organising them in crescent order.
Any suggestions, please?
I would appreciate any help as I'm going mad right now thinking of anything.
If the array was specified, then I wouldn't have a problem, but since it's a random array, I can not seem to find the right answer.
What you want to do is something like this:
Get the user's numbers and save them, prefably in an array.
Loop through all the numbers and check if the number is N.
If so, alert the user.
Sort the array, alert the user.
Attempt to code:
// All the user's numbers
var UserNumbers = [15, 10, 11, 12, 13];
//The value to search for
var N = 12;
//Loop through the values
for(var i = 0; i < UserNumbers.Length; i++){
if(UserNumbers[i] == N){
//The number matches
alert("You reached the end of the sequence";
}
}
// I am not sure what you mean with cresent order so that you'll have to solve for yourself.
//Sort the UserNumbers here
//Store the message to show the user
var message = "";
//Loop through the values - now in correct order and save them to the message
for(var i = 0; i < UserNumbers.Length; i++){
//Add to the message
message += UserNumbers[i] + " ";
}
//Show the message - all the containing numbers
alert(message);
Although this is an helpful community I am pretty certain that we're not made out of teachers here to teach you how to code. Since this sounds to be an homework rather than a real life scenario, talk with your teacher or classmates. Good luck and next time, try to do some research before posting a question - there are plenty of tutorials for beginners out there.

Custom function in Google Spreadsheet: disagreement between one person and rest of the group

I need to calculate the disagreement between respondents of a survey.
The first step was to calculate the disagreement between two respondents
function disagree(DISAG, otherMember) {
return Math.abs(DISAG.localeCompare(otherMember));
}
The second step is to calculate the average disagreement between one respondent and all the others.
To to so manually, I have to do:
1/(N-1) * (disagree(DISAG, otherMember) + disagree(DISAG, othermember2)) etc
To try to do it in one go, I wrote the following function, as a test
function TEST(DISAG) {
var otherMembers = ["No problem", "Can improve", "Can improve"]
var indivDisag = []
var sum = 0
for (var i in otherMembers) {
indivDisag[i] = DISAG.localeCompare(otherMembers[i])
};
for (var i in indivDisag) {
sum += Math.abs(indivDisag[i]);
};
return sum / indivDisag.length
}
EDIT: My original issue was due to a typo. I now get a number returned in the spreadsheet, but whatever the value of DISAG, the number is always the same. It must be that I didn't interpret the math correctly when coding the function.
The math look like this:
d(i,j) = disagreement of an individual (i) with another (j).
if i == j, then d(i,j) = 0. if i != j then d(i,j) = 1
di = average agreement of one person with the rest of the group.
di = 1/(N-1) * SUM( d(i,j)*f(j) )
where N = number of people who answered the survey
and f(j) is a function of the different individual disagreements.
In plain English : the average agreement of one person is the result of the sum of her individual agreements divided by the number of pairs possible in the group (N-1).
Thanks for your help
Couple of points
I don't link google script knows what localCompare() is.
So if you fix this it will think you have attached a function to an object.
The debugger won't run on your function because it expects to be passed a variable.
function TEST(DISAG) {
When you run the debugger, it won't know what "DISAG" refers to. So when it encounters it in
indivDisag = DISAG.localCompare(otherMembers[i])
It won't know what to do.
You could include it as a variable within the function TEST(), for debugging purposes.

How can I create (in Javascript), a tool to identify the longest repeated pattern in a series of numbers?

Alright, long story short, what I overall am attempting to do, is test the level of randomness in a series of multiple thousands of " previously generated seemingly "random" numbers.
I've already written something that will test for the probability of numbers with great success, however, the next step is identifying repeating or recurring patterns.
I'd prefer to get this part done in javascript, so as to avoid having to teach myself another language for the time being.
Now, obviously, I could just use regex and punch in some random sequences myself, but that is not ideal, and would take an infinite amount of time to get the results I'm after.
Ah, I missed a number of your comments above. I believe this is what you're looking for:
function findLongestMatch(StringOfNumbers) {
var matches = StringOfNumbers.match(/(.{2,})(?=.*?\1)/g);
if (!matches) { return null; }
var longestMatch = matches[0];
var longestMatchLength = longestMatch.length;
for (matchIndex = 1; matchIndex < matches.length; matchIndex++) {
if (matches[matchIndex].length > longestMatchLength) {
longestMatch = matches[matchIndex];
longestMatchLength = longestMatch.length;
}
}
return longestMatch;
}
It'll be slow, but it'll get the job done.

Categories

Resources