全国统一服务热线:400-633-9193

JavaScript实现计算圆周率到小数点后100位的方法示例

    网络     2018-05-28    1298

本文实例讲述了JavaScript实现计算圆周率到小数点后100位的方法。分享给大家供大家参考,具体如下:

浮点数的有效数位是16位,我自己做了一个大数类,能存储100位有效数位,并实现了大数类的基本运算。我用它来计算圆周率(割圆法,即多边形逼近),得到了小数点后一百位有效数字,比对了Machin 公式的计算结果,没有误差。用时约2秒。

完整示例如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>www.jb51.net js计算圆周率</title>

</head>

<body>

<script>

<!--

function BigNum(str, n, b)

{

  /*

   BigNum -- 大数类

   私有成员:

         data -- 119 位数字,放在长度为 17 的数组里,每个数组元素存放 7 位数字。

         decimal_place -- 小数点的位置,从最左位开始算。

         positive -- 是否是正数。

         recalc() -- 为了尽可能存放最多的有效数位,去除前缀的 0,并重新计算小数点位置。

         init() -- 部分初始化工作。

   公有成员:

         BigNum( String str, INT n, BOOL b) --

              构造函数。参数:str -- 字符串,各个有效数位;n -- 整数,小数点位置,从最左位开始算,比如 BigNum("123", 2) = 12.3; BigNum("123", 0) = 0.123; BigNum("123", -2) = 0.00123;b -- 布尔值,是否是正数。

         Add( BigNum num ) -- 加法。

         Subtract( BigNum num ) -- 减法。

         Multiply( BigNum num ) -- 乘法。

         Divide( BigNum num ) -- 除法。

         SquareRoot() -- 平方根。

         toString() -- 转换为字符串(包括小数点),以便以文本形式输出计算结果。

         Clone() -- 复制。

  */

  this.recalc = function() /* 去除前缀的 0,并重新计算小数点位置 */

  {

    for(var i = 0; i < 17; i ++)

    {

       if(this.data[0] != 0) break;

       this.data.shift();

       this.data.push(0);

       this.decimal_place --;

    }

  }

  this.init = function() /* 部分初始化工作 */

  {

    this.decimal_place = Math.ceil( n / 7 ); //小数点位置

    this.data = new Array(17); //保存有效数位的数组

    if(n % 7 > 0)

    {

       var arr = new Array( 8 - n % 7 );

    }

    else

    {

       var arr = new Array( 1 - n % 7 );

    }

    str = arr.join("0") + str;

    if(str.length > 119)

    {

       str = str.substr(0, 119);

    }

    else if(str.length < 119)

    {

       var arr = new Array(120 - str.length);

       str += arr.join("0");

    }

    for( var i = 0; i < 17; i ++ )

    {

       this.data[i] = parseInt( str.substr(i * 7, 7) , 10 );

    }

  }

  /* 初始化开始 */

  this.positive = b;

  if( ! /^0*$/.test(str) )

  {

    this.init();

    this.recalc();

  }

  else

  {

    this.data = new Array(17);

    for( var i = 0; i < 17; i ++ )

    {

       this.data[i] = 0;

    }

    this.decimal_place = 0;

  }

  /* 初始化结束 */

  this.Add = function(num) /* 加法 */

  {

    if(this.positive && !num.positive)

    {

       num.positive = true;

       var result = this.Subtract(num);

       num.positive = false;

       return result;

    }

    else if(num.positive && !this.positive)

    {

       this.positive = true;

       var result = num.Subtract(this);

       this.positive = false;

       return result;

    }

    var result = new BigNum("", 0, this.positive);

    var num1,num2;

    if(this.decimal_place >= num.decimal_place)

    {

       num1 = this;

       num2 = num;

    }

    else

    {

       num1 = num;

       num2 = this;

    }

    result.decimal_place = num1.decimal_place;

    if(num1.decimal_place - num2.decimal_place >= 17)

    {

       for(var i = 0; i < 17; i ++)

       {

         result.data[i] = num1.data[i];

       }

       return result;

    }

    var nOffDec = num1.decimal_place - num2.decimal_place;

    var nTmp = 0;

    for( var i = 16; i >= 0; i -- )

    {

       var nTmp1 = i - nOffDec;

       var nTmp2 = 0;

       if(nTmp1 >= 0)

       {

         nTmp2 = num1.data[i] + num2.data[nTmp1];

       }

       else

       {

         nTmp2 = num1.data[i];

       }

       nTmp2 += nTmp;

       nTmp = Math.floor(nTmp2 / 10000000);

       result.data[i] = nTmp2 % 10000000;

    }

    if(nTmp > 0)

    {

       result.data.unshift(nTmp);

       result.decimal_place ++;

    }

    return result;

  }

  this.Subtract = function(num) /* 减法 */

  {

    if(this.positive && !num.positive)

    {

       num.positive = true;

       var result = this.Add(num);

       num.positive = false;

       return result;

    }

    else if(!this.positive && num.positive)

    {

       this.positive = true;

       var result = this.Add(num);

       result.positive = false;

       this.positive = false;

       return result;

    }

    else

    {

       var num1 = num2 = null;

       var bPositive;

       if(this.decimal_place > num.decimal_place)

       {

         num1 = this;

         num2 = num;

         bPositive = this.positive;

       }

       else if(this.decimal_place < num.decimal_place)

       {

         num1 = num;

         num2 = this;

         bPositive = !this.positive;

       }

       else

       {

         for( var i = 0; i < 17; i ++ )

         {

           if(this.data[i] > num.data[i])

           {

              num1 = this;

              num2 = num;

              bPositive = this.positive;

              break;

           }

           else if(this.data[i] < num.data[i])

           {

              num1 = num;

              num2 = this;

              bPositive = !this.positive;

              break;

           }

         }

       }

       if( num1 == null)

       {

         return new BigNum("", 0, true);

       }

       else

       {

         if(num1.decimal_place - num2.decimal_place >= 17)

         {

           var result = new BigNum("", 0, bPositive);

           for(var i = 0; i < 17; i ++)

           {

              result.data[i] = num1.data[i];

           }

           result.decimal_place = num1.decimal_place;

           return result;

         }

         var result = new BigNum("", 0, bPositive);

         result.decimal_place = num1.decimal_place;

         var nOffDec = num1.decimal_place - num2.decimal_place;

         var nTmp = 0;

         for( var i = 16; i >= 0; i -- )

         {

           var nTmp1 = i - nOffDec;

           var nTmp2 = 0;

           if(nTmp1 >= 0)

           {

              nTmp2 = 10000000 + nTmp + num1.data[i] - num2.data[nTmp1];

           }

           else

           {

              nTmp2 = 10000000 + nTmp + num1.data[i];

           }

           if(nTmp2 >= 10000000)

           {

              result.data[i] = nTmp2 - 10000000;

              nTmp = 0;

           }

           else

           {

              result.data[i] = nTmp2;

              nTmp = -1;

           }

         }

         result.recalc();

         return result;

       }

    }

  }

  this.Multiply = function(num) /* 乘法 */

  {

    var bPositive;

    var nDecimalPlace = this.decimal_place + num.decimal_place - 1;

    if(this.positive == num.positive)

    {

       bPositive = true;

    }

    else

    {

       bPositive = false;

    }

    var result = new BigNum("", 0, bPositive);

    var nTmpData = 0;

    for( var i = 16; i >= 0; i -- )

    {

       for( var j = 16; j >= 0; j -- )

       {

         if(isNaN(result.data[j + i]))

           result.data[j + i] = 0;

         result.data[j + i] += this.data[j] * num.data[i];

         if(result.data[j + i] >= 10000000)

         {

           if( j + i -1 >= 0 )

           {

              result.data[j + i -1] += Math.floor(result.data[j + i] / 10000000);

           }

           else

           {

              nTmpData += Math.floor(result.data[j + i] / 10000000);

           }

           result.data[j + i] = result.data[j + i] % 10000000;

         }

       }

    }

    if(nTmpData > 0)

    {

       result.data.unshift(nTmpData);

       result.data.pop();

       nDecimalPlace ++;

    }

    result.decimal_place += nDecimalPlace;

    return result;

  }

  this.Divide = function(num) /* 除法 */

  {

    var bPositive;

    var nDecimalPlace = this.decimal_place - num.decimal_place + 1;

    if(this.positive == num.positive)

    {

       bPositive = true;

    }

    else

    {

       bPositive = false;

    }

    var result = new BigNum("", 0, bPositive);

    var arrTemp = new Array(17);

    for( var i = 0; i < 17; i ++ )

    {

       arrTemp[i] = this.data[i];

    }

    var bTest = true;

    var nTest = 0;

    for( var i = 0; i < 17; i ++ )

    {

       if(bTest)

       {

         nTest = Math.floor( ( arrTemp[0] * 10000000 + arrTemp[1] ) / ( num.data[0] * 10000000 + num.data[1] ) );

       }

       else

       {

         bTest = true;

       }

       if(nTest == 0)

       {

         result.data[i] = 0;

         arrTemp[1] += arrTemp[0] * 10000000;

         arrTemp.shift();

         arrTemp.push(0);

         continue;

       }

       var arrTemp1 = new Array(17);

       for( var j = 0; j < 17; j ++ )

       {

         arrTemp1[j] = 0;

       }

       for( var j = 16; j >= 0; j -- )

       {

         arrTemp1[j] += nTest * num.data[j];

         if(arrTemp1[j] >= 10000000)

         {

           if(j != 0)

           {

              arrTemp1[j - 1] += Math.floor( arrTemp1[j] / 10000000);

              arrTemp1[j] = arrTemp1[j] % 10000000;

           }

         }

       }

       for( var j = 0; j < 17; j ++ )

       {

         if(arrTemp[j] < arrTemp1[j])

         {

           bTest = false;

           break;

         }

         else if(arrTemp[j] > arrTemp1[j])

         {

           break;

         }

       }

       if(bTest)

       {

         result.data[i] = nTest;

         for( var j = 16; j >= 0; j -- )

         {

           if(arrTemp[j] >= arrTemp1[j])

           {

              arrTemp[j] -= arrTemp1[j];

           }

           else

           {

              arrTemp[j] = 10000000 + arrTemp[j] - arrTemp1[j];

              arrTemp[j - 1] --;

           }

         }

       }

       else

       {

         nTest --;

         i --;

         continue;

       }

       arrTemp[1] += arrTemp[0] * 10000000;

       arrTemp.shift();

       arrTemp.push(0);

    }

    result.decimal_place = nDecimalPlace;

    result.recalc();

    return result;

  }

  this.SquareRoot = function() /* 平方根 */

  {

    var result = new BigNum("", 0, true);

    nDecimalPlace = Math.ceil(this.decimal_place / 2);

    var arrTemp = new Array(17);

    for(var i = 0; i < 17; i ++)

    {

       arrTemp[i] = this.data[i];

    }

    var bTest = true;

    for(var i = 0; i < 17; i ++)

    {

       if( i == 0 )

       {

         if(this.decimal_place % 2 == 0)

         {

           var nTemp = arrTemp[0] * 10000000 + arrTemp[1];

           var nTemp1 = Math.floor( Math.sqrt( nTemp ) );

           var nTemp2 = nTemp - nTemp1 * nTemp1;

           arrTemp[0] = 0;

           arrTemp[1] = nTemp2;

           arrTemp.shift();

           arrTemp.push(0);

         }

         else

         {

           var nTemp1 = Math.floor( Math.sqrt( arrTemp[0] ) );

           var nTemp2 = arrTemp[0] - nTemp1 * nTemp1;

           arrTemp[0] = nTemp2;

         }

       }

       else

       {

         if(bTest)

         {

           if( i == 1 )

           {

              nTemp1 = Math.sqrt( (arrTemp[0] * 10000000 + arrTemp[1]) + 100000000000000 * Math.pow(result.data[0], 2) ) - 10000000 * result.data[0];

              nTemp1 = Math.floor(nTemp1);

           }

           else

           {

              nTemp = result.data[0] * 10000000 + result.data[1];

              nTemp1 = Math.floor( ( arrTemp[0] * 10000000 + arrTemp[1] ) / ( 2 * nTemp ) );

           }

         }

         else

         {

           bTest = true;

         }

         var arrTemp1 = new Array(17);

         var nTemp3 = 0

         for( var j = i - 1; j >= 0; j -- )

         {

           arrTemp1[j] = result.data[j] * 2 + nTemp3;

           if( arrTemp1[j] >= 10000000 && j > 0 )

           {

              nTemp3 = 1;

              arrTemp1[j] = arrTemp1[j] % 10000000;

           }

           else

           {

              nTemp3 = 0;

           }

         }

         arrTemp1[i] = nTemp1;

         nTemp3 = 0;

         for( var j = i; j >= 0; j -- )

         {

           arrTemp1[j] = arrTemp1[j] * nTemp1 + nTemp3;

           if( arrTemp1[j] >= 10000000 && j > 0 )

           {

              nTemp3 = Math.floor( arrTemp1[j] / 10000000 );

              arrTemp1[j] = arrTemp1[j] % 10000000;

           }

           else

           {

              nTemp3 = 0;

           }

         }

         var arrTemp2 = new Array(17);

         for( var j = 0; j < 17; j ++ )

         {

           arrTemp2[j] = arrTemp[j];

         }

         for( var j = i; j >= 0; j -- )

         {

           if( arrTemp2[j] >= arrTemp1[j] )

           {

              arrTemp2[j] -= arrTemp1[j];

           }

           else

           {

              if(j > 0)

              {

                arrTemp2[j] = arrTemp2[j] + 10000000 - arrTemp1[j];

                arrTemp2[j - 1] --;

              }

              else

              {

                bTest = false;

                break;

              }

           }

         }

         if(bTest)

         {

           arrTemp = arrTemp2;

         }

         else

         {

           nTemp1 --;

           i --;

           continue;

         }

       }

       result.data[i] = nTemp1;

       arrTemp[1] += arrTemp[0] * 10000000;

       arrTemp.shift();

       arrTemp.push(0);

    }

    result.decimal_place = nDecimalPlace;

    result.recalc();

    return result;

  }

  this.toString = function() /* 转换为字符串(包括小数点),以便以文本形式输出计算结果 */

  {

    var szData = "";

    var szOutPut = "";

    this.recalc();

    for( var i = 0; i < 17; i ++ )

    {

       var szTmpData = this.data[i].toString()

       var arr = new Array(8 - szTmpData.length);

       szData += arr.join("0") + szTmpData;

    }

    if( /^0*$/.test(szData) )

    {

       return "0";

    }

    var n = this.decimal_place * 7;

    for(var i = 0; i < 7; i++)

    {

       if(szData.substr(i, 1) != 0) break;

       n --;

    }

    szData = szData.replace(/^0+/g,"");

    szData = szData.substr(0, 101);

    szData = szData.replace(/0+$/g,"");

    if( n < 1 )

    {

       szOutPut = szData.substr(0, 1) +

         ( ( szData.length > 1 ) ? "." : "" ) +

         szData.substr(1) + "e" + ( n - 1 ).toString();

    }

    else if(n == szData.length)

    {

       szOutPut = szData;

    }

    else if(n > szData.length)

    {

       szOutPut = szData.substr(0, 1) + "." + szData.substr(1) + "e+" + (n - 1).toString();

    }

    else

    {

       szOutPut = szData.substr(0, n) + "." + szData.substr(n);

    }

    return ( this.positive ? "" : "-" ) + szOutPut;

  }

  this.Clone = function()   /* 复制 */

  {

    var result = new BigNum("", 0, true);

    for( var i = 0; i < 17; i ++)

    {

       result.data[i] = this.data[i];

    }

    result.decimal_place = this.decimal_place;

    result.positive = this.positive;

    return result;

  }

}

var a = new BigNum("1", 1, true)

var count = 168;

var nTwo = new BigNum("2", 1, true);

function loop(intTmpvar,intCount)

{

  if(intCount == 0) return intTmpvar;

  var v1 = intTmpvar.Divide( nTwo );

  var v11 = v1.Clone();

  var nTemp = v1.Multiply( v11 );

  var a1 = a.Clone();

  a1 = a.Multiply(a1);

  var nTemp1 = a1.Subtract( nTemp )

  v2 = nTemp1.SquareRoot();

  v3 = a.Subtract( v2 );

  var v31 = v3.Clone();

  var nTemp2 = v3.Multiply( v31 );

  var nTemp3 = nTemp2.Add(nTemp);

  v4 = nTemp3.SquareRoot();

  return loop( v4 , -- intCount )

}

var a1 = a.Clone();

var nTemp = a.Multiply(a1);

var nTemp1 = nTemp.Clone();

nTemp = nTemp.Add(nTemp1);

nTemp = loop(nTemp.SquareRoot(), count);

var nFour = new BigNum("4", 1, true);

nTemp = nTemp.Multiply( nFour );

nTemp1 = new BigNum("2", 1, true);

var nTemp2 = new BigNum("2", 1, true);

for(var i = 0; i < count - 1; i ++)

{

  nTemp1 = nTemp1.Multiply( nTemp2 );

}

nTemp = nTemp.Multiply( nTemp1 );

nTemp = nTemp.Divide( nTwo );

nTemp = nTemp.Divide( a );

document.write( nTemp )

//-->

</script>

</body>

</html>

运行结果:

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679


  分享到:  
0.3268s