Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Message-ID | <44dd28ba-7178-4877-a2e2-28bb24884f1b@googlegroups.com> (permalink) |
|---|---|
| Newsgroups | comp.std.c++ |
| From | john.salmon@googlemail.com |
| Subject | can generate_canonical return 1.0 exactly? |
| Organization | unknown |
| Date | 2014-05-30 15:33 -0600 |
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-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to comp.std.c++ | Previous | Next | Find similar
can generate_canonical return 1.0 exactly? john.salmon@googlemail.com - 2014-05-30 15:33 -0600
csiph-web