What does the > arithmetic operator in HLSL do? - operators

I found some code online for a shader containing these lines:
float2 preRect;
preRect.x = (IN.uv_MainTex.x > _RectMinX) - (IN.uv_MainTex.x > _RectMaxX);
In C based languages (IN.uv_MainTex.x > _RectMinX) would evaluate as a boolean but here it's assigning a numeric value to the float preRect.
What is this line doing & how is it doing it?
Thanks

The comment left above is incorrect.
return (a > b) - c;
is equivalent to:
return (a > b ? 1.0f : 0.0f) - c;

Related

How to compare float variables using strict inequality?

I'm trying to implement a maximize problem. In one part I am trying to compare a float to a dvar float for which I get the Error "The operator >(float, dvar float) is not available in the context of CPLEX." Using >= works, but I get wrong results. Is there any way to work around the error?
float price[D][A] = ...;
float volaforecast[D] = ...;
dvar float vola;
dvar int change[D][A];
maximize sum(d in D, a in A)(price[d][a] * change[d][a]);
subject to {
forall(d in D: d > 1) {
( volaforecast[d] <= vola &&
volaforecast[d-1] > vola &&
change[d]["a"] == 0
) || (
volaforecast[d] > vola &&
volaforecast[d-1] <= vola &&
change[d]["a"] == 1
);
}
}
As Tim mentioned in his comment, you should use var >= value + epsilon. Strict inequality doesn't fit the Linear Programming paradigm that CPLEX uses.

Using double interrogation mark sentences with three option in objective c syntax what is that meaning?

I downloaded a project in Github ,and see documents that have a expression I can't understand it.
Previous, I saw one question sentence with two option and understand expression,but the documents using two question sentence with three option in expression I can't understand it how it to operate.
{CGSize contentSize = CGSizeMake(direction == PRSlideViewDirectionHorizontal ? infiniteScrollingEnabled ? width * numberOfPages * 512 : width * numberOfPages : width,
direction == PRSlideViewDirectionVertical ? infiniteScrollingEnabled ? height * numberOfPages * 512 : height * numberOfPages : height);}
I guess you're confused by the a ? b ? c : d : e construct, aren't you? It's the so-called ternary operator used twice (very bad style). Let's break it down:
x ? y : z means: If x evaluates to true, use the expression y otherwise use z.
a ? b ? c : d : e parses as: a ? (b ? c : d) : e. So if a is true, then b is evaluated. If that is true, c is used, otherwise d. If a was false, e is used.
So, what can direction == PRSlideViewDirectionHorizontal ? infiniteScrollingEnabled ? width * numberOfPages * 512 : width * numberOfPages : width evaluate to?
direction == PRSlideViewDirectionHorizontal is true, infiniteScrollingEnabled is true: use width * numberOfPages * 512
direction == PRSlideViewDirectionHorizontal is true, infiniteScrollingEnabled is false: use width * numberOfPages
direction == PRSlideViewDirectionHorizontal is false: use width
Since this is hard to parse for humans I would avoid these constructs and go for a temporary variable and two simple if's instead.
CGFloat tmp;
if (direction == PRSlideViewDirectionHorizontal) {
if (infiniteScrollingEnabled) {
tmp = width * numberOfPages * 512;
} else {
tmp = width * numberOfPages;
}
} else {
tmp = width;
}

What is the Modulo function for negative decimals Objective-C?

I need to calculate modulos with decimals that can be negative as well
for example: fmod( -5.2, 3 );
while mod() works with integers, and fmod() (or fmodf()) works well with decimals, fmod() returns wrong results with negative decimals:
ex:
double modulo = fmod (5.2, 3);
NSLog (#"==> %f", modulo);
==> 2.2 // This is correct !!
double modulo = fmod (-5.2, 3);
NSLog (#"==> %f", modulo);
==> -2.2 // This is wrong !! should be 0.8
Is there another mod() in the library or should i write my own decimal negative mod function ?
something like :
if (res = fmod(x,m) < 0) {
res+=m;
}
Thx !
-2.2 is correct and is also -5.2 mod 3. The fmod function is a C function (and therefore also Objective C), so you can find more detail about it by typing man fmod into terminal. When doing fmod it will preserve the sign of the value that you are moding. So to get the mod you want, you will need to check the sign (of either the result, or the value you are passing in) and if it is negative you will need to add the modulo base, in this case 3.
This is the definition of the fmod function:
double
fmod(double x, double y);
Specifically, the functions return the value x-i*y, for some integer i such that, if y is non-zero, the result has the same sign as x and magnitude less than the magnitude of y.
from the OS X man page.
For your purposes, you can do something like this:
#include <math.h>
float f_mod(float a, float n) {
return a - n * floor(a / n);
}
Of course, be careful to check n>0.
f_mod(-5.2f, 2.0f) = 0.8
f_mod(5.2f, 2.0f) = 2.2
Thank you so i ended up writing a wrapper... What i was hopping i could avoid. This works great for me, and, in my opinion, represents the correct mathematical definition of the modulo (not the C implementation). I am sure this function can be optimized,but for clarity i leave it this way:
//--
//-- Modulo
//--
double calcModulo ( double x, double m) {
double res = INFINITY;
if (m==0)
return res ;
double posMod, negMod, posM, posX;
posM = m < 0 ? -m:m;
posX = x < 0 ? -x:x;
posMod = fmod (posX, posM);
negMod = fmod (-posX,posM) + posM;
// pick up the correct res
if ( x >= 0 ){
if (m > 0) {
res = posMod;
} else {
res = -negMod;
}
}else{
if (m > 0) {
res= negMod;
} else{
res= -posMod;
}
}
return res;
}

Is there alternative of the "<=>" operator from Perl in C/Objective-C?

I'm implementing some custom NSArray sort selectors and I was wondering whether there's anything like the <=> operator in C/Objective-C?
I have this:
if (self.count == otherObject.count) return 0;
return (self.count > otherObject.count)? 1 : -1;
and would love to have (as in Perl)
return self.count <=> otherObject.count;
Maybe the compare: method is what you are looking for? NSString, NSNumber etc implement it. All compare-like methods in Cocoa returns a NSComparisonResult:
enum {
NSOrderedAscending = -1,
NSOrderedSame,
NSOrderedDescending
};
typedef NSInteger NSComparisonResult;
So you can use the returned integer value directly. Assuming that count in your question is a NSNumber you can do:
return [self.count compare:otherObject.count];
If your case is limited to numbers and you want to use just an operator you can probably use good old minus. But be aware of integer overflow!:
return self.count - otherObject.count;
It's called the Spaceship Operator and it originated in Perl; besides Perl, only Ruby and Groovy have it.
Not in C, and probably not in Objective-C.
You could write a function easily enough, though it would be specific to a particular operand type:
int compare(int x, int y) {
return x < y ? -1 : (x > y);
}
Or you could write a macro, which could be applied to any type with <, ==, and > operators, but it would sometimes evaluate its arguments more than once:
#define COMPARE(x, y) ((x) < (y) ? -1 : ((x) > (y)))
(Note that both versions depend on the > operator yielding 0 for false, 1 for true.)

Rounding with significant digits

In Xcode /Objective-C for the iPhone.
I have a float with the value 0.00004876544. How would I get it to display to two decimal places after the first significant number?
For example, 0.00004876544 would read 0.000049.
I didn't run this through a compiler to double-check it, but here's the basic jist of the algorithm (converted from the answer to this question):
-(float) round:(float)num toSignificantFigures:(int)n {
if(num == 0) {
return 0;
}
double d = ceil(log10(num < 0 ? -num: num));
int power = n - (int) d;
double magnitude = pow(10, power);
long shifted = round(num*magnitude);
return shifted/magnitude;
}
The important thing to remember is that Objective-C is a superset of C, so anything that is valid in C is also valid in Objective-C. This method uses C functions defined in math.h.