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.
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I know at a first glance, this will get a duplicate mention. And it very well might be, but I imagine most people are going to think that the answer is similar to this question: Division returns zero
That is not my case. That case, is dividing a smaller number by a larger number integers and getting 0. That makes sense. What's happening for me does not make sense.
Here's my code:
decimal correctedImageWidth = screenWidth / maxColumnsWithMaxSizeImages;
The value for screenwidth, which is an int, is 1024. The value for maxColumnsWithMaxSizeImages, also an int, is 3.
Somehow, correctedImageWidth becomes 0. Which is odd, because 1024/3 does not equal 0, nor would the rounded off number be 0, like the other SO questions had. You'd think I'd get something like 341. Here's proof that the numbers are what I say they are:
As you can see in my watch. screenWidth is 1024, maxColumnsWithMaxSizeImages is 3. However, dividing these 2 into correctedImageWidth is 0? Is there any reason why I would get 0 from this? I have shown this to colleagues, who are equally as confused. Here's my entire code, perhaps there's something I'm doing? Unlikely, seeing as they're both ints and they both have valid integer values. But obviously there must be something I'm doing? Here's the code:
int maxColumnsWithMaxSizeImages = (int)System.Math.Ceiling(decimal.Divide(1024, 480));
if (maxColumnsWithMaxSizeImages < _minimumImagesPerRow)
{
.....
} else if (maxColumnsWithMaxSizeImages > _maximumImagesPerRow)
{
....
} else
{
//between 2 and 4 columns
var screenWidth = App.ScreenWidth;
decimal correctedImageWidth = (decimal)((decimal)screenWidth / (decimal)maxColumnsWithMaxSizeImages);
decimal test2 = 1024 / 3;
decimal test3 = (decimal)1024 / (decimal)3;
var test = correctedImageWidth;
}
UPDATE
For some reason, it appears that there was a conflict in my variable declarations. Even though I declared them both in different scopes, for whatever reason it was stirring a conflict. After renaming the variable, I get the corrected value.
You've defined the field correctedImageWidth twice, once as int (in the if block) and once as decimal (in the else block).
The screenshot of your Watch window shows the int typed field value, which shows 0 at this point (wasn't assigned since your in the else block). Try watching the decimal typed field, or just hovering your mouse over the other decimal typed field while debugging, it should show you the correct value.
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).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Recently I found an issue with C# Math.Round that makes me confused:
Math.Round(1.125, 2) == 1.12
However:
Math.Round(1.115, 2) == 1.12
Also
Math.Round(1.225, 2) == 1.23
And
Math.Round(1.215, 2) == 1.22
The last three samples are make sense just want to know how does it work for the first one?
Regarding to the document, if round to nearest even number then why in third sample round to Odd number?
Is anyone has any idea how is rounding the digits?
Updating
As I put the link, I know try to round to nearest even number, I want to know how implementing it (Using shift bytes)?
In the last three examples, it's working as documented. In the first case though, it appears to go wrong:
Math.Round(1.125, 2) == 1.12 // Would expect 1.13
The reason is simple: floats are stored as binary values; not decimal. So it's rounding the binary value that 1.125 has been converted to. In this case it is slightly below 1.125 and thus rounds "the wrong way".
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.