📄 pi.hpp~
字号:
// Pi implementation -*- C++ -*-// Copyright (C) 2005 Ben T. Bear//// This file is part of the BTB's Template<Integer> Library. This// library is free software; you can redistribute it and/or modify it// under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2, or (at your option)// any later version.// This library is distributed in the hope that it will be useful, but// WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// General Public License for more details.// You should have received a copy of the GNU General Public License// along with this library; see the file COPYING. If not, write to// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,// MA 02111-1307, USA.// As a special exception, you may use this file as part of a free// software library without restriction. Specifically, if other files// instantiate templates or use macros or inline functions from this// file, or you compile this file and link it with other files to// produce an executable, this file does not by itself cause the// resulting executable to be covered by the GNU General Public// License. This exception does not however invalidate any other// reasons why the executable file might be covered by the GNU General// Public License.#ifndef __bt2il_fn_pi_hpp#define __bt2il_fn_pi_hpp#include "fn.hpp"#include "factorial.hpp"namespace btil{ namespace fn { struct pi; struct e; template <int n> struct angle; struct __two { static const double f_val = 2.0; }; template <int n> struct __pi_n; template <int n> struct __pi_item; template <int n> struct __pi_item; template <> struct __pi_item<0> { struct item { static const double f_val = 0.0; }; typedef __two pi_n; }; template <int n> struct __pi_item { static const double a = 2.0 + __pi_item<n-1>::item::f_val; static const double x0 = 2.0; static const double x1 = (a / x0 + x0) / 2.0; static const double x2 = (a / x1 + x1) / 2.0; static const double x3 = (a / x2 + x2) / 2.0; static const double x4 = (a / x3 + x3) / 2.0; static const double x5 = (a / x4 + x4) / 2.0; static const double x6 = (a / x5 + x5) / 2.0; static const double x7 = (a / x6 + x6) / 2.0; static const double x8 = (a / x7 + x7) / 2.0; static const double x = x8; struct item { static const double f_val = x; }; struct pi_n { static const double f_val = __pi_item<n-1>::pi_n::f_val * 2.0 / x; }; }; template <bool c, int n> struct __pi_n_bool; template <int n> struct __pi_n { typedef typename __pi_n_bool<equal<typename __pi_item<n-1>::pi_n, typename __pi_item<n>::pi_n>::value, n>::value value; }; template <int n> struct __pi_n_bool<true, n> { typedef typename __pi_item<n>::pi_n value; }; template <int n> struct __pi_n_bool<false, n> { typedef typename __pi_n<n+1>::value value; }; struct pi { static const double f_val = __pi_item<35>::pi_n::f_val; }; template <int n> struct angle { struct value { static const double f_val = pi::f_val * n / 180.0; }; }; template <int n> struct __e_item { static const double e_n = (__e_item<n-1>::e_n + 1.0 / factorial<n>::value::f_val); }; template <> struct __e_item<0> { static const double e_n = factorial<0>::value::f_val; }; struct e { static const double f_val = __e_item<20>::e_n; }; }}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -