I have a few values
Quantity := 5;
Quantity2 := 8;
percent :=50;
so i want
Percent of Quantity + Quantity 2
which would be like : 50% of 13 = 6.5
I done it like this
HowMuchDamage := trunc(percent*(Quantity + Quantity2)/100);
How can i make it round up?
How can i make it round down?
Floor(X) returns the highest integer less than or equal to X.
Ceil(X) returns the lowest integer greater than or equal to X.
Related
I would like to encode three keyboard modifiers (CTRL, ALT, SHIFT) + the ASCII code of the pressed key into a single value. This falls naturally into the category of bitmasks.
One way I could do this is that the sender encodes each key as the following:
CTRL: 1000
ALT: 10000
SHIFT: 100000
KeyCode: 1-255
For example, if I were to click all modifiers + the last key in the ascii table, I would get:
100000 + 10000 + 1000 + 255 = 111255. The receiver side it would then be possible to do substraction and check if the number goes below 0 as such:
has_shift = X - 100000 < 0
has_alt = X - 10000 < 0
has_ctrl = X - 1000 < 0
if has_shift
X -= 100000
if has_alt
X -= 10000
if has_ctrl
X -= 1000
keyCode = X (the remainder)
Surely enough, I find this horrible and would assume that this could be done in a far better using bit-shift or something in that ballpark. How could this possibly be done better?
Instead add 256, 512, and 1024 respectively for ctrl, alt, shift. Then use the and operator in whatever language you're using (missing from question tags) to extract the modifiers and code. In C and many languages, that operator is &. So X & 1024 is not zero if shift was pressed. X & 255 is the character code.
I am making a Pong game in Delphi.
Paddle.Left := X - Paddle.Width div 2;
Paddle.Top := ClientHeight - Paddle.Height - 2;
I expect the output of 5/2 to be 2.5, but the actual output is 2.
The div operator performs integer division (5 div 2 = 2, throwing away the decimal .5), whereas the / operator performs floating point division (5 / 2 = 2.5).
In VCL, a control's Left, Top, Width, and Height values are expressed using whole integers, not floating point numbers.
In FMX, a control's Position and Size values are expressed using floating point numbers.
div is the integer division operator. It is a binary operator that takes two integers, and returns an integer, the truncated value of the division. For instance,
0 div 3 = 0
1 div 3 = 0
2 div 3 = 0
3 div 3 = 1
4 div 3 = 1
5 div 3 = 1
6 div 3 = 2
...
If you want to perform a floating-point division, you need to use the / operator:
0 / 3 = 0
1 / 3 = 0.33333333333333
2 / 3 = 0.66666666666666
3 / 3 = 1
4 / 3 = 1.33333333333333
5 / 3 = 1.66666666666666
6 / 3 = 2
...
Of course, the result cannot be stored in an integer variable. If you eventually need an integer value to specify a pixel on the screen, you need to round the floating-point value to an integer (using the Round function).
How we can generate randomize number between a range in the Float numbers (in delphi xe3) ?
For example, randomize number between [0.10 to 0.90].
I need give results like:
[ 0.20 , 0.32 , 0.10 , 0.50 ]
Thanks for solutions....
Another option is to use RandomRange (returns: AFrom <= r < ATo) as follow:
RandomRange(10, 90 + 1) / 100
or
RandomRange(10, 90 + 1) * 0.01
will return numbers in the range of 0.10 to 0.90 (including 0.90)
var
float : Double;
float := Random; // Random float in range: 0 <= float < 1
float := 0.1 + float*0.8 // 0.1 <= float < 0.9
To initialize the Random number generator, make a single call to Randomizeor set the RandSeed parameter before calling the Random function for the first time.
Not doing so, generates the same sequence every time you run the program. Note however, that this sequence is not guaranteed when recompiling for another compiler version.
Try this:
function RandomRangeF(min, max: single): single;
begin
result := min + Random * (max - min);
end;
This is a bit cheeky but here goes: Depends how many numbers you want after the floating point. For example, if you want 1 number, you could generate in the 100 - 999 range and then divide by 10. Or 1000 - 9999 and divide by 100.
Hi there =) And sorry for my English, in advance
I have a task to calculate hurst exponent by method of linear regression. And I have text description of solution. It looks very easy, but always i get values, that go out from range 0..1. Usually, value is 1.9 or something similar. Sometimes it gets negative value that is close to zero.
I have looked over code about thousand times but couldn't see a mistake.
var
max_z,min_z,x_m:real; //max and min of cumulative sum and mean value of X for every Tau
st,ss,sst,st2 :real;
Al, Herst: real;
x_vr:array of double; //a piece of array with length=tau
i, j, nach: integer;
begin
//file opening and getting values of X array are in another function
nach:=3; //initial value of tau
Setlength(ln_rs,l-nach); //length of ln(R/S) array
Setlength(ln_t,l-nach); //length of ln(tau) array
Setlength(r,l-nach); //length of R array
Setlength(s,l-nach); //length of S array
//Let's start
for tau:=nach to l do //we will change tau
begin
Setlength(x_vr,tau+1); //set new local array (length=tau)
for i:=0 to length(x_vr)-1 do
x_vr[i]:=x[i];
x_m:=Mean(x_vr); //mean value
Setlength(y,tau+1); //length of array of difference from mean value
Setlength(z,tau+1); //length of array of cumulative sum
for i:=0 to tau do
y[i]:=x_vr[i]-x_m; //difference from mean value
z[0]:=y[0];
for i:=1 to tau do //cumulative sum
for j :=i downto 0 do
z[i]:=z[i]+y[j];
max_z:=z[0];
for i:=1 to tau do //max of cumulative sum
max_z:=max(max_z,z[i]);
min_z:=z[0];
for i:=1 to tau do //min of cumulative sum
min_z:=min(min_z,z[i]);
r[tau-nach]:=max_z-min_z; //R value
s[tau-nach]:=0;
for i:=0 to tau do
s[tau-nach]:=power(y[i],2)+s[tau-nach]; //S value
s[tau-nach]:=sqrt(s[tau-nach]/(tau+1));
//new array values
ln_rs[tau-nach]:=Ln(R[tau-nach]/S[tau-nach]); // ln(R/S)
ln_t[tau-nach]:=ln(tau); // ln (tau)
end; //End of calculating
//Method of Least squares
for i:=0 to length(ln_rs)-1 do
st:=st+ln_t[i];
st:=(1/length(ln_rs))*st;
for i:=0 to length(ln_rs)-1 do
ss:=ss+ln_rs[i];
ss:=(1/length(ln_rs))*ss;
for i:=0 to length(ln_rs)-1 do
sst:=sst+ln_t[i]*ln_rs[i];
sst:=(1/length(ln_rs))*sst;
for i:=0 to length(ln_rs)-1 do
st2:=st2+ln_t[i]*ln_t[i];
st2:=(1/length(ln_rs))*st2;
Herst:=(sst-st*ss)/(st2-st*st); //coefficient of approximal function
al:=ss-st*Herst;
Thanks everybody =)
P.S.
for tau:=nach to l do
There is L, not 1. And L is Length of X array. And L>nach always besides last step, when l=nach.
P.P.S.
It works, guys. But values are not right. And they go out from range. Maybe, there is mistake in algorithm. Or maybe I skiped some step.
Last Update
It's mystic, but i only changed method of calculating array Z and it started works correctly....
Thanks all =)
First thing I see:
nach := 3;
for tau := nach to l do //w
This counts up. And because nach>1, the body of this loop won't be executed.
If you expect to count down. Use the downto variant. To count down:
for tau := nach downto l do //w
Given that the main loop (for tau) iterates from nach to l, the first four SetLength calls should set the length of l - nach + 1 instead of l - nach.
Should the line
z[i]:=z[i]+y[j];
not be
z[i]:=z[i - 1]+y[j];
?
newValue := oldValue;
repeat
delta := (RandomRange(0, 200) / 100) - 1;
newValue := newValue + delta;
until (newValue > 24) and (newValue < 40);
oldValue := newValue;
newValue2 := oldValue2;
repeat
delta := (RandomRange(0, 200) / 100) - 1;
newValue2 := newValue2 + delta;
until (newValue2 > 24) and (newValue2 < 40) and (newValue2 < newValue);
oldValue2 := newValue2;
after a few iterations, this hits an endless loop in the second loop. It is meant to change a Float randomly by -1 to +1 and keep it in the range 24 to 40 while still being less than another Float which is being randomly changed in the same way.
Who can be first to make me say "d'oh!"? (probably by (newValue2 < newValue))
d'oh!
Well, now that it is pointed out, the answer is obvious. newValue := oldValue + delta;, not ` newValue := newValue + delta;', so that the code reads (similar for both loops)
newValue := oldValue;
repeat
delta := (RandomRange(0, 200) / 100) - 1;
newValue := oldValue + delta; <==== **NOT** newValue
until (newValue > 24) and (newValue < 40);
oldValue := newValue;
Thanks, all, and lots of +1 all round
What do you mean by "keep it in the range 24 to 40"? Your condition "until (newValue > 24) and (newValue < 40)" implies that it will stop once it is in that range; it will go forever if it is outside that range.
The chances of it terminating depend upon oldValue. What values are you expecting oldValue to have?
In any case, such a loop is not guaranteed to terminate. You are changing the number randomly each time, so there is no guarantee it will move into the termination range at all. In particular, a large number of random numbers between -1 and 1 all added together will usually sum to approximately 0, so you can't expect the number to change significantly over time. It's probably the case that it happens never to enter that range.
You set the value of oldValue twice, once after each loop. It looks like you really want to set the value of oldValue2 after the second loop.
I'm not a delphi person so I could be way off (and I'll delete my answer if someone tells me I am)
but won't the delta just as likely be away from zero negatively as it will be positively on each iteration. Or simplified just as likely to be -1 as it is +1.
If this is true won't the value of newValue over many iteration have almost no change?
Update
Or to clarify won't the sum of a lot of random numbers between -1 and 1 be very near zero.
In any case wouldn't be simpler to create a variable to hold the number of iterationsyou might want.
e.g.
repeatcount:= RandomRange(24,40)
num:0
repeat
num := num +1
until (num = repeatcount)
or if you just want new value to be somewhere between 24 and 40
newValue := Random(24,40)
While i don't know Delphi, I wonder what's going on with the RandomRange function. The way its written, it looks as though RandomRange is getting you a number from 0 to 200, which you are then dividing by 100 to get a number from 0 to 2. Then, you subtract 1, getting a number from -1 to 1. If I read it correctly, then you the value should stay just about the same over time.
What do you get if you trace the values in the loop conditions?