- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
In my application I have the following code for the calculation of the number of rows/columns of a symmetric square matrix which is stored as upper triangle matrix
[cpp]#include#include int main() { int exclude_matrix[] = { 1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1, 1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1, 1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1, 1,0,0,1,1,1,1,1,1,1,1,1,1,1,1, 1,0,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1, 1,0,0,0,0,0,0,0,0,0,0,0, 1,0,0,0,0,0,0,0,0,0,0, 1,0,0,0,0,0,0,0,0,0, 1,0,0,0,0,0,0,0,0, 1,0,0,0,0,0,0,0, 1,0,0,0,0,0,0, 1,0,1,1,1,0, 1,1,1,1,1, 1,0,0,1, 1,0,1, 1,1, 1 }; double tmp = (sqrt(2.*sizeof(exclude_matrix)/sizeof(int)+.25)-.5); std::cout << tmp << std::endl; std::cout << static_cast (tmp) << std::endl; return 0; }
[/cpp]
This code is compiled with the Intel cpp-compiler, version 10.1.018 running on Intel 64.
Using -O0 or -O1 as optimization the right result (24) is obtained.
With -O2 and -O3 the result after casting the double value to an unsigned short value is 23 and not 24.
The value of tmp before casting is still 24.
Thanks.
Stefan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The static_cast is returning the integer portion of the number which may be one (or two) bits below 24.0withing the precision of the double.
Remove the -.5 from your expression evaluating tmp and you will receive the correct value in the integer portion of tmp.
Jim Dempsey
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The static_cast is returning the integer portion of the number which may be one (or two) bits below 24.0withing the precision of the double.
Remove the -.5 from your expression evaluating tmp and you will receive the correct value in the integer portion of tmp.
Jim Dempsey
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page