当前位置: 首页 > news >正文

大数计算:e^1000/300!

1.问题:大数计算可能超出数据类型范围

当单独计算 e^{1000},因为 e^{1000} > e^{700} \approx 1.01432*e304,double的最大取值为1.79769e+308,所以 e^{1000} 肯定超过了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.问题&#xff1a;大数计算可能超出数据类型范围 当单独计算 &#xff0c;因为 &#xff0c;double的最大取值为1.79769e308&#xff0c;所以 肯定超过了double的表示范围。 同样&#xff0c;对于300&#xff01;也是如此。 那我们应该怎么去计算和存储结果呢&#xff1f;…...

力扣164最大间距

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

聚观早报 | “百度世界2023”即将举办;2024款岚图梦想家上市

【聚观365】10月13日消息 “百度世界2023”即将举办 2024款岚图梦想家上市 腾势D9用户超10万 华为发布新一代GigaGreen Radio OpenAI拟进行重大更新 “百度世界2023”即将举办 “百度世界2023”将于10月17日在北京首钢园举办。届时&#xff0c;百度创始人、董事长兼首席执…...

Windows 应用程序监控重启

执行思路 1.定时关闭可执行程序&#xff0c;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 激活函数&#xff1a;3.4 全连接层3.5 使用tensorflow中keras模块实现卷积神经网络 4 inception_v3网络5 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; *…...

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 是可用于从托管代码访问非托管库中的结构、回调和函数的一种技术。 托管代码与非托管的区别 托管代码和非托管代码的主要区别是内存管理方式和对计算机资源的访问方式。托管代码通常运行在托管环境中&#xff0c;如 mono 或 java 虚拟机等…...

新版Android Studio搜索不到Lombok以及无法安装Lombok插件的问题

前言 在最近新版本的Android Studio中&#xff0c;使用插件时&#xff0c;在插件市场无法找到Lombox Plugin&#xff0c;具体表现如下图所示&#xff1a; 1、操作步骤&#xff1a; &#xff08;1&#xff09;打开Android Studio->Settings->Plugins&#xff0c;搜索Lom…...

BST二叉搜索树

文章目录 概述实现创建节点查找节点增加节点查找后驱值根据关键词删除找到树中所有小于key的节点的value 概述 二叉搜索树&#xff0c;它具有以下的特性&#xff0c;树节点具有一个key属性&#xff0c;不同节点之间key是不能重复的&#xff0c;对于任意一个节点&#xff0c;它…...

【Leetcode】211. 添加与搜索单词 - 数据结构设计

一、题目 1、题目描述 请你设计一个数据结构&#xff0c;支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 。 实现词典类 WordDictionary &#xff1a; WordDictionary() 初始化词典对象void addWord(word) 将 word 添加到数据结构中&#xff0c;之后可以对它…...

Discuz户外旅游|旅行游记模板/Discuz!旅行社、旅游行业门户网站模板

价值328的discuz户外旅游|旅行游记模板&#xff0c;本模板需要配套【仁天际-PC模板管理】插件使用。 模板说明 1、模板页面宽度1200px&#xff0c;简洁大气&#xff0c;较适合户外旅行、骑行、游记、摩旅、旅游、活动等类型的论坛、频道网站&#xff1b; 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技术专家成长体系教程

课程围绕安全&#xff0c;网络&#xff0c;存储&#xff0c;云原生4个维度去讲解核心技术点。 6个专栏组成&#xff1a;dpdk网络专栏、存储技术专栏、安全与网关开发专栏、虚拟化与云原生专栏、测试工具专栏、性能测试专栏 一、dpdk网络 dpdk基础知识 多队列网卡&#xff0…...

树莓派玩转openwrt软路由:5.OpenWrt防火墙配置及SSH连接

1、SSH配置 打开System -> Administration&#xff0c;打开SSH Access将Interface配置成unspecified。 如果选中其他的接口表示仅在给定接口上侦听&#xff0c;如果未指定&#xff0c;则在所有接口上侦听。在未指定下&#xff0c;所有的接口均可通过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…...

缓存降级代码结构设计

缓存降级设计思想 接前文缺陷点 本地探针应该增加计数器&#xff0c;多次异常再设置&#xff0c;避免网络波动造成误判。耦合度过高&#xff0c;远端缓存和本地缓存应该平行关系被设计为上下游关系了。公用的远端缓存的操作方法应该私有化&#xff0c;避免集成方代码误操作&…...

一文深入理解高并发服务器性能优化

我们现在已经搞定了 C10K并发连接问题 &#xff0c;升级一下&#xff0c;如何支持千万级的并发连接&#xff1f;你可能说&#xff0c;这不可能。你说错了&#xff0c;现在的系统可以支持千万级的并发连接&#xff0c;只不过所使用的那些激进的技术&#xff0c;并不为人所熟悉。…...

pytorch中的归一化函数

在 PyTorch 的 nn 模块中&#xff0c;有一些常见的归一化函数&#xff0c;用于在深度学习模型中进行数据的标准化和归一化。以下是一些常见的归一化函数&#xff1a; nn.BatchNorm1d, nn.BatchNorm2d, nn.BatchNorm3d&#xff1a; 这些函数用于批量归一化 (Batch Normalization…...

iOS 26 携众系统重磅更新,但“苹果智能”仍与国行无缘

美国西海岸的夏天&#xff0c;再次被苹果点燃。一年一度的全球开发者大会 WWDC25 如期而至&#xff0c;这不仅是开发者的盛宴&#xff0c;更是全球数亿苹果用户翘首以盼的科技春晚。今年&#xff0c;苹果依旧为我们带来了全家桶式的系统更新&#xff0c;包括 iOS 26、iPadOS 26…...

Mac软件卸载指南,简单易懂!

刚和Adobe分手&#xff0c;它却总在Library里给你写"回忆录"&#xff1f;卸载的Final Cut Pro像电子幽灵般阴魂不散&#xff1f;总是会有残留文件&#xff0c;别慌&#xff01;这份Mac软件卸载指南&#xff0c;将用最硬核的方式教你"数字分手术"&#xff0…...

【服务器压力测试】本地PC电脑作为服务器运行时出现卡顿和资源紧张(Windows/Linux)

要让本地PC电脑作为服务器运行时出现卡顿和资源紧张的情况&#xff0c;可以通过以下几种方式模拟或触发&#xff1a; 1. 增加CPU负载 运行大量计算密集型任务&#xff0c;例如&#xff1a; 使用多线程循环执行复杂计算&#xff08;如数学运算、加密解密等&#xff09;。运行图…...

C++使用 new 来创建动态数组

问题&#xff1a; 不能使用变量定义数组大小 原因&#xff1a; 这是因为数组在内存中是连续存储的&#xff0c;编译器需要在编译阶段就确定数组的大小&#xff0c;以便正确地分配内存空间。如果允许使用变量来定义数组的大小&#xff0c;那么编译器就无法在编译时确定数组的大…...

Aspose.PDF 限制绕过方案:Java 字节码技术实战分享(仅供学习)

Aspose.PDF 限制绕过方案&#xff1a;Java 字节码技术实战分享&#xff08;仅供学习&#xff09; 一、Aspose.PDF 简介二、说明&#xff08;⚠️仅供学习与研究使用&#xff09;三、技术流程总览四、准备工作1. 下载 Jar 包2. Maven 项目依赖配置 五、字节码修改实现代码&#…...

人机融合智能 | “人智交互”跨学科新领域

本文系统地提出基于“以人为中心AI(HCAI)”理念的人-人工智能交互(人智交互)这一跨学科新领域及框架,定义人智交互领域的理念、基本理论和关键问题、方法、开发流程和参与团队等,阐述提出人智交互新领域的意义。然后,提出人智交互研究的三种新范式取向以及它们的意义。最后,总结…...

处理vxe-table 表尾数据是单独一个接口,表格tableData数据更新后,需要点击两下,表尾才是正确的

修改bug思路&#xff1a; 分别把 tabledata 和 表尾相关数据 console.log() 发现 更新数据先后顺序不对 settimeout延迟查询表格接口 ——测试可行 升级↑&#xff1a;async await 等接口返回后再开始下一个接口查询 ________________________________________________________…...

认识CMake并使用CMake构建自己的第一个项目

1.CMake的作用和优势 跨平台支持&#xff1a;CMake支持多种操作系统和编译器&#xff0c;使用同一份构建配置可以在不同的环境中使用 简化配置&#xff1a;通过CMakeLists.txt文件&#xff0c;用户可以定义项目结构、依赖项、编译选项等&#xff0c;无需手动编写复杂的构建脚本…...

Linux中《基础IO》详细介绍

目录 理解"文件"狭义理解广义理解文件操作的归类认知系统角度文件类别 回顾C文件接口打开文件写文件读文件稍作修改&#xff0c;实现简单cat命令 输出信息到显示器&#xff0c;你有哪些方法stdin & stdout & stderr打开文件的方式 系统⽂件I/O⼀种传递标志位…...

保姆级【快数学会Android端“动画“】+ 实现补间动画和逐帧动画!!!

目录 补间动画 1.创建资源文件夹 2.设置文件夹类型 3.创建.xml文件 4.样式设计 5.动画设置 6.动画的实现 内容拓展 7.在原基础上继续添加.xml文件 8.xml代码编写 (1)rotate_anim (2)scale_anim (3)translate_anim 9.MainActivity.java代码汇总 10.效果展示 逐帧…...