![]() |
Home | Libraries | People | FAQ | More |
這個庫中的所有的函數和分佈類型可以與滿足下面給出的要求的任何的RealType 一起使用。所有的內建的(built-in)浮點類型都滿足這些要求。用戶定義的數據類型如果滿足這些要求也可以與這個庫一起使用。例如,一個對NTL (RR) 進行一個 廋包裝類(thin wrapper class) 也可以使用。提交與其它的擴展精度類型進行綁定的代碼也是非常受歡迎的!
在這些指導原則背後的要求是RealType 的行為與一個內建的(built in)的浮點類型的行為一樣。
對於這個庫中的所有函數,這些要求是通用的。
在下面的表中,r 是一個 RealType類型的對象,cr 是 cr2 const
RealType類型的對象,且ca是一個 const arithmetic-type 類型的對象(算術類型包含所有內建的(built in)整型和浮點類型)。
|
表達式 |
結果類型 |
說明 |
|---|---|---|
|
|
RealType |
RealType可拷貝構造. |
|
|
RealType |
RealType 可以從算術類型對像進行拷貝構造。 |
|
|
RealType& |
賦值運算符。 |
|
|
RealType& |
可以從算術類型進行賦值的賦值運算符。 |
|
|
RealType& |
將 cr 加到 r。 |
|
|
RealType& |
將 ar 加到 r。 |
|
|
RealType& |
從 r 中減去cr 。 |
|
|
RealType& |
從 r 中減去ca 。 |
|
|
RealType& |
用 r 乘以 cr |
|
|
RealType& |
用 r 乘以 ca。 |
|
|
RealType& |
用 cr 除以 r 。 |
|
|
RealType& |
用 ca 除以 r。 |
|
|
RealType |
一元取負。 |
|
|
RealType& |
相等運算符。 |
|
|
RealType |
二元加法。 |
|
|
RealType |
二元加法。 |
|
|
RealType |
二元加法。 |
|
|
RealType |
二元減法。 |
|
|
RealType |
二元減法。 |
|
|
RealType |
二元減法。 |
|
|
RealType |
二元乘法。 |
|
|
RealType |
二元乘法。 |
|
|
RealType |
二元乘法。 |
|
|
RealType |
二元除法。 |
|
|
RealType |
二元除法。 |
|
|
RealType |
二元除法。 |
|
|
bool |
相等比較。 |
|
|
bool |
相等比較。 |
|
|
bool |
相等比較。 |
|
|
bool |
不相等比較。 |
|
|
bool |
不相等比較。 |
|
|
bool |
不相等比較。 |
|
|
bool |
小於等於。 |
|
|
bool |
小於等於。 |
|
|
bool |
小於等於。 |
|
|
bool |
大於等於。 |
|
|
bool |
大於等於。 |
|
|
bool |
大於等於。 |
|
|
bool |
小於比較。 |
|
|
bool |
小於比較。 |
|
|
bool |
小於比較。 |
|
|
bool |
大於比較。 |
|
|
bool |
大於比較。 |
|
|
bool |
大於比較。 |
|
|
int |
RealType中的有效數字的個數。 |
|
|
RealType |
RealType類型可以表示的最大的數。 |
|
|
RealType |
RealType類型可以表示的最小的數。 |
|
|
RealType |
RealType類型可以表示的最大的數的自然對數。 |
|
|
RealType |
RealType類型可以表示的最小的數的自然對數。 |
|
|
RealType |
RealType類型的機器精度(machine epsilon) |
注意:
log_max_value 和函數 log_min_value 可以由其它函數合成,因此沒有要求明確的特化(explicit specialisation)。epsilon 可以由其它的函數合成,所以,如果RealType的精度在運行時不會發生改變,那麼沒有要求明確的特化(explicit specialisation)(參考boost/math/bindings/rr.hpp
頭文件瞭解精度在運行時發生變化)。digits,
max_value 和 min_value,可以從std::numeric_limits自動合成。然而如果對類型RealType沒有特化函數numeric_limits ,那麼當代碼試圖使用這些函數的時候,你會得到一個編譯錯誤,除非你明確地特化化這個函數。例如,如果 RealType的精度在運行時發生變化,那麼對函數numeric_limits 的支持可能是不合適的。參考boost/math/bindings/rr.hpp
來查看一些例子。![]() |
提示 |
|---|---|
|
如果 Boost.Test: 給出的迷惑性報錯消息(misleading error messages)類似於 "difference between {9.79796} and {9.79796} exceeds 5.42101e-19%". Boost.LexicalCast 和 Boost.Serialization 在將數值轉換成字符串的時候會產生嚴重的精度損失。
雖然看起來應當要求 RealType 提供對 |
這個庫中的許多函數(雖然不是所有的)都調用了標準庫中的函數,下面的表總結了這些要求。注意,這個庫中的大多數函數只會調用這裡列舉的函數中的很少一部分,因此,如果對一個用戶定義的類型是否對標準庫有足夠的支持而可以使用存在疑慮,那麼最好的建議是進行嘗試並查看結果!
在下面的表中r 是一個 RealType類型的對象,cr1 和 cr2 是 const
RealType類型的對象,而i是 int型對象。
|
表達式 |
結果類型 |
|---|---|
|
|
RealType |
|
|
RealType |
|
|
RealType |
|
|
RealType |
|
|
RealType |
|
|
RealType |
|
|
RealType |
|
|
RealType |
|
|
RealType |
|
|
RealType |
|
|
RealType |
|
|
RealType |
|
|
RealType |
|
|
RealType |
|
|
RealType |
|
|
RealType |
|
|
RealType |
|
|
int |
|
|
RealType |
|
|
int |
注意,上面的表中只列舉了那些已知會被這個庫用到的標準庫函數(或者有可能在將來的庫的實現中用到)。下面的函數:acos,
atan2, fmod,
cosh, sinh,
tanh, log10,
lround, llround,
ltrunc, lltrunc 以及modf` 當前並沒有用到,但是如果將來增加新的特殊函數就可能用到。
注意,函數round, trunc 和 modf
並不是當前的C++標準的一部分:它們是C99標準的增加部分,並可能成為下一個C++標準的一部分。Boost庫實現的這些函數作為一個備份(backup),這些函數總是稱作未修飾的(unqualified),以便可以進行參數依賴性查找( argument-dependent-lookup) 。
除此之外,對於高效和高精度的結果,非常期望有一個蘭克澤斯逼近(Lanczos
approximation) 。 你或許可以從 boost/math/special_functions/lanczos.hpp
或libs/math/tools/ntl_rr_lanczos.hpp中修改一個已存在的逼近方法:你將需要修改 static_cast 為 lexical_cast ,並將常量改為字符串(為了確保係數不會被截斷為 long double) 並且對於T類型特化lanczos_traits
。否則你需要研究libs/math/tools/lanczos_generator.cpp
來為你的RealType查找一個合適的逼近方法。如果你沒有這樣做,代碼也可以編譯,但是對於任何的調用 gamma/beta/erf 系列函數的函數,精度和效率都會大打折扣。