⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fntrigo.hpp

📁 浮点数基本运算 浮点数的基本运算主要有四则运算、符号处理、大小比较
💻 HPP
字号:
// Trigonometric functions implementation -*- C++ -*-

// Copyright (C) 2005 Ben T. Bear
//
// This file is published under GNU GPL 2.0. See more in "copy.hpp".

// #include "copy.hpp"

#ifndef __bt2il_fn_trigonometric_hpp
#define __bt2il_fn_trigonometric_hpp

#include "fnconst.hpp"

namespace btil
{
  namespace fn
  {
    template <int n>
    struct angle;

    template <typename x>
    struct sin;

    template <typename x>
    struct cos;

    template <typename x>
    struct tan;

    template <typename x>
    struct arcsin;



    template <int n>
    struct angle
    {
      struct value
      {
	static const double f_val = pi::f_val * n / 180.0;
      };
    };



    template <typename x>
    struct __sin;

    template <typename x>
    struct sin
    {
      static const double __t = (int)(x::f_val / (pi::f_val * 2.0));
      static const double __x = x::f_val - __t * 2.0 * pi::f_val;
      static const double __s = ((__x <= pi::f_val)
				 ? __x : __x - 2.0 * pi::f_val);
      
      struct xx
      {
	static const double f_val = __s;
      };

      typedef typename __sin<xx>::value value;
    };


    template <int n, typename x>
    struct __sin_item;

    template <typename x>
    struct __sin
    {
      typedef typename __sin_item<35, x>::sin_n value;
    };

    template <typename x>
    struct __sin_item<0, x>
    {
      struct item
      {
	static const double f_val = x::f_val;
      };

      typedef item sin_n;
    };

    template <int n, typename x>
    struct __sin_item
    {
      struct item
      {
	static const double f_val = (-__sin_item<n-1, x>::item::f_val
				     * x::f_val * x::f_val 
				     / (n*2) / (n*2+1));
      };

      struct sin_n
      {
	static const double f_val = (__sin_item<n-1, x>::sin_n::f_val
				     + item::f_val);
      };
    };



    template <typename x>
    struct cos
    {
      struct xx
      {
	static const double f_val = x::f_val + pi::f_val / 2.0;
      };
      
      typedef typename sin<xx>::value value;
    };



    template <typename x>
    struct tan
    {
      struct value
      {
	static const double f_val = (sin<x>::value::f_val
				     / cos<x>::value::f_val);
      };
    };



    template <int n, typename x>
    struct __arcsin_item
    {
      static const double item = (__arcsin_item<n-1, x>::item
				  * (2*n-1) / (2*n) * x::f_val * x::f_val);
      static const double as_n = (__arcsin_item<n-1, x>::as_n
				  + item / (2*n+1));
    };

    template <typename x>
    struct __arcsin_item<0, x>
    {
      static const double item = x::f_val;
      static const double as_n = item;
    };

    template <typename x>
    struct arcsin
    {
      static const bool s = x::f_val < 0.0;
      static const double abs = s ? -x::f_val : x::f_val;

      static const bool big = abs > sin<angle<45>::value>::value::f_val;

      struct __xx
      {
	static const double f_val = big ? (1.0 - abs*abs) : (abs*abs);
      };
      struct __x
      {
	static const double f_val = sqrt<__xx>::value::f_val;
      };

      static const double v = __arcsin_item<60, __x>::as_n;
      static const double vv = big ? (pi::f_val / 2.0 - v) : v;

      struct value
      {
	static const double f_val = s ? -vv : vv;
      };
    };
  }
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -