Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Can you please explain what's going on in this code (how is it multiplying by 4 as said in comments in code?):
public static int GetNextSize(int i)
{
//multiply it by four and make sure it is positive
return i > 0 ? i << 2 : ~(i << 2) + 1;
}
Is there any better or cleaner way to do this? or is this one the optimum one?
Also, any practical situations where this (or this type of) code will be helpful?
Thanks.
The ? is the ternary operator, effectively a returnable if/else statement
if (i>0)
return i multiplied by four (bitshift to the left two)
else
return negative i multiplied by four
The ~x+1 means two's compliment and add one, effectively making it a negative number. The x here happens to be i<<2
It looks like some optimized C-like code to me.
For #2, are you referring to the logical OR operator?
a || b=c
Since a is evaluated first, the total expression will be true if a is true, so b=c is only evaluated is a is false. This effectively means if not a: b=c
if i is positive:
it will shift the bits by two to the left, which is effectively the same as multiplying by 4.
if i is not positive (is negative or zero), it will again multiply by 4, then negate all the bits (that's what ~ does) and add 1 (due to 2s complement -- it's necessary for positive numbers).
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to take a float value, with an arbitrary minimum and maximum possible value, and convert it to a linear scale, for representation on a bar-shaped indicator. The problem is, I can't just lerp it between the minimum and maximum, because the maximum value will always be dramatically higher than the minimum value. I have an array of arbitrary values that I want to act as intermediate points between the minimum and maximum. Now I just need to calculate a logical best-fit curve through the points. Each value is always larger than the last, and the rate of increase in value accelerates the further up you go, but there is no simple formula for calculating this rate of acceleration.
Here's an example of the values that may be used:
6.0, 13.5, 30.0, 75.0, 375.0
where 6 is the minimum, and 375 is the maximum.
If x is exactly one of these values, I would want a simple value depending on how many total values there are, I.E 0, 0.25, 0.5, 0.75, 1. The issue is calculating the in-between values.
How would I go about achieving this? I apologize if a question like this has already been asked, as it feels like a common problem, although I didn't know what to search for. If this has already been answered before, please just point me in the right direction.
Reposting my comment as an answer, as requested.
If a curve might be y(x) = k^(ax+b), take logs of both sides and you have a linear relation. As pointed out tho, this is maths not programming.
I’d pick k = 2, e or 10 for easier implementation; a & b you work out from data.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to compare two strings in a linq expression. Do I take advantage if I use `string.CompareOrdinal or is it the same?
list.Where(str1 => string.CompareOrdinal(str1, str2) == 0);
list.Where(str1 => str1 == str2);
According to benchmarks done by someone else, string.CompareOrdinal can be slightly faster than == when doing a lot of comparisons:
Most of the board remained green up through 10,000 comparisons and didn’t register any time.
At the 100,000 and 1,000,000 marks, things started to get a bit more interesting in terms of time differences.
String.CompareOrdinal was the constant superstar. What surprised me is for the case-insensitive comparisons, String.CompareOrdinal outperformed most other methods by a whole decimal place.
For case sensitive comparisons, most programmers can probably stick with the “==” operator.
-- The Curious Consultant: Fastest Way to Compare Strings in C# .Net
Note, though, that we are talking about a total difference of 3 milliseconds for 100,000 case-sensitive string comparisons, and that no measurable differences have been observed for 10,000 and 1,000,000 comparisons.
Thus, is very unlikely that this difference is relevant to your application (especially if you are using LINQ-to-objects), so the more readable == should be preferred.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Im working out a compression ratio and I noticed that I have a "Redundant Cast" in this line:
double ratio = (double)data.Length / (double)comp.Length;
Obviously dividing both array lengths without casting to double will return only the integer component of the number.
But why is this cast redundant?
Having just one cast on one Length removes the warning.
To my understanding if we had:
double ratio = (double)data.Length / comp.Length;
or
double ratio = data.Length / (double)comp.Length;
wouldnt that then potentially cause another developer to get confused about what value is being calculated here.
It seams to me that having an implicit cast can cause code readability issues. Especially in more complex one line calculations (which really is another readability question).
I also thought casting values to different forms produce different outputs depending on where the cast is made in the equation.
As a developer, I can say that
double ratio = (double)data.Length / comp.Length;
or
double ratio = data.Length / (double)comp.Length;
Will never confuse me. I recoginze this "pattern":
Division operation
One operand is casted to double
So, I understand, that second operand and result are double. And I (as developer) don't need second cast operator.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Given variables a and b, determine which one is the bigger one using only Boolean. Is that possible?
No Great than ">" or Less "<" than signs are allowed.
Is this even possible?
Some people didn't understand what I was asking, even though I saw a couple of smart and interesting solutions already, such as checking for the negative sign on a string... :-)
So, the question may be re-written in the following way...
given two numeric variables, determine which of them contains the bigger value without using < or > and using only boolean operators (AND, OR, XOR, NOT, etc.)
Thanks for those who answered...
If I understood the question, you could try using Min function
If Math.Min(a-b, 0) = 0 Then
' a >= b
Else
' a < b
Same theory as #Nizam, but I'm pretty sure Math.Min uses greater than and less than. You can use purely bitwise operators instead and test to see if the sign bit flipped.
C#
static bool GreaterThan(this int test, int other)
{
return ((test - other) & ~0x7FFFFFFF) == 0;
}
VB.NET
<Extension()>
Function GreaterThan(ByVal test As Integer, other As Integer) As Boolean
Return ((test - other) And Not &H7FFFFFFF) = 0
End Function
Unclear if it's "fuzzy" though.
#Nizam's solution will work and is the most elegant while #Frère Chloé's won't, and you risk a division by zero. Well, of course mine is awful ! but it works!
If InStr(Cstr(a - b), "-") Then
Debug.Print "b>a"
Else
Debug.Print "b<=a"
End If
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm unable to think through this one I think it's one of those moments where the answer is really simple but I'm too close to the problem to see the solution.
I have a distance that's changeable and an object that has to traverse this distance in the same time regardless of length.
The start of the distance is valued as 0 and the end of the distance is valued as 1.
Obviously the incrementation will be smaller the larger the length to keep the times equal.
What formula could I use to calculate the 0-1 incrementation but keep the time taken equal.
I know it seems overly complicated way to increment but it's part of the third party plugin I've been given.
I'm coding in C#.
Thanks.
[EDIT]
Sorry I wasn't very clear.
For incrementation the start point is always 0 and the end point is always 1.
So the object can move += 0.5 for example.
so when the length increase from say 30 to 65 it should take longer to increment from 0 to 1.
So you are looking for a way to have a number x in the range [0,1] that maps to some y in some arbitrary range [min,max], and are looking for the increment value a such that if x -> y then x + a -> y + b for some constant b? If I have understood your question correctly, then your a value should be:
a = b / (max - min) note: make sure to format this correctly for C#, especially be sure to cast and that sort of thing.
This is basically saying that a should be the fraction of the range that a spans, that if b is half the range from min to max, then a should be 0.5, and if b spans one fifth the range, a should be 0.2.