This question already has answers here:
Why is NaN not equal to NaN? [duplicate]
(6 answers)
Closed 8 years ago.
isNan is defined like this in iOS SDK, math.h as below,
#define isnan(x) \
( sizeof(x) == sizeof(float) ? __inline_isnanf((float)(x)) \
: sizeof(x) == sizeof(double) ? __inline_isnand((double)(x)) \
: __inline_isnanl((long double)(x)))
And the inline function goes like this,
__header_always_inline int __inline_isnanf(float __x) {
return __x != __x;
}
__header_always_inline is just to force the compiler to make the function surely inline.
What actually has been done in the inline function is pretty evasive to my eyes.
return __x != __x;
What does this line do? How does it verify if the argument is a NaN or not?
Edit:
The question here is NOT why NaN is not equal to NaN; but how its implemented. So please guide your answers towards the actual low level implementation.
Any help is greatly appreciated. Thanks.
CF http://en.wikipedia.org/wiki/NaN
A comparison with a NaN always returns an unordered result even when comparing with itself.
Mean a NaN is ALWAYS different from what you compare it too :)
I suppose the compiler/CPU has their own way to check for this special value, maybe someone can give a better answer than me about the implementation...
Related
I am currently implementing an optimization problem with pyomo and since now some hours I get the message that my problem is unbounded. After searching for the issue, I came along one term which seems to be unbounded. I excluded this term from the objective function and it shows that it takes a very high negative value, which supports the assumption that it is unbounded to -Inf.
But I have checked the problem further and it is impossible that the term is unbounded, as following code and results show:
model.nominal_cap_storage = Var(model.STORAGE, bounds=(0,None)) #lower bound is 0
#I assumed very high CAPEX for each storage (see print)
dict_capex_storage = {'battery': capex_battery_storage,
'co2': capex_co2_storage,
'hydrogen': capex_hydrogen_storage,
'heat': capex_heat_storage,
'syncrude': capex_syncrude_storage}
print(dict_capex_storage)
>>> {'battery': 100000000000000000, 'co2': 100000000000000000,
'hydrogen': 1000000000000000000, 'heat': 1000000000000000, 'syncrude': 10000000000000000000}
From these assumptions I already assume that it is impossible that the one term can be unbounded towards -Inf as the capacity has the lower bound of 0 and the CAPEX is a positive fixed value. But now it gets crazy. The following term is has the issue of being unbounded:
model.total_investment_storage = Var()
def total_investment_storage_rule(model):
return model.total_investment_storage == sum(model.nominal_cap_storage[storage] * dict_capex_storage[storage] \
for storage in model.STORAGE)
model.total_investment_storage_con = Constraint(rule=total_investment_storage_rule)
If I exclude the term from the objective function, I get following value after the optimization. It seems, that it can take high negative values.
>>>>
Variable total_investment_storage
-1004724108.3426505
So I checked the term regarding the component model.nominal_cap_storage to see the value of the capacity:
model.total_cap_storage = Var()
def total_cap_storage_rule(model):
return model.total_cap_storage == sum(model.nominal_cap_storage[storage] for storage in model.STORAGE)
model.total_cap_storage_con = Constraint(rule=total_cap_storage_rule)
>>>>
Variable total_cap_storage
0.0
I did the same for the dictionary, but made a mistake: I forgot to delete the model.nominal_cap_storage. But the result is confusing:
model.total_capex_storage = Var()
def total_capex_storage_rule(model):
return model.total_capex_storage == sum(model.nominal_cap_storage[storage] * dict_capex_storage[storage] \
for storage in model.STORAGE)
model.total_capex_storage_con = Constraint(rule=total_capex_storage_rule)
>>>>
Variable total_capex_storage
0.0
So my question is why is the term unbounded and how is it possible that model.total_investment_storage and model.total_capex_storage have different solutions though both are calculated equally? Any help is highly appreciated.
I think you are misinterpreting "unbounded." When the solver says the problem is unbounded, that means the objective function value is unbounded based on the variables and constraints in the problem. It has nothing to do with bounds on variables, unless one of those variable bounds prevents the objective from being unbound.
If you want help on above problem, you need to edit and post the full problem, with the objective function, and (if possible) the error. What you have now is a collection of different snippets of different variations of a problem, which isn't really informative on the overall issue.
I solved the problem by setting a lower bound to the term, which takes a negative value:
model.total_investment_storage = Var(bounds=(0, None)
I am still not sure why this term can take negative values but this solved at least my problem
This question already has answers here:
Is there a built-in way to compare two iterators?
(2 answers)
Closed 3 years ago.
I want to compare to iterators in a test case. Unfortunately, assert_eq! does not like to be passed iterators.
One workaround looks like this:
let mut a = 0..5;
let mut b = 0..6;
assert!(a.by_ref().zip(b.by_ref()).all(|(a, b)| a == b));
assert_eq!(a.next(), None);
assert_eq!(b.next(), None);
But it requires 3 (or at least) 2 lines for the test and the iterator needs to be made mutable. Is there anything better?
There is Iterator::eq. I think this should be the winner:
assert!(a.eq(b));
And it turns out there is itertools::assert_equal:
let a = 0..6;
let b = 0..6;
itertools::assert_equal(a, b);
And it's not exactly pretty but here is a solution using zip_longest:
assert!(a.zip_longest(b).all(|thingy| thingy.as_ref().left() == thingy.as_ref().right()));
This question already has answers here:
Does Objective-C use short-circuit evaluation?
(4 answers)
Closed 6 years ago.
I can't test this well but I was wondering if someone might know this. Having this simple check
if ([dict objectForKey:#"key"] != nil || [a isEqualToString:b] || someIntValue> 12) { ... }
Obviously (no matter the small margin) the first check will probably be heavier than the second, and the third even lighter.
So my question is; will the binary in my app still check the other 2 statements if the first was already true? Because if so I was thinking of doing a small round checks in my code-base to tweak checks like those above to move the simple int comparison earlier in that if-statement.
p.s.
I know you can't check unexisting objects in Objective-C like you can in PHP and I also only mean existing objects in this example, not to do something hacky that object(s) in the second statement won't exist:
$a = 0; $b = '';
if ($a == 0 || $i_dont_exist > 0) {
...
}
This is called short-circuit evaluation. Objective-C is a strict superset of C, since C supports short-circuit evaluation so does Objective-C.
So if the first condition in an "or" statement is true the rest of the conditions should not be evaluated.
This question already has answers here:
Comparing NSNumbers in Objective C
(5 answers)
Closed 9 years ago.
I have a method which parses HTML. In there there is an if/else statement:
if ((NSNumber1 == NSNumber2)) {
NSLog(#"dafuq1?");
} else {
NSLog(#"dafuq2?");
}
The log is sometimes like this:
...:dafuq1?
...:dafuq2?
So both parts are called. But other times just one of them gets called! Why?
Btw. iOS 7.0.4, Xcode 5.0.1
And
(NSNumber1 == NSNumber2) is true
These are objects. You can't use == to compare equality. Use isEqualToNumber:.
if ([NSNumber1 isEqualToNumber:NSNumber2])
This question already has answers here:
What are the details of "Objective-C Literals" mentioned in the Xcode 4.4 release notes?
(4 answers)
Closed 8 years ago.
Going through iTunes U Developing iOS 7 Apps for iPhone and iPad and in Lecture 3 slides, on page 120, there's a Quiz question that asks what the following line of code does. Frankly, I'm a bit stumped, and hoped someone could break it down.
cardA.contents = #[cardB.contents,cardC.contents][[cardB match:#[cardC]] ? 1 : 0];
So, I get the first part, cardA.contents = a new array with cardB.contents and cardC.contents in the array. But, next comes (I guess??) an index that returns either 1 or 0 depending if cardB matches an array that includes cardC.
Here's what I don't "get", and maybe it's just a syntax issue.... is what this does?
How is
cardA.contents = #[cardB.contents,cardC.contents][0];
or
cardA.contents = #[cardB.contents,cardC.contents][1];
Valid? Or, did I miss something?
Your understanding is completely correct; you're just missing one bit of syntax. Subscripted access of NSArrays with the array[index] form is part of the "collection literals" feature introduced by Clang a little while back.
It's transformed by the compiler into a call to [array objectAtIndexedSubscript:index].
As usual, in writing this out, it makes sense. It's literally saying, that if cardB matched cardC, use an index of [1] (cardC), if not, use an index of [0] (cardB)
So,
cardA.contents = cardB.contents // if does NOT match
cardA.contents = cardC.contents // if matches
(based on the index).
Duh.. Sorry for a silly question...