Recently I run into the well known floating point precision errors of Javascript. Usually I would avoid floating point calculations on the thin client & rather leave it to the back-end.

I started using the big.js library created by Michael Mclaughlin. Though it has a square-root method/function, it does not have a nth-root methods/function nor does the power function support fraction values as arguments.

So I was wondering if anyone using the library has extended it to have such a function or at least use it to calculate accurate nth-root results.

Michael Mclaughlin suggested that I implement such a function similar in structure to the square-root function. However my attempts at understanding the logic proofed my maths-disability, resulting in simple calculations yielding very wrong results.

Using the algorithm on Rosetta Code also yields incorrect results.

So I was wondering if anyone using the library has extended it to have such a function or at least use it to calculate accurate nth-root results.

Here is the code to my last attempt:

```
P['nthrt'] = P['nthroot'] = function (n, prec)
{
var negate, r,
x = this,
xc = x['c'],
i = x['s'],
e = x['e'];
// Argument defaults
n = n || 2;
prec = prec || 12;
// Zero?
if ( !xc[0] ) {
return new Big(x)
}
// Negative?
negate = ( n % 2 == 1 && i < 0 );
// Estimate.
r = new Big(1); // Initial guess.
for (var i = 0; i < prec; i++) {
r = (ONE.div(n)).times(r.times(n-1).plus(x.div(r.pow(n-1))));
}
if (negate) r['s'] = -1;
return r;
};
```

It does not even get obvious results correct like the 4th root of 81 = 3, instead it gets 3.00000000xxx

Newton's method only gives an approximation for the root, so 3.0000xxx should be expected. If you know that the answer should be an integer, you can round `r`

down (Newton's method overestimates the root) and check that `r^n=x`

.

Similar Questions

I've been looking for formatting decimal numbers easily as in JavaScript by using toFixed(). What's your suggestion?

Problem: Double minus another double is returning unaccurate results. Usual solution: Use big decimal to perform calculations. I can't use this because it's in the middle of a game loop, can only set

Including many javascript files on the same page can lead to low performance. What I want to ask is: Is it best to keep separate files or include all files in one javascript file? And if it is better

Possible Duplicate: jQuery: rounding numbers to 2 digits after comma While printing out a number in JavaScript, I only want keep only three digits after the decimal point. The code I am using shows

As shown here: http://jsfiddle.net/3sNbV/ $('.box:nth-child(5n)').addClass('edge'); .. the nth element is incorrect if you have any other html elements in there besides the one its looking for, altho

I need to sort an array of this format, using plain old Javascript. var arr = [0.01 BASIC, 0.01 SEF, 0.015 BASIC]; What I need is for the array to be sorted first by decimal then by string and

I need to make big DIV structures and append them to a wrapper. But what I've seen so far was always about appending one DIV element into another one. There is an empty DIV-Wrapper, to be filled with

I want to approximate a number showing only two digits after the decimal point. I wrote this: String result = String.valueOf(new BigDecimal(price).setScale(4, BigDecimal.ROUND_UP).doubleValue()); If

I'm having trouble understanding a Big O problem. The Problem public boolean isHB(TreeNode root){ if (root==null) return tree; int heightL = height(root.left); int heightR = height(root.right); boolea

Why we use root node. In below given function they use root node function. I want to understand this function also what is meaning of '!rootNode'. function getElementsByClassName(cn, rootNode) { if (

Is it possible to select, say, every fourth element in a set of elements? Ex: I have 16 <div> elements... I could write something like. div:nth-child(4), div:nth-child(8), div:nth-child(12), div

I have an object representing a queue and I want to keep last nth elements from it (fixed size). var m = {}; m[t1] = { a: t1, b: t1 }; m[t2] = { a: t2, b: t2 }; ...... ...... m[tn-1] = {

using apache web server and javascript/php webpage, is there ANY WAY to include javascript exist outside the root as a script in the index (html file) ?

I have to find nth root of numbers that can be as large as 10^18, with n as large as 10^4. I know using pow() we can find the nth roots using, x = (long int)(1e-7 + pow(number, 1.0 / n)) But this is

I have 2 big variables, and I need to compare like: var a = 15000000000000000000000001 // integer var b = 15000000000000000000000000 // string In all my test comparisons get wrong results. eg: Con

May I know know I can trigger system to repaint nth row in JList? Currently, what I did is jList0.repaint(); // Repaint entire JList, which is not efficient. I am only interested to update nth row in

I want to JavaScript-encode [sic] a big block of HTML so that I can use it as a template in various places. Something like this would be ideal: var template = {% jsencode %} <input type=text valu

Possible Duplicate: Is there a bignum library for JavaScript? How to deal with big numbers in javascript I need a JavaScript library to manipulate big integers. It should support all the basic arith

We can modify the document root directory path for PHP using $_SERVER['DOCUMENT_ROOT'] = to/some/new/directory; //Now the / which represent the ^(above) path in .htaccess we have RewriteBase /t

I'd like to round up to 2 decimal places, but only if necessary. Input: 10 1.7777777 9.1 Output: 10 1.78 9.1 How can I do this in JavaScript?

I have a number with decimal places and I am wondering if it's possible to round the decimal to the nearest whole using javascript? My number is: 4.59 I need my number to round to: 4.60

If someone could explain to me the difference between Decimal and decimal in C# that would be great. In a more general fashion, what is the difference between the lower-case structs like decimal, int,

I'm using JavaScript/jQuery to detect if browsers support the nth-of-type CSS3 pseudo-class. A 5px padding-left is assigned in the CSS (see code below) and the detection succeeds if the JS/jQ can read

Recently, I came across a problem in someone's programming class. It asked them to compute a square root using only integers; they were to use one integer to represent the part before the decimal poin

I have this JavaScript function (see below). It only return whole number, it only works when I inputted a whole number. But when I inputted decimal value it doesn't work. What should I do to handle de

I could develop the following code in Javascript that allows only digits to a given HTML TextField. function isNumberKey(evt) { var charCode = (evt.which) ? evt.which : event.keyCode; if (charCode >

I wanted to create a Javascript regex, which accepts 123, 123.123, 12.34, 1.324 But should not accept 1246, 1234.45, 1.2364

I realise that this may seem like a stupid request but I'm going to ask anyway. I wish to use a regular expression to find every nth comma in a list of numbers ie: 88574,93243,129659,135504,136357,14

I have the following javascript code to add checkboxes together when selected and produce a total. What do I need to add to the code to get the total to display 2 decimal places always. <script typ

I have a strange case of trying to combine two pseudoclasses: :not and :nth-of-type() to get rainbow striping on non-hidden items Having a :not([selector]):nth-of-type([rule]), I assumed css wouuld fi

Without resorting to using jQuery, how could one find a replacement function in Javascript or CSS to simulate the css: table#report tr:nth-child(odd) { background-color:#eeeeee; } table#report tr:nth-

I am a bit confused about the nth-of-type pseudo class, and how this is supposed to work - especially as compared to the nth-child class. Maybe I have the wrong idea, but given this structure <div

I have been looking around for this on Google but I have not yet found a good result. I would like to simply select the nth child element of a Silverlight Grid, using Javascript, like this: for (var i

I am calculating the n'th root of a positive integer using standard library method pow() .Here is the snippet from my program : double x,y; x=pow(64,(1.0/3)); int z; printf(x=%lf\n,x); z=(int)x; pri

I have always been coding in java and have recently started coding in javascript (node.js to be precise). One thing that's driving me crazy is add operation on decimal numbers; Consider the following

I'm not even sure I asked the question in a way that is accurately describing my issue. In JavaScript, I would like to round a given number(x) up based on another decimal(y). For example: x = 8.6333 y

I have got a Html and Javascript code, that contains about 1000 lines and I need to put it to php variable. Sure I was thinking about the EOT method, But there is one problem with it, if there is word

This question already has an answer here: JavaScript: formatting number with exactly two decimals 12 answers I'm trying to get the following to output to 2 decimal places: $(#incP1).click(fun

Just wondering if its worth it to make a monolithic loop function or just add loops were they're needed. The big loop option would just be a loop of callbacks that are added dynamically with an add f

I have a page which brings database entries in and displays them, and i have given every other entry/listing this style: hjl:nth-child(odd) { background: #F2F2F2;} And this is my HTML/PHP <a href=

I called a class called test for my textbox. When I entered the first value for e.g. the first value as 4., then suddenly the output coming as 4.00. I just want to restrict entry only for two decimal

I need to output decimal numbers in a price format, i.e. 10 = 10.00 11.1 = 11.10 How can I achieve this using decimal.Decimal class ? pad_zero(Decimal('10.0')) >>>Decimal('10.00') *EDIT:*for

I found a function in the GeoServer source that will allow me to convert Degrees Minutes Seconds to Decimal Degrees in Javascript. But i need to conver Degrees Decimal Minutes to Decimal Degrees. Curr

It feels like I am missing something obvious here. This has been asked a number of times - and the answer usually boils down to: var num = 4.5; num % 1 === 0; // false - 4.5 is a decimal But, this fa

This question already has an answer here: How do you round to 1 decimal place in Javascript? 8 answers How to change the decimal value to one digit in javascript.For eg. I have value 5 and I ne

I have a text field where the user will be entering a number and I want the number to be an integer. To do this, I want to ignore when a user types a decimal point. Currently, I have a javascript fun

Im trying to write a function that takes my decimal degrees (lat or long) and converts them to DMS degrees minutes seconds. I know I am meant to times the decimal point number by 60 then it's decimal

I have small issue with the JavaScript function toFixed(2). If I round this decimal number 45.24859, I get 45.25 using this function. But my problem is, if I round 10 (it has no decimal part), the fun

I have a string value ' 9223372036854775807 '. I use Number() function in JavaScript to convert it to a number value using the following Code var numericVal = Number(' 9223372036854775807 '); when i

I have two big decimal fields x and y. If x is not null I would like to display x otherwise I would like to display y in a jsp. The struts if statement in my jsp say: if (x != null){ display x} else {