4

My program requires several floats to be set to a default number when the program launches. As the program runs these integers will be set to their true values. These true values however can be any real number. My program will be consistently be checking these numbers to see if their value has been changed from the default.

For example lets say I have integers A,B,C. All these integers will be set to a default value at the start (lets say -1). Then as the program progresses, lets say A and B are set to 3 and 2 respectfully. Since C is still at the default value, the program can conclude than C hasn't been assigned a non-default value yet.

The problem arises when trying to find a unique default value. Since the values of the numbers can be set to anything, if the value its set to is identical to the default value, my program won't know if a float still has the default value or its true value is just identical to the default value.

I considered NULL as a default value, but NULL is equal to 0 in C++, leading to the same problem!

I could create a whole object consisting of an bool and a float as members, where the bool indicates whether the float has been assigned its own value yet or not. This however seems like an overkill. Is there a default value I can set my floats to such that the value isn't identical to any other value? (Examples include infinity or i)

I am asking for C/C++ solutions.

4

8 に答える 8

7

I could create a whole object consisting of an bool and a integer as members, where the bool indicates whether the number has been assigned its own value yet or not. This however seems like an overkill.

What you described is called a "nullable type" in .NET. A C++ implementation is boost::optional:

boost::optional<int> A;

if (A)
  do_something(*A);
于 2012-06-20T20:20:32.040 に答える
5

On a two's complement machine there's an integer value that is less useful than the others: INT_MIN. You can't make a valid positive value by negating it. Since it's the least useful value in the integer range, it makes a good choice for a marker value. It also has an easily recognizable hex value, 0x80000000.

于 2012-06-20T20:43:38.873 に答える
2

There is no bit pattern you can assign to an int that isn't an actual int. You need to keep separate flags if you really have no integer values that are out of bounds.

于 2012-06-20T20:18:57.033 に答える
1

If the domain of valid int values is unlimited, the only choice is a management bit indicating whether it is assigned or not.

But, are you sure MAX_INT is a desired choice?

于 2012-06-20T20:19:19.210 に答える
1

There is no way to guarantee that a value you assign an int to is not going to be equal to another random int. The only way to assure that what you want to happen occurs, is to create a separate bool to account for changes.

于 2012-06-20T20:20:25.050 に答える
1

No, you will have to create your own data type which contains the information about whether it has been assigned or not.

于 2012-06-20T20:20:29.873 に答える
0

boolと整数をメンバーとして含むオブジェクト全体を作成できます。ここで、boolは、数値に独自の値がまだ割り当てられているかどうかを示します。しかし、これはやり過ぎのようです。

私の最初の推測は、フラグを効果的に使用し、各変数にマークを付けることです。しかし、もちろんこれがあなたの唯一の選択ではありません。

  1. ポインタ(NULLの場合もあります)を使用して、メモリを動的に割り当てることができます。あまり便利ではありません。
  2. ほとんど使用されないカスタム値を選択できます。次に、この値をデフォルト値として定義できます。多くの場合、この値をフロートに割り当てる必要がありますが、このケースは頻繁には発生せず、この変数を追跡する必要があります。このようなケースが発生した場合は、単純なリンクリストで十分です。
于 2012-06-20T20:29:46.717 に答える
0

If as you say, no integer value is off limits, then you cannot assign a default "uninitialised" value. Just use a struct with an int and a bool as you suggest in your question.

于 2012-06-20T20:22:21.070 に答える