大数计算: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…...
设计模式和设计原则回顾
设计模式和设计原则回顾 23种设计模式是设计原则的完美体现,设计原则设计原则是设计模式的理论基石, 设计模式 在经典的设计模式分类中(如《设计模式:可复用面向对象软件的基础》一书中),总共有23种设计模式,分为三大类: 一、创建型模式(5种) 1. 单例模式(Sing…...

黑马Mybatis
Mybatis 表现层:页面展示 业务层:逻辑处理 持久层:持久数据化保存 在这里插入图片描述 Mybatis快速入门 
关于iview组件中使用 table , 绑定序号分页后序号从1开始的解决方案
问题描述:iview使用table 中type: "index",分页之后 ,索引还是从1开始,试过绑定后台返回数据的id, 这种方法可行,就是后台返回数据的每个页面id都不完全是按照从1开始的升序,因此百度了下,找到了…...
JVM垃圾回收机制全解析
Java虚拟机(JVM)中的垃圾收集器(Garbage Collector,简称GC)是用于自动管理内存的机制。它负责识别和清除不再被程序使用的对象,从而释放内存空间,避免内存泄漏和内存溢出等问题。垃圾收集器在Ja…...

【快手拥抱开源】通过快手团队开源的 KwaiCoder-AutoThink-preview 解锁大语言模型的潜力
引言: 在人工智能快速发展的浪潮中,快手Kwaipilot团队推出的 KwaiCoder-AutoThink-preview 具有里程碑意义——这是首个公开的AutoThink大语言模型(LLM)。该模型代表着该领域的重大突破,通过独特方式融合思考与非思考…...

Cinnamon修改面板小工具图标
Cinnamon开始菜单-CSDN博客 设置模块都是做好的,比GNOME简单得多! 在 applet.js 里增加 const Settings imports.ui.settings;this.settings new Settings.AppletSettings(this, HTYMenusonichy, instance_id); this.settings.bind(menu-icon, menu…...

【论文阅读28】-CNN-BiLSTM-Attention-(2024)
本文把滑坡位移序列拆开、筛优质因子,再用 CNN-BiLSTM-Attention 来动态预测每个子序列,最后重构出总位移,预测效果超越传统模型。 文章目录 1 引言2 方法2.1 位移时间序列加性模型2.2 变分模态分解 (VMD) 具体步骤2.3.1 样本熵(S…...

基于Springboot+Vue的办公管理系统
角色: 管理员、员工 技术: 后端: SpringBoot, Vue2, MySQL, Mybatis-Plus 前端: Vue2, Element-UI, Axios, Echarts, Vue-Router 核心功能: 该办公管理系统是一个综合性的企业内部管理平台,旨在提升企业运营效率和员工管理水…...

ZYNQ学习记录FPGA(一)ZYNQ简介
一、知识准备 1.一些术语,缩写和概念: 1)ZYNQ全称:ZYNQ7000 All Pgrammable SoC 2)SoC:system on chips(片上系统),对比集成电路的SoB(system on board) 3)ARM:处理器…...

Android写一个捕获全局异常的工具类
项目开发和实际运行过程中难免会遇到异常发生,系统提供了一个可以捕获全局异常的工具Uncaughtexceptionhandler,它是Thread的子类(就是package java.lang;里线程的Thread)。本文将利用它将设备信息、报错信息以及错误的发生时间都…...