Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

can generate_canonical return 1.0 exactly?

56 views
Skip to first unread message

john....@googlemail.com

unread,
May 30, 2014, 4:40:02 PM5/30/14
to

The text in 26.5.7.2 [rand.util.canonical] says that 0 <= tj < 1. But
it seems to me that if arithmetic with RealType can round upwards,
then a result of 1.0 is possible. All that's required is for the
conversion of (gi - g.min()) to RealType to round up to R.

Here's a demonstration, using gcc-4.9.0. I expect it to hold on any
machine with IEEE floats.


drdlogin0039$ cat canbeone.cpp
#include <random>
#include <iostream>

int main(int argc, char **argv){
std::mt19937 e, f;
unsigned long long n = 60571531ull;

e.discard(n);
unsigned u = e();
std::cout << "after discard(" << n << ") engine returns " <<
std::hex << u << "\n";

f.discard(n);
std::cout << "calling std::generate_canonical<float, 32>:\n";
float v = std::generate_canonical<float, 32>(f);
if( v == 1.0f ){
std::cout << "v==1.0f exactly\n";
}else{
std::cout << "v=" << v << "\n";
}
return 0;
}
drdlogin0039$ dw -m gcc/4.9.0-28B/bin g++ canbeone.cpp -std=c++11 -Wall
drdlogin0039$ ./a.out
after discard(60571531) engine returns ffffffe1
calling std::generate_canonical<float, 32>:
v==1.0f exactly
drdlogin0039$


--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp...@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

0 new messages