Given two numbers X and Y, how can I compute how many X's can fit into Y?
Good old division to the rescue!
float x = 16;
float y = 12345;
float howMany = ((float)y)/x; //> 771.5625
int floor = (int)howMany; //> 771
int ceil = (int)(howMany+0.5f);//> 772
Alliteratively, since you leave us guessing, you may want:
int lenX = "16".Length;
int lenY = "12345".Length;
float howMany = (float)lenY/lenX; //> 2.5
There are two ways your question could be interpreted.
(1) How many fractional X's can fit into Y?
In mathematics, the answer to this question is Y / X. In a programming language, if X and Y are integer values, you'll have to take care to cast them to floating point values before performing the division.
When X and Y are integers
int X = 5;
int Y = 17;
double N = (double)Y / (double)X;
// N is 3.4
When X and Y are real numbers
double X = 2.5;
double Y = 11.5;
double N = Y / X;
// N is 4.6
(2) How many whole X's can fit into Y?
In mathematics, the answer to this question is ⌊Y / X⌋, the floor of Y divided by X. In a programming language, if X and Y are integer values, the / operator applied to them will typically perform integer division. Integer division discards the remainder of division, so you don't have to call any floor function.
When X and Y are integers
int X = 5;
int Y = 17;
int N = Y / X;
// N is 3
When X and Y are real numbers
double X = 2.5;
double Y = 11.5;
int N = (int)Math.Floor(Y / X);
// N is 4
Related
I have an issue with one moving element on my webapp. There is a double yvalue which can be from -10 to 10. Then I have a double tope which can be from 0 to 500, which represents position from top of an absolute element.
Both values are relative to each other. So when y is increasing tope is decreasing. Problem is I cannot change initial y = -10 value, otherwise I would just make it from 1 to 20. So I made some manipulations:
double y = -10 to 10 // comes from function, increases, decreases randomly, but never jumps over numbers
double ynum = 25;
double divy = 250;
double tope = 500;
if(y<0)
{
y = y * -1; //Since y starts at -10, I convert it to 10
y = y + 10; // I add 10 to reverse, so 10+10 is 20, or 9+10 is 19 ...
tope = ynum * y; // 25x20 first initial tope value is 500px, then lets say 25x19.. dropping
}
else // at some point y reaches positive side and I need to decrease tope value further
{
tope = divy / y;
}
All of this works fine. y goes from -10 to 10 and tope goes from 500 to 0. Except when y reaches double values below zero -0.154.., -0.254.., 0.345 .. since division/multiplication from fractions are quite different.
I tried to force all fractions to be 1 or 0, but then the moving absolute element looses it's smoothness.
How could I solve this problem?
You want a linear function, that maps [-10, 10] to [500, 0], so
tope = a*y + b
substituting your conditions:
0 = 10a + b => b=-10a
500 = -10a + b
Plugin the first to the second:
500 = -10a + -10a => a = -500/20 = -25
b = -10a = 250
So you just need:
tope = -25y + 250
You can consider the steps required to take to the whole range of y to get to the target range of tope:
y : [10..-10]
tope: [0..500]
Subtract 10 to align the first value:
y - 10: [0..-20]
tope : [0..500]
Now scale to match the size of the last value:
500/20 = 25
(y - 10)*25: [0..-500]
tope : [0..500]
Finally negate:
-(y - 10)*25: [0..500]
tope : [0..500]
so
tope = -(y - 10)*25
This question might answer yours. You can map the ranges of values together.
This is copied from the accepted answer:
You can do it with an Extension Method (for decimal for example):
public static class ExtensionMethods
{
public static decimal Map (this decimal value, decimal fromSource, decimal
toSource, decimal fromTarget, decimal toTarget)
{
return (value - fromSource) / (toSource - fromSource) * (toTarget -
fromTarget) + fromTarget;
}
}
Then you can use it like:
decimal res = 2.Map(1, 3, 0, 10);
// res will be 5
Type double is signed.
Equation for a straight line is y = m*x + c where m is the gradient and c is constant.
Given your end points A:(500,-10) and B:(0,+10)
Plug the coordinates of B into the equation: 10 = m*0 + c
Solve: c = 10
Plug in A and c: -10 = m*500 + 10
Solve: m = -1/25 = -0.04
Formulae (that work for positive and negative x and y)...
y = f(x) = -0.04*x + 10; // y from x
x = f(y) = 25*(10-y); // x from y
I'm probably missing something really simple, but I'm trying to figure out how to calculate what's left over after I divide X by Y. I don't mean the remainder, I mean, e.g. if I divide 100 by 7 => 6 groups of 15 + one group of 10, how do I get 10?
I don't have code to show because I have no idea where start. X and Y are both integers.
It's not as simple as just using modulus. The fiddly bit is calculating your initial group size from the number of groups.
Try this:
int population = 100;
int numberOfGroups = 7;
int groupSize = (population + numberOfGroups - 1)/numberOfGroups;
Console.WriteLine(groupSize);
int remainder = population%groupSize;
Console.WriteLine(remainder);
I don't mean the remainder
Yes you mean remainder.
If you divide 100 by 15, you get 6 as quotient and 10 as remainder.
use Modulus operator like
int remainder = 100 % 15; // This will return 6
int quotient = 100/15; // This will return 10
It's a modulo operator, isntead of:
var result = x / y;
Try this:
var result = x % y;
EDIT.
Ok You are very unclear but i think one of below solutions is what You are trying to avhieve.
S1. do this:
int x = 100/15;
int z = 15 * x;
int y = 100 - z; // and You got Your 10
S2. do this:
int x = 100/7;
if ( x * 7 != 100)
{
int GroupSize = x+1;
int rest = 100 - GroupSize;
}
in my ASP.NET project i did a survey page that uses Application to save the votes. I have a problem with the making of the percentages amount. I've tried many things. here is the problematic part of my code:
double x = (count / sum) ;
double f = (count1 / sum) ;
double g = (count2 / sum) ;
double h = (count3 / sum) ;
if (sum > 0)
{
a = (int)x * 100;
b = (int)f * 100;
c = (int)g * 100;
d = (int)h * 100;
}
I used breakpoints and figured out that the problem was in the double variables: the (count/sum) equals 0 anyway.
I'm assuming count and sum are integer types.
The result of division of 2 integers is a truncated integer.
You need to cast one side of the division to a double, then the result will be double
So
((double)count)/sum
What are the datatypes of count, count[1-3] and sum? If they are integral types, then integer division is performed. This
int x = 100 ;
int y = 300 ;
double z = x / y ;
yields the value 0.0 for z.
Try something like
double h = (double) ( count3 / sum ) ;
You might also want to move your test for sum > 0 up: as coded, if sum is zero, you'll throw a DivideByZeroException before you get to your test, thus rendering your test moot.
Your count and sum variables are probably integers. Cast one of them to double:
double x = count / (double)sum;
UPDATE:
Actually, if you want the percentage as an integer, you can skip the doubles altogether:
int a = 100 * count / sum;
Trying to create a Mandelbrot set, I have been trying to using 8 and 15 digit floating point variables in my program, and have run into an issue: the double approximates to 0. I tried unit testing and wrote this code to isolate the issue. Using the variable viewer, the h and w values both were on 0.0, as opposed to 0.00185185185185 and 0.0015625, yet when I just write double h = 0.0015625, it works.
Many thanks for assistance.
int apparentwidth = 3;
int apparentheight = 2;
int height = 1080;
int width = 1920;
double w = (apparentwidth / width);
double h = (apparentheight / height);
Console.WriteLine(w);
Console.WriteLine(h);
You're dividing two int variables, and the result is an int. You're storing the result in a double, but the scale (portion after the decimal) has already been lost.
Use double throughout:
double apparentwidth = 3;
double apparentheight = 2;
double height = 1080;
double width = 1920;
double w = (apparentwidth / width);
double h = (apparentheight / height);
Or cast one of the variables to a double when dividing:
double w = ((double)apparentwidth / width);
double h = ((double)apparentheight / height);
You are doing integer division on accident.
double w = (double)apparentwidth / (double)width;
double h = (double)apparentheight / (double)height;
Just to provide further explanation to the other answers:
When you do mathematical operations on ints, the result is an int, and this is achieved by effectively truncating the non-integer portion:
3 / 2 == 1
When an operation is performed involving at least one double, the int input is first converted to a double, so the result is a double:
1.0 / 4 == 0.25
4 * 0.25 == 1
And of course, if both inputs are double the result is double and no implicit conversion occurs:
1.0 / 4.0 == 0.25
I can't get this to divide into a decimal. It is rounding to value 0.
private void button24_Click(object sender, EventArgs e)
{
double x = 0;
x = 1 / 2;
ans.Text = x.ToString();
}
When I debug, x is zero before it is sent to the textbox 'ans.'
I tried..and string variable is still zero..
double x = 1/5;
string displayX = x.ToString("0.0000");
It's integer division and those are the expected outputs.
double x = 1.0 / 5; // this will not perform integer division
double x = 1/5; // this does (1/5 = 0).
double x = 1D / 5; // this will not because 1 is treated as a double
You can do one of the follow:
double x = 1;
double y = 1.5;
double ans = x / y;
Replace double x = 1/5 with double x = 1.0/5 and that should fix it. Because both numbers you're dividing are integers, it still processes it as an integer, rather than as a double. When you think through logically, it makes some sense - it does the division in whatever form those numbers are and then saves it to the variable; the variable type is inconsequential to the actual equation.
(I realize there are other answers already, but hopefully this will help you see why the issue exists.)