I'm trying to get this calculation done in javascript using Big.js
r = (a * b)/ sqrt( ( a*sin(θ) )^2 + ( b*cos(θ) )^2 )
I've also tried math.js and have the same result.
You can see the discrepancies in Wolfram and Google calculations, as google is using javascript.
Wolfram [=40] vs Google [=43.4008369271]
I've done this jsfiddle where you can see it working:
http://jsfiddle.net/herkulano/k1h5d4zk/
How do you solve it?
The difference stems from the fact that javascript by default uses radians and in your example, Wolfram is defaulting to degrees. You can convert degrees to radians with a function like this.
function degToRad(deg){
return deg*(Math.PI/180)
}
Related
I am experiencing the following bug in my JS.
(1.001 * Math.pow(10, 3))
Instead of returning 1001 this returns 1000.99999999. I am trying to eradicate this bug and have been looking at using this big.js library.
I am unsure what to do to fix this issue.
I have tried the following but it doesn't seem to work.
var x = new Big(10);
(1.001 * x.pow(3));
This produces the same bug as without the library.
You have to use Big.js that way, I think you have to forget using conventional operators :
console.log(Big(1.001).times(Big(10).pow(3)))
<script src="https://cdnjs.cloudflare.com/ajax/libs/big.js/3.2.0/big.min.js"></script>
I have an assignment to make a program that can calculate the area beneath a graph in a certain area.
My function is f(x)=3+(tan(x))^2
I have turned this into javascript so it looks like this:
var y = 3 + Math.pow(Math.tan((x)*(180/Math.PI)), 2)
This is giving the right result at certain x-values. But sometimes, eg. at x = 3.4, it gives a strange number like 16 where I expected something around 3 or 4.
I have read that it can be caused by the problem with floating point behavior, but it should not give that big a difference? I don't understand it. :(
I use a for loop to change the x value and and array to store the values.
x*180/π converts an angle x, given in radians, into degrees. But that's almost certainly wrong here: The JavaScript trigonometric functions expect their arguments in radians. So if your angle is in radians, you don't need any conversion at all, and if it is in degrees, you should convert the other way round, namely x*π/180.
This problem is being asked with a node.js server in mind, but I stated the question as "javascript" because I will likely use this same logic for a client-side script, as well.
Here's the problem: given a set of x values, y needs to scale in a logarithmic way. The Math object performs a natural log [ln(x)], but does not provide an interface for specifying the base of the logarithm.
For a specific example, I need to find the following:
log[512](2)
Which should return .1111~
However, I do not see an interface that allows me to accomplish this, nor can I seem to find a library that exposes an option for the log's base. Surely this is a common problem and has a solution, but my searching has only found solutions for different/unrelated problems. Ideas?
You can use the logarithm base change formula:
log[a](n) = log[b](n) / log[b](a)
So in order to get log(2) base 512, use:
function log(b, n) {
return Math.log(n) / Math.log(b);
}
alert(log(2, 512));
Note that Math.log above uses the natural log base; i.e., it would be written as ln mathematically.
I found this answer as a first result in google today, and if anyone else finds it too, there's a small mistake. The correct version is as follows:
function log(b, n) {
return Math.log(n) / Math.log(b);
}
i created a program in javascript that computes vector coordinates, everything was smooth since i have the correct formula, but when i try to conpute for the cosine of 143.1301 using Math.cos in javascript it returns 0.1864 instead of 0.7999 from the scientific calculator why is that? can anyone explain to me why? and also please give me the solution for this problem... thanks in advance... :) here;s a sample of my code
function cyltoxec(a)
{
ans = Math.cos(a);
return ans.toFixed(4);
}
var = x;
return cyltoxec(x);
Trigonometric functions in JavaScript (and indeed, in most mathematical parlance and programming) use radians as the angular unit, not degrees.
There are 2 * Pi radians in 360 Degrees. Thus, the cosine of a degrees is
Math.cos(a * Math.PI/180)
Math.cos expects its argument to be in radians, not degrees. Try Math.cos(a * Math.PI/180) instead.
Using the javascript function
function squareIt(number) {
return number * number;
}
When given the number 4294967296 the function returns 18446744073709552000 is returned
Everyone knows the real answer is 18446744073709551616 :-)
I guess this is to to with rounding on my 32-bit machine. However, would this script give the right answer on a 64 bit machine? Has anyone tried this?
ChrisV- see this post. Also it easier for people to evaluate your question by typing the following JavaScript directly into the browser URL textbox:
javascript:4294967296*4294967296
what about this
function squareIt(number){
return Math.pow(number,2)
}
Javascript uses 64 bit floating point arithmetic internally for numerical calculations - the results you see are a reflection of this, and will happene regardless of the underlying architecture.
Here is one more example based on BigInteger.js.
alert(bigInt(4294967296).square());
<script src="https://cdnjs.cloudflare.com/ajax/libs/big-integer/1.6.40/BigInteger.min.js"></script>