0

I'm in a situation where I have a std::vector<double> but I want some of those doubles to be "nothing"/"non-existent". How is this done in C++? We can safely assume that all "normal" doubles are not negative for my purposes.

Should I let -1 (or some negative) denote "nothing"? That doesn't sound very elegant.

Should I create a Double class with a "nothing" bool member? That could work but seems rather lengthy and ugly.

Should I create a Double class and create a "NoDouble : public Double" subclass? That sounds even worse.

Any ideas would be appreciated.

6
  • 1
    std::vector<boost::optional<double>> is one option.
    – ildjarn
    Feb 19, 2013 at 23:47
  • @MooingDuck I just use "double" as one example. What if I had some other class like "Car"? Should I then create a NAN member in Car? Feb 19, 2013 at 23:51
  • 3
    Boost's optional class is a good way to get the same effect as creating a class with a bool member and it provides clean syntax. Feb 19, 2013 at 23:54
  • @DavidSchwartz Boost optional looks somewhat like a different (and not much easier) way of doing unique_ptr with nullptr (if "nothing") or not nullptr (if not "nothing"). correct my if i'm wrong. Feb 20, 2013 at 0:06
  • 2
    @user2015453: unique_ptr also is designed to use the heap, wheras boost::optional is just a value like double itself. Feb 20, 2013 at 0:58

3 Answers 3

5

If you have IEEE floating point arithmetic then use std::numeric_limits<double>::quiet_NaN() as value for "nothing". For checking if d is "nothing" use isnan(d). Also d != d is true only when d is NaN. Problem with NaN is that one may get it when doing defective calculations like dividing zero by zero or taking sqare root from negative number. Any calculations with NaN result also with NaN.

If you happen to use boost you may use boost::optional<double> that adds other level of not availability to side of NaN. Then you have two bad states: invalid number and missing number. Boost contains lot of useful libraries so it is worthy tool anyway.

If you need several possible reasons attached for why it is "nothing", then use special fallible class instead of double. Fallible was invented by Barnton and Nackman, the authors of the highly acclaimed "Scientific and Engineering C++" book.

You mentioned that there may not be negative numbers. On such case enwrap the double into class. What you have is not technically normal double so your class can add limitations to it.

5
  • 1
    Doesn't allow you to distinguish between non-existent results and results that should be there but were sabotaged by some problem during the calculations. Imho something like boost::optional is the only safe way to do this.
    – us2012
    Feb 20, 2013 at 0:03
  • This was the first idea that crossed my mind as well.
    – doug65536
    Feb 20, 2013 at 0:04
  • @us2012 when you want to have also the reason (from list) why the value is not available then the optional falls also short. On such cases you need "fallible". Look it up. Sad that boost does not contain one.
    – Öö Tiib
    Feb 20, 2013 at 0:07
  • 1
    But it falls a little bit less short than your solution. With optional, you can have 'nothings' as well as NaNs.
    – us2012
    Feb 20, 2013 at 0:08
  • @us2012 Ok, you are correct, i add them as alternatives to answer. :)
    – Öö Tiib
    Feb 20, 2013 at 0:10
1

You could use std::vector<double *>. A NULL pointer would indicate an empty slot or value.

4
  • 4
    This changes all the semantics of things like copies and destructors though. This is an "H-bomb to kill an ant" approach. Feb 19, 2013 at 23:56
  • @DavidSchwartz smart pointers could make it much simpler right? no need for any special copy/move/destrucs then? Feb 19, 2013 at 23:57
  • 3
    @user2015453: smart pointers would be better than raw pointers, but that's just a slightly smaller bomb Feb 19, 2013 at 23:59
  • 2
    There's no need for dynamic allocation at all. Boost.Optional is a perfectly serviceable, light-weight solution...
    – Kerrek SB
    Feb 20, 2013 at 0:17
1

What you want to do is to keep the vector the same, but also use a vector of bool, and then wrap that in a class.

Using the vector of bool on decent compiler should be optimized to 1 bit per boolean so that should get rid of your space problem.

class MyNullable {
public:
  double value;
  bool is_null;  
};

class NullableDoubles {
public:
  std::vector<double> values;
  std::vector<bool> nulls;
  void push_back(double d, bool is_null) {
    values.push_back(d);
    nulls.push_back(is_null);
  }
  MyNullable GetValue(int index) {
    MyNullable result;
    result.value = values[index];
    result.is_null = nulls[index];
    return result;
  }
  bool IsNull(int index) { return nulls[index]; }
  bool MakeNull(int index) { nulls[index] = false; }
};

And I am sure you can see the value(not pun intended) of wrapping that up in a template or two and then making nullable lists of anything.

template <class T>
class NullablesClass {
public:
  std::vector<T> values;
  std::vector<bool> nulls;
  void push_back(T d, bool is_null) {
    values.push_back(d);
    nulls.push_back(is_null);
  }
  MyNullable GetValue(int index) {
    MyNullableT<T> result;
    result.value = values[index];
    result.is_null = nulls[index];
    return result;
  }
  bool IsNull(int index) { return nulls[index]; }
  bool MakeNull(int index) { nulls[index] = false; }
  T GetValue(int index) { return values[index]; }
};

I hope that can do. Seems like the best way to be able to use all possible double values and also know if it is NULL while using the least memory and using the best alignment of memory. The vector is a specialization template in the C++ library so you should really only get 1 bit per bool for that.

4
  • Personally I'd stick with boost::optional<>, but +1 for a rare valid use of std::vector<bool>.
    – ildjarn
    Feb 20, 2013 at 0:34
  • For memory locality it is usually wise to keep closely related information in same class not split between several equal size containers.
    – Öö Tiib
    Feb 20, 2013 at 0:34
  • C# has Nullable<T> which is essentially a bool flag and a T. The same idea could work fine for C++. Alignment would probably cause 7 "wasted" bytes per item, but that's not a big deal unless the vector is huge.
    – doug65536
    Feb 20, 2013 at 0:37
  • @doug65536 : That's exactly what boost::optional<> is.
    – ildjarn
    Feb 20, 2013 at 0:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.