不相符的 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; } } }