Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

long primitive NaN value wanted

Scenario:

1) I have a long state variable that can either be set or not set.

2) This long variable has valid values from Long.MIN_VALUE to Long.MAX_VALUE including zero

3) This is performance sensitive code so I don't want to use a Long wrapper type

How do I write an 'is set' kind of check for this long? Do I really have to add a second boolean value to test whether the long is valid or not? This seems sloppy. I know I could use a Long wrapper here but this seems like a performance waste to be creating so many objects and checking for null.

Pseudo Code (this is sort of what I want):

class foo {

long someLong = NaN; //NaN = hypothetical not a number like Double

public reset() {
  someLong = NaN;
}

public doSomethingElse() {
   if(someLong !=NaN) {
     //report
     reset();
   }
}

public doSomeStuff() {
  if(someLong == NaN) {
   someLong = //something
  }
}

}

}
like image 225
Ryan R. Avatar asked Feb 20 '23 16:02

Ryan R.


2 Answers

You'd have to set aside a special value for NaN. If you really use all possible values, there is none.

Are you sure Long objects are that much of a performance problem?

If so, maybe have an extra boolean to denote if the value is set or not?

like image 50
Thilo Avatar answered Feb 28 '23 08:02

Thilo


Use a Long instead of a long, and use null as the NaN value.

like image 43
user207421 Avatar answered Feb 28 '23 06:02

user207421