c ++数学赋值 [英] c++ math assignment
问题描述
分配是写一个计算机程序,将自身加大1/3次,并将结果与1/3乘以1/3加到自身的次数进行比较。它也是做同样的事情与½。程序是做这个算术两次,一次使用单精度(浮动)和一次使用双精度(双)。这两个都将在一个程序。确保你使用类型为您的柜台与这些大数字工作。
您的程序将执行这些添加109(10亿)次。
The assignment is to write a computer program that will add 1/3 to itself a large number of times and to compare the result to multiplying 1/3 by the number of times 1/3 was added to itself. It is also to do the same thing with ½.The program is to do this arithmetic twice, once using single precision (float) and once using double precision (double). Both of these will be in one program. Make certain you use a type for your counter that works with these large numbers. Your program will do these additions 109 (1 billion) times.
#include<iostream>
#include<conio.h>
#include<math.h>
#include <limits>
using namespace std;
typedef std::numeric_limits< double > dbl;
int main()
{
long size=1000000000;
int count=0;
long N=10;
float nAdd=1;
float nMul=1;
cout.precision(dbl::digits10);
cout<<"Iterration #\t\tAdd\t\t\tMul"<<endl;
for(long i=1; i<=size; i++)
{
nAdd+=1.0/3.0;
nMul*=1.0/3.0;
count++;
if(count%N==0 && count!=0)
{
N*=10;
cout<<i<<"\t\t"<<fixed <<nAdd<<"\t\t"<<fixed <<nMul<<endl;
}
if(count==size)
{
cout<<"Difference : "<<fixed <<nAdd<<" - "<<fixed <<nMul<<" = "<<fixed <<nAdd-nMul<<endl;
}
}
getch();
return 0;
}
所以我已经这样做了
i不正确
我必须使用的数字将乘以1/3或1/3将被添加到
so for i have done this i don't get it properly what number i have to use which will be multiply by 1/3 or 1/3 will be added into it
你可以解释我这个lil
非常感谢
can you guyz explain me this a lil thanks alot
推荐答案
大量添加1/3本身
add 1/3 to itself a large number of times
这意味着您应该添加1/3次:1/3 + 1/3 + 1/3 + 1/3 + ...
This means that you should add ⅓ a bunch of times: ⅓ + ⅓ + ⅓ + ⅓ + ...
然后你应该比较计算的结果和1/3乘以你添加的次数的结果。例如,如果您将它们加在一起四次(1/3 + 1/3 + 1/3 + 1/3),那么您将该结果与1/3×4的结果进行比较。
Then you're supposed to compare the result of that calculation with the result of multiplying ⅓ by the number of times you added it. So for example if you add it together four times (⅓ + ⅓ + ⅓ + ⅓) then you compare that result with the result of ⅓ × 4.
应该是相同的,但这个任务的目的是教你如何计算机执行计算。
Mathematically the results should be the same, but the purpose of this assignment is to teach you something about how the computer performs the calculation.
这篇关于c ++数学赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!