- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If I have an integer value represented in single or double form what is the best method to cast this value to an int without loss of precision?
Case::Example
[cpp] float dis = 2.9999F;
int answer = (dis * 10.0000001);
std::cout << answer << std::endl;
// Output: 29[/cpp]
In this case, for whatever reason the float has lost some precision, or is unable to precisely represent the true integer value (3) so when a cast is made, the default action is to round down... hence the answer being 29 and not 30.
What I want to do is guarantee that the cast in the operation will return 30 and not 29.
What Im wondering is: if I used v?Round to first round the float/double to the nearest integer value, within the types range/representation, and then cast that value to an int, would I be guaranteed to always get the appropriate closest integer value? Or are there simply cases whereby even v?Round can only approximate an integer value, and the cast will always round down?
Newton
----------
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Case::Example
[cpp] float dis = 2.9999F;
int answer = (dis * 10.0000001);
std::cout << answer << std::endl;
// Output: 29[/cpp]
What I want to do is guarantee that the cast in the operation will return 30 and not 29.
Wouldn't lrint() and the like solve your problem? This looks more like a C++ than an MKL question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wouldn't lrint() and the like solve your problem? This looks more like a C++ than an MKL question.
Thanks for the reply,
Apologies, I was probably not very clear in specifically relating to the MKL:
When using the mkl vector rounding functions: ?Round, ?Floor et al. are there cases whereby the nearest integer representation (in single or double form) is not precisely represented, e.g. 0.9999999... == 1 on some machines. If this is the case (which Im not sure it is), would the nearest integer value be slightly over or under, by some error margin, the desired value? If the case could ever be slightly under, then casting to an integer would result in a rounding down (which is what I'm trying to determine; the manuals do not elaborate on the underlying method).
Yes, there are c++ ways of doing this, but they do not apply to the application of the vml methods im referring to, which are more scalable: fast iteration; multicore support; and also can operate in variable precision.
Newton
--------

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page