大数计算: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…...

使用docker在3台服务器上搭建基于redis 6.x的一主两从三台均是哨兵模式
一、环境及版本说明 如果服务器已经安装了docker,则忽略此步骤,如果没有安装,则可以按照一下方式安装: 1. 在线安装(有互联网环境): 请看我这篇文章 传送阵>> 点我查看 2. 离线安装(内网环境):请看我这篇文章 传送阵>> 点我查看 说明:假设每台服务器已…...
[特殊字符] 智能合约中的数据是如何在区块链中保持一致的?
🧠 智能合约中的数据是如何在区块链中保持一致的? 为什么所有区块链节点都能得出相同结果?合约调用这么复杂,状态真能保持一致吗?本篇带你从底层视角理解“状态一致性”的真相。 一、智能合约的数据存储在哪里…...

C++实现分布式网络通信框架RPC(3)--rpc调用端
目录 一、前言 二、UserServiceRpc_Stub 三、 CallMethod方法的重写 头文件 实现 四、rpc调用端的调用 实现 五、 google::protobuf::RpcController *controller 头文件 实现 六、总结 一、前言 在前边的文章中,我们已经大致实现了rpc服务端的各项功能代…...

stm32G473的flash模式是单bank还是双bank?
今天突然有人stm32G473的flash模式是单bank还是双bank?由于时间太久,我真忘记了。搜搜发现,还真有人和我一样。见下面的链接:https://shequ.stmicroelectronics.cn/forum.php?modviewthread&tid644563 根据STM32G4系列参考手…...
Cesium1.95中高性能加载1500个点
一、基本方式: 图标使用.png比.svg性能要好 <template><div id"cesiumContainer"></div><div class"toolbar"><button id"resetButton">重新生成点</button><span id"countDisplay&qu…...

CentOS下的分布式内存计算Spark环境部署
一、Spark 核心架构与应用场景 1.1 分布式计算引擎的核心优势 Spark 是基于内存的分布式计算框架,相比 MapReduce 具有以下核心优势: 内存计算:数据可常驻内存,迭代计算性能提升 10-100 倍(文档段落:3-79…...

[ICLR 2022]How Much Can CLIP Benefit Vision-and-Language Tasks?
论文网址:pdf 英文是纯手打的!论文原文的summarizing and paraphrasing。可能会出现难以避免的拼写错误和语法错误,若有发现欢迎评论指正!文章偏向于笔记,谨慎食用 目录 1. 心得 2. 论文逐段精读 2.1. Abstract 2…...

论文浅尝 | 基于判别指令微调生成式大语言模型的知识图谱补全方法(ISWC2024)
笔记整理:刘治强,浙江大学硕士生,研究方向为知识图谱表示学习,大语言模型 论文链接:http://arxiv.org/abs/2407.16127 发表会议:ISWC 2024 1. 动机 传统的知识图谱补全(KGC)模型通过…...

从零实现STL哈希容器:unordered_map/unordered_set封装详解
本篇文章是对C学习的STL哈希容器自主实现部分的学习分享 希望也能为你带来些帮助~ 那咱们废话不多说,直接开始吧! 一、源码结构分析 1. SGISTL30实现剖析 // hash_set核心结构 template <class Value, class HashFcn, ...> class hash_set {ty…...

SpringTask-03.入门案例
一.入门案例 启动类: package com.sky;import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCach…...