2017-01-21 14 views
2

std::uniform_real_distributionには、組み込みのない浮動小数点のような型がいくつかあります。 half_float::halfまたはboost::multiprecision::float128。しかし、私が得るものは、g ++ 5.2からのホイールを改造することなく、カスタムタイプのために均一に分布するランダム実数を生成する方法は?

/opt/gcc-5.2/include/c++/5.2.0/bits/random.h:1868:7: error: static assertion failed: template argument not a floating point type

です。ここでは例のプログラムは、(g++ -std=c++11 -fext-numeric-literals test.cpp -o testでコンパイルする)です:

だから、
#include <random> 
#include <boost/multiprecision/float128.hpp> 
#include <half.hpp> 

template<typename Float> 
void test() 
{ 
    std::random_device rd; 
    std::mt19937 mt(rd()); 
    std::uniform_real_distribution<Float> rnd(Float(1),Float(10)); 
} 

int main() 
{ 
    test<float>(); 
    test<double>(); 
    test<long double>(); 
    test<boost::multiprecision::float128>(); // doesn't compile 
    test<half_float::half>(); // doesn't compile 
} 

、どのように1は、このようなカスタム型のために一様に分布する乱数の実数を生成することになっていますか?車輪を再発明せずに行く方法はありますか?

+0

1からnまで浮動し、お気に入りのフロートタイプを返す関数を定義することはどうですか?次に、デフォルトの引数として、入力としてランダムな浮動小数点数を持つ。 – lorro

+0

@lorro float型を組み込み型から必要な型に変換する関数を意味しますか?しかし、それは流通を統一させたままにしません。 – Ruslan

+0

私は、数学上の意味で「関数」を意味します:カスタムフロートの要素とN標準の浮動小数点の要素の間の1対1のマッピングです(浮動小数点が標準浮動小数点より小さい場合は逆もあります)。 intを使って説明する方が簡単です:擬似コード: 'int64_t getint64(int32_t a、int32_t b){return int64_t(a)<< 32 + b; } '。そのような関数は、(おそらく、最も重要なものからいくつかのビットを無視することによって)すべての型に対して作成することができます。 – lorro

答えて

0

規格で定義されているRealTypeタイプで作業標準の乱数施設、26.5.1.1.dのように、種類のテンプレート引数としてfloatdoubleまたはlong doubleを持っている必要があります。

Throughout this subclause 26.5, the effect of instantiating a template:

<...>

d) that has a template type parameter named RealType is undefined unless the corresponding template argument is cv-unqualified and is one of float , double , or long double .

一つの解決策はありますstd::uniform_real_distributionboost::random::uniform_real_distributionに置き換えてください。後者は、非組み込みフロート型を受け入れます。 half_float::half(float)コンストラクタは、明示的である一方で、

#include <random> 
#include <boost/random/uniform_real_distribution.hpp> 
#include <boost/multiprecision/float128.hpp> 

template<typename Float> 
void test() 
{ 
    std::random_device rd; 
    std::mt19937 mt(rd()); 
    boost::random::uniform_real_distribution<Float> rnd(Float(1),Float(10)); 
} 

int main() 
{ 
    test<float>(); 
    test<double>(); 
    test<long double>(); 
    test<boost::multiprecision::float128>(); 
} 

*half_float::halffloatに一定の成果によって、その乗算ので、動作しない、とブーストの実装はしていません。ここでは、合理的なタイプ*との例ですそれを明示的に呼び出す

0

あなたがhereから見ることができるように、uniform_real_distributionは次のように定義されます

RealTypeがある
template< class RealType = double > 
class uniform_real_distribution; 

The result type generated by the generator. The effect is undefined if this is not one of float, double, or long double.

あなたのコンパイラが明示的に解決策として、カスタム型を使用することを禁止しているようです対処してください。受け入れられないタイプ。したがって、私はあなたがそれを働かせる機会がないと言います。

+0

しかし、実際の問題は、何らかの良い選択肢、つまり、私が独自の実装を実装する代わりに使うことができるものがある場合です。 – Ruslan

+0

@Ruslan型として 'float'、' double'、 'long double'を使うことができます。それで全部です。あるいは、 'uniform_real_distribution'に代わるものを探していますか? – skypjack

+0

はい、私は他の浮動小数点型でも動作する代替の機構を探しています。 – Ruslan

関連する問題