Reputation: 593
Fairly simple question, but I know I'm going to have to clarify - I am NOT talking about doing operations of any kind.
I have some currency values and other decimal values that need to be handled. Is it safe to store them as floats, and when any sort of operation needs to be done, convert them to integers, and then back to floats (assume I can handle the decimal places). I understand precision errors when doing math with floats, but is there anything explicitly dangerous about simply storing them for ease of use?
Upvotes: 1
Views: 563
Reputation: 41474
Any integer between -2^24 and 2^24 can be converted to and from a single-precision floating point number without error; for double-precision, it's -2^54 to 2^54. For that matter, you can do floating-point operations on them without introducing any error, as long as the results are guaranteed to be integers in the same range.
Upvotes: 1