大数计算:e^1000/300!
1.问题:大数计算可能超出数据类型范围
当单独计算 ,因为
,double的最大取值为1.79769e+308,所以
肯定超过了double的表示范围。
同样,对于300!也是如此。
那我们应该怎么去计算和存储结果呢?
2.解决方案
从数学角度出发,对于超级大的数,运算方式、运算规律等肯定保持不变的。
很多时候,我们主要是利用相关的定理、公式进行简化或者极限处理。
由于我项目里的精度要求就 e-10,于是,可以采用相对宽松的方式解决这个问题:
科学计数法!
3.代码实现
#ifndef __BigNumeric_h
#define __BigNumeric_h
#include <cmath>template<typename _T>
class BigNumeric
{
private:_T mantissa; //基数int exponent; //指数public:BigNumeric(_T num){if (num == 0) {exponent = 0;mantissa = 0;}else{exponent = std::floor(std::log10(std::abs(num)));mantissa = num / std::pow(10, exponent);}}~BigNumeric(){}BigNumeric(const BigNumeric& other){if (this == &other) return;this->exponent = other.exponent;this->mantissa = other.mantissa;}BigNumeric& operator=(const BigNumeric& other){/*if(this == &other) return *this;*/this->exponent = other.exponent;this->mantissa = other.mantissa;return *this;}public:_T value(){if (this->exponent >= 308) return 0.0;return this->mantissa * std::pow(10.0, this->exponent);}//乘法BigNumeric& operator*(const BigNumeric& opr){BigNumeric<_T> resmnt(this->mantissa * opr.mantissa);this->exponent = resmnt.exponent + this->exponent + opr.exponent;this->mantissa = resmnt.mantissa;return *this;}BigNumeric& operator*(const _T opr){BigNumeric<_T> oprbgn(opr);*this = *this * oprbgn;return *this;}friend BigNumeric operator*(const _T opr1, BigNumeric& opr2){return opr2 * opr1;}//除法BigNumeric& operator/(const BigNumeric& opr){BigNumeric<_T> resmnt(this->mantissa / opr.mantissa);this->exponent = resmnt.exponent + this->exponent - opr.exponent;this->mantissa = resmnt.mantissa;return *this;}BigNumeric& operator/(const _T opr){BigNumeric<_T> oprbgn(opr);*this = *this / oprbgn;return *this;}friend BigNumeric operator/(const _T opr, const BigNumeric& opr1){BigNumeric<_T> oprbgn(opr);return oprbgn / opr1;}//加法BigNumeric& operator+(const BigNumeric& opr){if (this->exponent - opr.exponent > 15) return *this;else if (this->exponent - opr.exponent < -15){*this = opr;return *this;} int min = this->exponent > opr.exponent ? opr.exponent : this->exponent;BigNumeric<_T> resmnt(this->mantissa * std::pow(10.0, this->exponent - min) + opr.mantissa * std::pow(10.0, opr.exponent - min));this->exponent = resmnt.exponent + min;this->mantissa = resmnt.mantissa;return *this;}BigNumeric& operator+(const _T opr){BigNumeric<_T> oprbgn(opr);*this = *this + oprbgn;return *this;}friend BigNumeric operator+(const _T opr1, BigNumeric& opr2){return opr2 + opr1;}//减法BigNumeric& operator-(const BigNumeric& opr){BigNumeric temp(opr);*this = *this + temp * (-1.0);return *this;}BigNumeric& operator-(const _T opr){BigNumeric oprbgn(opr);*this = *this - oprbgn;return *this;}friend BigNumeric operator-(const _T opr1, BigNumeric& opr2){return opr2 - opr1;}//开方BigNumeric& Sqrt(){_T bgnmant = std::sqrt(this->mantissa);int bgnexp = this->exponent;if (bgnexp % 2 == 0){this->mantissa = bgnmant;this->exponent = bgnexp / 2;}else{BigNumeric temp(bgnmant * std::sqrt(10.0));this->mantissa = temp.mantissa;this->exponent = temp.exponent + bgnexp / 2;}return *this;}//幂BigNumeric& Pow(_T exp){BigNumeric temp(Vpow(this->mantissa, exp));this->mantissa = temp.mantissa;this->exponent = temp.exponent + this->exponent * exp;return *this;}public:static BigNumeric Factorial(int opr){if (opr < 0) return 1.0 / Factorial(-1.0 * opr + 1);else if (opr == 0) return BigNumeric(1.0);return Factorial(opr - 1) * opr;}static BigNumeric Epow(_T exp){BigNumeric res(1.0);double e = 2.71828182845904523536;if (std::abs(exp) <= 700) return BigNumeric(std::pow(e, exp));int count = exp / 700;BigNumeric bgn(std::pow(e, 700.0));for (size_t i = 0; i < count; i++)res = res * bgn;BigNumeric bgn1(std::pow(e, exp - count * 700));res = res * bgn1;return res;}static BigNumeric Vpow(_T e, _T exp){BigNumeric res(1.0);BigNumeric bgnmant(e);int chk = bgnmant.exponent == 0 ? std::abs(exp) : std::abs(exp) * bgnmant.exponent;if (chk <= 300) return BigNumeric(std::pow(e, exp));int count = exp / 300;BigNumeric bgn(std::pow(e, 300.0));for (size_t i = 0; i < count; i++)res = res * bgn;BigNumeric bgn1(std::pow(e, exp - count * 300));res = res * bgn1;return res;}};#endif // !__BigNumeric_h
4.测试
//测试
#include "BigNumeric.hpp"int main() {BigNumeric<double> bignum = BigNumeric<double>::Factorial(300);BigNumeric<double> bignum1 = BigNumeric<double>::Epow(1000);bignum = bignum1 / bignum;return 0;
}
结果:6.4369310844548986e-181,数字部分精度为 e-12,指数部分完全准确。
相关文章:
大数计算:e^1000/300!
1.问题:大数计算可能超出数据类型范围 当单独计算 ,因为 ,double的最大取值为1.79769e308,所以 肯定超过了double的表示范围。 同样,对于300!也是如此。 那我们应该怎么去计算和存储结果呢?…...

力扣164最大间距
1.前言 因为昨天写了一个基数排序,今天我来写一道用基数排序实现的题解,希望可以帮助你理解基数排序。 这个题本身不难,就是线性时间和线性额外空间(O(n))的算法,有点难实现 基数排序的时间复杂度是O(d*(nradix)),其中…...

聚观早报 | “百度世界2023”即将举办;2024款岚图梦想家上市
【聚观365】10月13日消息 “百度世界2023”即将举办 2024款岚图梦想家上市 腾势D9用户超10万 华为发布新一代GigaGreen Radio OpenAI拟进行重大更新 “百度世界2023”即将举办 “百度世界2023”将于10月17日在北京首钢园举办。届时,百度创始人、董事长兼首席执…...
Windows 应用程序监控重启
执行思路 1.定时关闭可执行程序,2.再通过定时监控启动可执行程序 定时启动关闭程序.bat echo off cd "D:\xxxx\" :: 可执行程序目录 Start "" /b xxxx.exe :: 可执行程序 timeout /T 600 /nobreak >nul :: 600秒 taskkill /IM xxxx.exe /…...

springboot 通过url下载文件并上传到OSS
DEMO流程 传入一个需要下载并上传的url地址下载文件上传文件并返回OSS的url地址 springboot pom文件依赖 <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w…...

docker创建elasticsearch、elasticsearch-head部署及简单操作
elasticsearch部署 1 拉取elasticsearch镜像 docker pull elasticsearch:7.7.0 2 创建文件映射路径 mkdir /mydata/elasticsearch/data mkdir /mydata/elasticsearch/plugins mkdir /mydata/elasticsearch/config 3 文件夹授权 chmod 777 /mydata/elastic…...

竞赛选题 深度学习+python+opencv实现动物识别 - 图像识别
文章目录 0 前言1 课题背景2 实现效果3 卷积神经网络3.1卷积层3.2 池化层3.3 激活函数:3.4 全连接层3.5 使用tensorflow中keras模块实现卷积神经网络 4 inception_v3网络5 最后 0 前言 🔥 优质竞赛项目系列,今天要分享的是 🚩 *…...

Codeforces Round 903 (Div. 3)ABCDE
Codeforces Round 903 (Div. 3)ABCDE 目录 A. Dont Try to Count题目大意思路核心代码 B. Three Threadlets题目大意思路核心代码 C. Perfect Square题目大意思路核心代码 D. Divide and Equalize题目大意思路核心代码 E. Block Sequence题目大意思路核心代码 A. Don’t Try t…...
C# 与 C/C++ 的交互
什么是平台调用 (P/Invoke) P/Invoke 是可用于从托管代码访问非托管库中的结构、回调和函数的一种技术。 托管代码与非托管的区别 托管代码和非托管代码的主要区别是内存管理方式和对计算机资源的访问方式。托管代码通常运行在托管环境中,如 mono 或 java 虚拟机等…...

新版Android Studio搜索不到Lombok以及无法安装Lombok插件的问题
前言 在最近新版本的Android Studio中,使用插件时,在插件市场无法找到Lombox Plugin,具体表现如下图所示: 1、操作步骤: (1)打开Android Studio->Settings->Plugins,搜索Lom…...
BST二叉搜索树
文章目录 概述实现创建节点查找节点增加节点查找后驱值根据关键词删除找到树中所有小于key的节点的value 概述 二叉搜索树,它具有以下的特性,树节点具有一个key属性,不同节点之间key是不能重复的,对于任意一个节点,它…...
【Leetcode】211. 添加与搜索单词 - 数据结构设计
一、题目 1、题目描述 请你设计一个数据结构,支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 。 实现词典类 WordDictionary : WordDictionary() 初始化词典对象void addWord(word) 将 word 添加到数据结构中,之后可以对它…...

Discuz户外旅游|旅行游记模板/Discuz!旅行社、旅游行业门户网站模板
价值328的discuz户外旅游|旅行游记模板,本模板需要配套【仁天际-PC模板管理】插件使用。 模板说明 1、模板页面宽度1200px,简洁大气,较适合户外旅行、骑行、游记、摩旅、旅游、活动等类型的论坛、频道网站; 2、所优化的页面有&…...
【重拾C语言】十一、外部数据组织——文件
目录 前言 十一、外部数据组织——文件 11.1 重新考虑户籍管理问题——文件 11.2 文件概述 11.2.1 文件分类 11.2.2 文件指针、标记及文件操作 11.3 打开、关闭文件 11.4 I/O操作 11.4.1 字符读写 11.4.2 字符串读写 11.4.3 格式化读写 11.4.4 数据块读写 11.4.5 …...

dpdk/spdk/网络协议栈/存储/网关开发/网络安全/虚拟化/ 0vS/TRex/dpvs技术专家成长体系教程
课程围绕安全,网络,存储,云原生4个维度去讲解核心技术点。 6个专栏组成:dpdk网络专栏、存储技术专栏、安全与网关开发专栏、虚拟化与云原生专栏、测试工具专栏、性能测试专栏 一、dpdk网络 dpdk基础知识 多队列网卡࿰…...

树莓派玩转openwrt软路由:5.OpenWrt防火墙配置及SSH连接
1、SSH配置 打开System -> Administration,打开SSH Access将Interface配置成unspecified。 如果选中其他的接口表示仅在给定接口上侦听,如果未指定,则在所有接口上侦听。在未指定下,所有的接口均可通过SSH访问认证。 2、防火…...
Gin:获取本机IP,获取访问IP
获取本机IP func GetLocalIP() []string {var ipStr []stringnetInterfaces, err : net.Interfaces()if err ! nil {fmt.Println("net.Interfaces error:", err.Error())return ipStr}for i : 0; i < len(netInterfaces); i {if (netInterfaces[i].Flags & ne…...
缓存降级代码结构设计
缓存降级设计思想 接前文缺陷点 本地探针应该增加计数器,多次异常再设置,避免网络波动造成误判。耦合度过高,远端缓存和本地缓存应该平行关系被设计为上下游关系了。公用的远端缓存的操作方法应该私有化,避免集成方代码误操作&…...

一文深入理解高并发服务器性能优化
我们现在已经搞定了 C10K并发连接问题 ,升级一下,如何支持千万级的并发连接?你可能说,这不可能。你说错了,现在的系统可以支持千万级的并发连接,只不过所使用的那些激进的技术,并不为人所熟悉。…...
pytorch中的归一化函数
在 PyTorch 的 nn 模块中,有一些常见的归一化函数,用于在深度学习模型中进行数据的标准化和归一化。以下是一些常见的归一化函数: nn.BatchNorm1d, nn.BatchNorm2d, nn.BatchNorm3d: 这些函数用于批量归一化 (Batch Normalization…...

未来机器人的大脑:如何用神经网络模拟器实现更智能的决策?
编辑:陈萍萍的公主一点人工一点智能 未来机器人的大脑:如何用神经网络模拟器实现更智能的决策?RWM通过双自回归机制有效解决了复合误差、部分可观测性和随机动力学等关键挑战,在不依赖领域特定归纳偏见的条件下实现了卓越的预测准…...
【Linux】shell脚本忽略错误继续执行
在 shell 脚本中,可以使用 set -e 命令来设置脚本在遇到错误时退出执行。如果你希望脚本忽略错误并继续执行,可以在脚本开头添加 set e 命令来取消该设置。 举例1 #!/bin/bash# 取消 set -e 的设置 set e# 执行命令,并忽略错误 rm somefile…...

docker详细操作--未完待续
docker介绍 docker官网: Docker:加速容器应用程序开发 harbor官网:Harbor - Harbor 中文 使用docker加速器: Docker镜像极速下载服务 - 毫秒镜像 是什么 Docker 是一种开源的容器化平台,用于将应用程序及其依赖项(如库、运行时环…...
Oracle查询表空间大小
1 查询数据库中所有的表空间以及表空间所占空间的大小 SELECTtablespace_name,sum( bytes ) / 1024 / 1024 FROMdba_data_files GROUP BYtablespace_name; 2 Oracle查询表空间大小及每个表所占空间的大小 SELECTtablespace_name,file_id,file_name,round( bytes / ( 1024 …...

2021-03-15 iview一些问题
1.iview 在使用tree组件时,发现没有set类的方法,只有get,那么要改变tree值,只能遍历treeData,递归修改treeData的checked,发现无法更改,原因在于check模式下,子元素的勾选状态跟父节…...
第25节 Node.js 断言测试
Node.js的assert模块主要用于编写程序的单元测试时使用,通过断言可以提早发现和排查出错误。 稳定性: 5 - 锁定 这个模块可用于应用的单元测试,通过 require(assert) 可以使用这个模块。 assert.fail(actual, expected, message, operator) 使用参数…...

相机从app启动流程
一、流程框架图 二、具体流程分析 1、得到cameralist和对应的静态信息 目录如下: 重点代码分析: 启动相机前,先要通过getCameraIdList获取camera的个数以及id,然后可以通过getCameraCharacteristics获取对应id camera的capabilities(静态信息)进行一些openCamera前的…...

Module Federation 和 Native Federation 的比较
前言 Module Federation 是 Webpack 5 引入的微前端架构方案,允许不同独立构建的应用在运行时动态共享模块。 Native Federation 是 Angular 官方基于 Module Federation 理念实现的专为 Angular 优化的微前端方案。 概念解析 Module Federation (模块联邦) Modul…...
【C语言练习】080. 使用C语言实现简单的数据库操作
080. 使用C语言实现简单的数据库操作 080. 使用C语言实现简单的数据库操作使用原生APIODBC接口第三方库ORM框架文件模拟1. 安装SQLite2. 示例代码:使用SQLite创建数据库、表和插入数据3. 编译和运行4. 示例运行输出:5. 注意事项6. 总结080. 使用C语言实现简单的数据库操作 在…...

深度学习习题2
1.如果增加神经网络的宽度,精确度会增加到一个特定阈值后,便开始降低。造成这一现象的可能原因是什么? A、即使增加卷积核的数量,只有少部分的核会被用作预测 B、当卷积核数量增加时,神经网络的预测能力会降低 C、当卷…...