簡體   English   中英

System.Decimal是否一致?

[英]Is System.Decimal consistent?

使用浮點或雙精度浮點計算都可以給出不同的架構或優化設置不同的結果,所討論的硬件執行這里

在該線程上簡要提及但未得到完全答復的一件事是System.Decimal的情況。 有人提到System.Decimal 可能用作一致的浮點類型,它會非常慢,並且.NET(System.Math)提供的數學函數未針對System.Decimal實現。

我能找到的關於這種類型的唯一其他信息是Marc Gravell 說,它是在軟件中實現的,這強烈暗示了它確實是一致的。

因此,我的問題是:可以使用System.Decimal在所有硬件平台上獲得一致的結果,還是容易出現與System.Single和System.Double相同的問題?

如果System.Decimal是一致的,那么是否有任何免費的庫為其實現超越功能(cos,sin,tan,sqrt,log等)?

是一致的,它將在所有.NET實現上產生相同的結果。

以始終代表正確操作編號的方式保持一致。 任何浮點都有代表某些價值的麻煩。 System.Decimal只能准確地表示那些精確地表示為十進制值的數字,例如1/3將不正確地表示為0.33333 ......並且cos,sin,tan,sqrt和log的許多結果將是由於存在任何數字系統(例如十進制或二進制)的許多值只能表示為最接近的近似值,因此近似值。

C#規范說: The result of an operation on values of type decimal is that which would result from calculating an exact result (preserving scale, as defined for each operator) and then rounding to fit the representation. Results are rounded to the nearest representable value, and, when a result is equally close to two representable values, to the value that has an even number in the least significant digit position. The result of an operation on values of type decimal is that which would result from calculating an exact result (preserving scale, as defined for each operator) and then rounding to fit the representation. Results are rounded to the nearest representable value, and, when a result is equally close to two representable values, to the value that has an even number in the least significant digit position.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM