| 网站首页 | 业界新闻 | 群组 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
共有 573 人关注过本帖
标题:不相符的 C++ 的程序答案
只看楼主 收藏
promach
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-7-21
  问题点数:0  回复次数:0   
不相符的 C++ 的程序答案
http://coliru.stacked-crooked.com/a/3efa0cbe9dc22d0c

为何不是 time_ps-(double)number_of_baud_clocks_passed *(double)BAUD_OUT_PERIOD = 1.01561×10^14 − 974979×104167000 = 362507000 ? 以下的C++程序却跑出 5.20734 X 10^7 的答案 ?

程序代码:
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cassert>
#include <cstdlib>
#include <cstdio>
#include <math.h>

using namespace std;

#define HALF_48MHz_PERIOD 10416.7
#define BAUD_OUT_PERIOD 104167000

double time_ps = 0;
unsigned int number_of_baud_clocks_passed = 0;
bool finished=false;

void update_clk(void) {

    time_ps = time_ps + HALF_48MHz_PERIOD/2;


    /*******************************************/

    time_ps = time_ps + HALF_48MHz_PERIOD;


    /*******************************************/

    time_ps = time_ps + HALF_48MHz_PERIOD/2;
}

int main()
{
    while(!finished){
        update_clk();
        number_of_baud_clocks_passed = (unsigned int)(time_ps/(double)BAUD_OUT_PERIOD);
      

        if ( (number_of_baud_clocks_passed == 974979) && ((time_ps-(double)number_of_baud_clocks_passed*(double)BAUD_OUT_PERIOD <= (double)BAUD_OUT_PERIOD/2+(double)HALF_48MHz_PERIOD) && (time_ps-(double)number_of_baud_clocks_passed*(double)BAUD_OUT_PERIOD >= (double)BAUD_OUT_PERIOD/2-(double)HALF_48MHz_PERIOD)) ) {
            cout << "number_of_baud_clocks_passed = " << number_of_baud_clocks_passed << "\ttime_ps = " << time_ps << endl;
           

            cout << "(double)number_of_baud_clocks_passed = " << (double)number_of_baud_clocks_passed << endl;
            cout << "(double)BAUD_OUT_PERIOD = " << (double)BAUD_OUT_PERIOD << endl;
           

            cout << "time_ps-(double)number_of_baud_clocks_passed*(double)BAUD_OUT_PERIOD = " << time_ps-(double)number_of_baud_clocks_passed*(double)BAUD_OUT_PERIOD << endl;
           

            cout << "(double)BAUD_OUT_PERIOD/2+(double)HALF_48MHz_PERIOD = " << (double)BAUD_OUT_PERIOD/2+(double)HALF_48MHz_PERIOD << endl;
            cout << "(double)BAUD_OUT_PERIOD/2-(double)HALF_48MHz_PERIOD = " << (double)BAUD_OUT_PERIOD/2-(double)HALF_48MHz_PERIOD << endl;
           

            finished=true;
        }
    }
}

2017-07-21 16:01







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.058078 second(s), 8 queries.
Copyright©2004-2018, BCCN.NET, All Rights Reserved