| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1254 人关注过本帖
标题:不相符的 C++ 的程序答案
只看楼主 加入收藏
promach
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-7-21
收藏
 问题点数:0 回复次数:0 
不相符的 C++ 的程序答案
http://coliru.

为何不是 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;
        }
    }
}

搜索更多相关主题的帖子: C++ 答案 double include cout 
2017-07-21 16:01
快速回复:不相符的 C++ 的程序答案
数据加载中...
 
   



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

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