issue with floating point precision js - javascript

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>

Related

Calculation result different in javacript and lua

I'm currently creating my own bot made with NodeJS and having a issue where my formula won't calculate the same way it does in Lua
Here is a example:
XP = 79878
math.floor((1/4+XP/125)^0.5-1/2)
Lua: returns 24
JavaScript: returns 639
If anyone knows how to make this formula work with JavaScript please provide an example below.
Thanks.
The ^ operator in Javascript does a XOR operation, rather than raising something to a power. In recent versions of Javascript (Node.js 8 seems to support it, I'm not sure about earlier versions) you can use the ** operator instead; if you need to support earlier versions, you should use Math.pow().
// newer code
Math.floor((1/4 + XP/125) ** 0.5 - 1/2)
// older code
Math.floor(Math.pow(1/4 + XP/125, 0.5) - 1/2)
You should use like this:
Math.floor(Math.pow(1/4+(XP/125), 0.5)-1/2);

Rounding in JS: inconsistent and unexpected behaivior

I'm dealing with relatively small and simple numbers. I first tried to do the rounding (2 signs after digit) with infamous toFixed. It's a known issue: sometimes it works incorrectly. But what struck me is the fact that it also works inconsistently:
(0.395).toFixed(2); // "0.40"
(0.295).toFixed(2); // "0.29"
These numbers are very similar, 3 signs after digit and yet different behavior.
So, I decided to switch to using Math.round. Shortly, I encountered another problem:
Math.round(0.35055 * 10000) / 100; // produces 35.05 instead of 35.06
Is Math.round also problematic? Then, what method should be used?
Unfortunately JavaScript is known to have such precision issues, issues that are better explained in the following question: Is floating point math broken?, as pointed in the comments.
If you require a greater degree of numerical accuracy, I would suggest you to use a library such as BigNumber, which also comes with its own toFixed method.
Your example would look something like this:
var a = new BigNumber('0.35055');
a = a.times(10000)
a = a.dividedBy(100)
console.log(a.toFixed(2)); //would log "35.06"
For brevity you can also chain the operations, like this: a.times(10000).dividedBy(100).toFixed(2)
I think this is working as designed. Keep in mind these numbers are stored in base 2, so there is a loss of precision when converting to and from base 10. And you have to look at these conversions if you want to understand why it looks inconsistent. If you have a fixed number of decimals that you want to keep precisely, you can use integers for operations and convert only for display.

Javascript precision with math libraries

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)
}

How can you perform varying base logarithmic functions in Javascript?

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);
}

Javascript - How to squared a number?

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>

Categories