1

I am making a webpage that displays the digits of pi. I was able to get my pi variable to display 20 digits of pi using the toFixed method but I am wondering if I can display more. Is the number of decimal places limited by the memory size of my variable? Is there a way to display more than 20 digits?

EDIT: I should have clarified that I was using the Chudnovsky Algorithmn to calculate the value of pi. I assuming that because of the memory limitations of the var variable that it can only display up to a certain amount of digits which is limited to 20? Source

0x90
  • 34,073
  • 33
  • 137
  • 218
  • You won't be able to simply display an arbitrary number of decimal points using `Math.PI`. You'll need to use some sort of mathematical formula (like this one https://www.math.hmc.edu/funfacts/ffiles/20010.5.shtml) if you want arbitrary decimal precision for `pi`. The spec says that `toFixed` does not accept arguments higher than 20. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) – Gershy Jul 19 '17 at 04:32
  • I should have clarified that I was using the Chudnovsky Algorithm in order to calculate the value of Pi. It seems that I would have to make my data type a bignum object in order to save more digits? my [source code](https://github.com/so0p/ChudnovskyAlgorithm/blob/master/script.js) is here if anybody cares to take a look. I appreciate any advice. – Charlson So Jul 19 '17 at 04:40
  • How many digits do you need, or do you want arbitrary precision? – Gershy Jul 19 '17 at 04:45
  • I would like to display up to a 100 digits. Am I misunderstanding how the calculation works? I assumed that the value of Pi could be accurately calculated using the algorithm and that all I had to do was display the variable. Is mathematical imprecision an unavoidable problem due to the constraints of memory? – Charlson So Jul 19 '17 at 04:49
  • The issue you're encountering is that computers have CPUs which can only perform native mathematical operations on values with limited bit-widths. Most programming languages give access to only these native operations, and don't automatically provide fallbacks to more advanced, non-native solutions when the input bit-width exceeds that of the native limitations. In this sense mathematical imprecision IS an unavoidable problem that is usually addressed with a library. – Gershy Jul 19 '17 at 04:53
  • By the way, if you only need 100 digits my solution is really really dirty and cheap, but it will do the job ;D – Gershy Jul 19 '17 at 04:53
  • Thank you for your help! I will take a look at your solution posted earlier. – Charlson So Jul 19 '17 at 05:01
  • Any luck with this? – Gershy Jul 24 '17 at 22:08
  • I'm still bamboozled. The easiest way seems to be saving the digits of pi as a string. Even when using the Chudnovsky Algorithm I can only get it to be accurate to the 16th digit. – Charlson So Jul 27 '17 at 03:48
  • You'll never store super-high-precision numeric values in a single numeric `var`. Any algorithm that requires such high precision must work with datatypes of arbitrary length (e.g. a `String`, which can be of any length). On each iteration of the algorithm, the `String` being used to store the running result must be interpreted as a number, have operations performed on it, and then again reassembled into a `String`. Does my answer solve your problem? – Gershy Jul 27 '17 at 18:43
  • The datatype used doesn't need to be a `String` though. An array of integers, where each next integer is considered to continue the chain of digits of the previous, would be more efficient. This is likely how some libraries implement high-precision numerical methods. – Gershy Jul 27 '17 at 18:45
  • Yes, Thank you. I was able to solve my issue. – Charlson So Aug 01 '17 at 02:46

3 Answers3

1

You can't.

Try storing the bits in more than one variable, use a string, or get a bignum library, or if you only need to deal with integers, a biginteger library.

sidewinder
  • 632
  • 4
  • 9
1

You can't do this with native JavaScript for more info see this.

The following is cast to float:

var x =  3.14159265358979323846;
print(x.toFixed(20));

However, you can either print a string or to use mathjs:

var one = math.bignumber(1);
var pi = math.bignumber('3.14159265358979323846');
var ans = math.multiply(one, pi);
print(ans.toString());

Will give you "3.14159265358979323846"

0x90
  • 34,073
  • 33
  • 137
  • 218
1

I'll cheat:

var pi = '3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019';

var piDecimal = function(numDigits) {
  return pi.substr(0, numDigits + 2); // Add 2 to compensate for "3."
};

for (var i = 1; i < 100; i++) {
  console.log(i + ' decimal place' + (i === 1 ? '' : 's') + ': ' + piDecimal(i));
}
Gershy
  • 5,378
  • 1
  • 27
  • 38