久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Matlab 與 C++ 雙精度

Matlab vs C++ Double Precision(Matlab 與 C++ 雙精度)
本文介紹了Matlab 與 C++ 雙精度的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在將一些代碼從 Matlab 移植到 C++.

I am porting some code from Matlab to C++.

在 Matlab 中

format long
D = 0.689655172413793 (this is 1.0 / 1.45)
E = 2600 / D
// I get E = 3.770000000000e+03

在 C++ 中

double D = 0.68965517241379315; //(this is 1.0 / 1.45)
double E = 2600 / D;
//I get E = 3769.9999999999995

這對我來說是個問題,因為在兩種情況下我都必須四舍五入到 0(Matlab 的修復),在第一種情況下(Matlab)變成 3770,而在第二種情況下(C++)變成 3769.

It is a problem for me because in both cases I have to do rounding down to 0 (Matlab's fix), and in the first case (Matlab) is becomes 3770, whereas in the second case (C++) it becomes 3769.

我意識到這是因為 C++ 案例中另外兩個最低有效數字15".鑒于 Matlab 似乎只能以雙精度存儲多達 15 個有效數字的精度(如上所示 - 0.689655172413793),我如何有效地告訴 C++ 忽略后面的15"?

I realise that it is because of the two additional least significant digits "15" in the C++ case. Given that Matlab seems to only store up to 15 significant digits of precision in double precision (as shown above - 0.689655172413793), how can I effectively tell C++ to ignore the "15" at the back?

所有計算均以雙精度完成.

All calculations are done in double precision.

推薦答案

您對 C++ 和 MATLAB 打印雙精度值的不同方式感到困惑.MATLAB 的 format long 只打印15 有效數字,而 C++ 打印 17 有效數字.在內部兩者使用相同的數字:IEEE 754 64 位浮點數.為了在 MATLAB 中重現 C++ 行為,我定義了一個 匿名函數 disp17 打印 17 位有效數字的數字:

You got confused by the different ways C++ and MATLAB are printing double values. MATLAB's format long only prints 15 significant digits while C++ prints 17 significant digits. Internally both use the same numbers: IEEE 754 64 bit floating point numbers. To reproduce the C++-behaviour in MATLAB, I defined a anonymous function disp17 which prints numbers with 17 significant digits:

>> disp17=@(x)(disp(num2str(x,17)))

disp17 = 

    @(x)(disp(num2str(x,17)))

>> 1.0 / 1.45

ans =

   0.689655172413793

>> disp17(1.0 / 1.45)
0.68965517241379315

您在 MATLAB 和 C++ 中看到的結果是相同的,它們只是打印不同數量的數字.如果你現在繼續使用相同的常量在兩種編程語言中,你會得到相同的結果.

You see the result in MATLAB and C++ is the same, they just print a different number of digits. If you now continue in both programming languages with the same constant, you get the same result.

>> D = 0.68965517241379315 %17 digits, enough to represent a double.

D =

   0.689655172413793

>> ans = 2600 / D %Result looks wrong

ans =

     3.770000000000000e+03

>> disp17(2600 / D) %But displaying 17 digits it is the same.
3769.9999999999995

打印 17 或 15 位數字的背景:

The background for printing 17 or 15 digits:

  • Adouble 需要在不丟失精度的情況下存儲 17 個有效數字,這是 C 打印的內容.
  • 對于最多 15 位數字,任何數字都可以從字符串轉換為雙精度字符串并返回原始數字,這正是 MATLAB 所做的.
  • A double requires 17 significant digits to be stored without precision loss, which is what C prints.
  • For up to 15 digits any number can be converted from string to double to string and results back in the original number, which is what MATLAB does.

這篇關于Matlab 與 C++ 雙精度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 亚洲精品电影网在线观看 | 国产一区二区在线免费视频 | 日韩影音 | 久久久久久国产精品免费免费男同 | 国产福利在线视频 | 久久国产精品视频 | 爱操av | 久久久久国产精品一区二区 | www.亚洲一区 | 黄色大片免费播放 | 午夜精品导航 | 亚洲一区二区在线视频 | 国产精品久久久久久久久动漫 | 美女黄18岁以下禁止观看 | 国产精品久久久久久亚洲调教 | 黄色一级免费 | 久久久久国产 | 黄色免费观看网站 | 久久国产成人 | 国产精品久久久久久久午夜 | 国产日韩欧美另类 | av男人的天堂在线 | 精品一区二区三区在线观看国产 | 福利社午夜影院 | 精品国产一二三区 | 国产精品国产精品国产专区不卡 | 国产精品区一区二区三区 | 特黄视频 | 久久久久久久久国产精品 | 免费国产视频在线观看 | 麻豆天堂 | 99精品视频一区二区三区 | 欧美日韩看片 | 啪啪免费网站 | 亚洲精品国产成人 | 国产精品一区二区在线免费观看 | 国产精品一区二区三区久久 | 亚洲欧美精品久久 | 亚洲欧美日韩电影 | 中文字幕在线一区 | 日韩高清国产一区在线 |