欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

【数学】Floating-Point Hazard

发布时间:2025/3/8 编程问答 41 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【数学】Floating-Point Hazard 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

Floating-Point Hazard

时间限制: 1 Sec 内存限制: 128 MB
提交: 106 解决: 42
[提交] [状态] [命题人:admin]

题目描述

Given the value of low, high you will have to find the value of the following expression:

If you try to find the value of the above expression in a straightforward way, the answer may be incorrect due to precision error.

输入

The input file contains at most 2000 lines of inputs. Each line contains two integers which denote the value of low, high (1 ≤ low ≤ high ≤ 2000000000 and high-low ≤ 10000). Input is terminated by a line containing two zeroes. This line should not be processed.

输出

For each line of input produce one line of output. This line should contain the value of the expression above in exponential format. The mantissa part should have one digit before the decimal point and be rounded to five digits after the decimal point. To be more specific the output should be of the form d.dddddE-ddd, here d means a decimal digit and E means power of 10. Look at the output for sample input for details. Your output should follow the same pattern as shown below.

样例输入

1 100
10000 20000
0 0

样例输出

3.83346E-015
5.60041E-015

题目大意:

输入两个整数lllrrr,求∑i=lr((i+10−15)3−i3)\sum_{i=l}^r(\sqrt[3]{(i+10^{-15})}-\sqrt[3]{i})i=lr(3(i+1015)3i)的值,并用d.dddddE-ddd的格式输出。

解题思路:

首先,我们可以先回顾一下求导的公式:f(x)′=f(x+Δx)−f(x)Δxf(x)\prime=\frac{f(x+\Delta x)-f(x)}{\Delta x}f(x)=Δxf(x+Δx)f(x),因此当f(x)=x3,Δx=10−15f(x)=\sqrt[3]{x},\Delta x=10^{-15}f(x)=3xΔx=1015时,f(x)′=x+10−153−x310−15f(x)\prime=\frac{\sqrt[3]{x+10^{-15}}-\sqrt[3]x}{10^{-15}}f(x)=10153x+10153x,因此∑i=lr((i+10−15)3−i3)=∑i=1rf(x)′∗10−15\sum_{i=l}^r(\sqrt[3]{(i+10^{-15})}-\sqrt[3]{i})=\sum_{i=1}^rf(x)\prime*10^{-15}i=lr(3(i+1015)3i)=i=1rf(x)1015
而此题主要就是因为根号里面的值太小,可能导致计算后为0,所以我们可以先算出∑i=1rf(x)′\sum_{i=1}^rf(x)\primei=1rf(x)的值,10−1510^{-15}1015可以直接先放在指数上,最后化简为所需要的形式即可。

代码:

#include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <map> #include <stack> #include <queue> #include <vector> #include <bitset> #include <set> #include <utility> #include <sstream> #include <iomanip> using namespace std; typedef long long ll; typedef unsigned long long ull; #define inf 0x3f3f3f3f #define rep(i,l,r) for(int i=l;i<=r;i++) #define lep(i,l,r) for(int i=l;i>=r;i--) #define ms(arr) memset(arr,0,sizeof(arr)) //priority_queue<int,vector<int> ,greater<int> >q; const int maxn = (int)1e5 + 5; const ll mod = 1e9+7; int main() {#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int l,r;while(scanf("%d %d",&l,&r)&&l+r) {double t=0;for(int i=l;i<=r;i++) {t+=(1.0/3.0)*(pow(double(i),-2.0/3.0));}double x=0;int y=-15;while(t>10.0) {t=t/10.0;y++;}x=t;while(t<1.0) {t=t*10.0;y--;}x=t;printf("%.5fE-%03d\n",x,(-y));}return 0; }

总结

以上是生活随笔为你收集整理的【数学】Floating-Point Hazard的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。