Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
7234 Discussions

Can I guarantee precision in casting floating point integers to int32/64s?

newton_particle
Beginner
725 Views

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

----------

0 Kudos
2 Replies
TimP
Honored Contributor III
725 Views
Quoting - newton_particle


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.

0 Kudos
newton_particle
Beginner
725 Views
Quoting - tim18

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
--------

0 Kudos
Reply