0

I have a complete design of my developing software in C++. I really do not want to change the structure.

However, I sometimes get erroneous outputs to store in integer variable. Output is not any number, output is NaN. But I do not want to add any other variable to check whether my integer variable is erroneous or not.

Is there any way to store a thing such as NaN in an integer variable?

totten
  • 2,769
  • 3
  • 27
  • 41
  • Similar question: http://stackoverflow.com/questions/3949457/can-an-integer-be-nan-in-c basically seems to be no. – Shafik Yaghmour Jun 12 '13 at 13:54
  • 1
    You want something like `std::optional` without using it ? There no Nan with integers, so either you can afford the luxury do dedicate a special value for this purpose, or you need to change your interface. – Matthieu Rouget Jun 12 '13 at 13:55
  • @ShafikYaghmour thank you, I did search but got nothing. I submit close request. – totten Jun 12 '13 at 13:56
  • 2
    The only reasonable solution is to use an integer value that isn't otherwise used - `MIN_INT` or `MAX_INT` may be good candidates - or -999999999, 0xDEADBEEF or some other value that is not "a reasonable value for your application" (presumably, since you are using integer values to store floating point results, you are not using the entire integer range?) – Mats Petersson Jun 12 '13 at 13:56
  • @MatsPetersson, is it safe enough? – totten Jun 12 '13 at 13:57
  • You have to check this special value by yourself, everywhere you may use it. Thus, safety of this solution depends on your own code (or on the code that use you function). – Matthieu Rouget Jun 12 '13 at 14:00
  • Safe enough for what? I guess if your floating point calculation can result in "any value", then no - although if it's `float` rather than `double`, you could technically find a value that can't be represented in a `float`. If the results are `double`, not only do you have the problem that all integer values can be represented as integers in a `double`, but you also have integer values that can be WAY outside your integer value (and are precise). – Mats Petersson Jun 12 '13 at 14:01
  • Basically, even though you do not change the signature of your function, you change the specification (i.e. behavior of it). It is how your propagate this specification change through the whole application that may be unsafe. – Matthieu Rouget Jun 12 '13 at 14:01
  • How about throwing an exception instead? Or would that change the structure? – aggsol Jun 12 '13 at 14:06

2 Answers2

2

It's not magic, it's information theory basics. int is something that stores values in range [INT_MIN, INT_MAX]. That is all it can do, no less no more.

You constrain to use just the int, leaving you the only option to use some value as your indicator. If that is not good enough, you must reconsider the constraint.

Balog Pal
  • 16,195
  • 2
  • 23
  • 37
0

No, there is no value that you can store in an integral type which can represent a NaN.

If you need to store this value, you are going to have to reconsider your design. This doesn't necesarrily mean adding a new variable, but you might change an existing one. For example, the int variable where you currently store this value which can be NaN could be changed to something like boost::optional <int>. That way, it could be unset if the value was NaN, or set otherwise.

John Dibling
  • 99,718
  • 31
  • 186
  • 324