【数学】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: 【数学】Floating-Point Hazard_第1张图片

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

题目大意:

输入两个整数 l l l r r r,求 ∑ i = l r ( ( i + 1 0 − 15 ) 3 − i 3 ) \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 ) Δ x f(x)\prime=\frac{f(x+\Delta x)-f(x)}{\Delta x} f(x)=Δxf(x+Δx)f(x),因此当 f ( x ) = x 3 , Δ x = 1 0 − 15 f(x)=\sqrt[3]{x},\Delta x=10^{-15} f(x)=3x Δx=1015时, f ( x ) ′ = x + 1 0 − 15 3 − x 3 1 0 − 15 f(x)\prime=\frac{\sqrt[3]{x+10^{-15}}-\sqrt[3]x}{10^{-15}} f(x)=10153x+1015 3x ,因此 ∑ i = l r ( ( i + 1 0 − 15 ) 3 − i 3 ) = ∑ i = 1 r f ( x ) ′ ∗ 1 0 − 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 = 1 r f ( x ) ′ \sum_{i=1}^rf(x)\prime i=1rf(x)的值, 1 0 − 15 10^{-15} 1015可以直接先放在指数上,最后化简为所需要的形式即可。

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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 ,greater >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int main() 
{
    #ifndef ONLINE_JUDGE
    freopen("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;
}

你可能感兴趣的:(数学)