$c = 8 / (12 - 8) * 4;
echo $c; //output 8
I read in http://php.net/manual/en/language.operators.precedence.php. I think that it should be 8/16 instead of 8. Are there anybody help me explaination, thanks.
$c = 8 / (12 - 8) * 4;
echo $c; //output 8
Because (12 - 8) equal 4 it will execute first. After this execution we will get
$c = 8 / 4 * 4;
After that the result will be 2 * 4 php will execute 8 / 4 and give a result 2.
Finally the result will be 8.
In this code Parentheses will get first priority, then the division and finally multiplication.
operators' precedence order from high to low:/ == * > -
But () will enforce priority to the highest.
Operators with the same precedence, operations order will be from left to right.
so, the operations process is:
<?php
$c = 8 / (12 - 8) * 4;
// Give priority to parentheses inside
$c = 8 / 4 * 4; // 12 - 8 ==> 4
// operators with precedence, from left to right
$c = 2 * 4; // 8 / 4 ==> 2
$c = 8; // 2 * 4 ==> 8
This involves only the basic mathematical arithmetic
Related
I have been investigating various collision resolution techniques for hashtables implemented via open addressing. However, all the collision resolution methods I have investigated so far (linear probing, quadratic probing, double hashing) have the pitfall that there exists a probing sequence that produces a cycle whose length is less than the size of the table. This becomes problematic when you're trying to insert an element with the open addressing scheme because there are free buckets to insert an entry but they might not be reachable if they're not part of the cycle.
For example, if we're using linear probing on a table of size 12 with the linear function: H(k, i) = (h(k) + 4*i) mod 12 then a cycle would occur if a particular key hashes to 8 and all the slots 0, 4, and 8 are already filled:
H(k, 0) = 8 + 0 mod 12 = 8
H(k, 1) = 8 + 4 mod 12 = 0
H(k, 2) = 8 + 8 mod 12 = 4
H(k, 3) = 8 + 12 mod 12 = 8
H(k, 4) = 8 + 16 mod 12 = 0
H(k, 5) = 8 + 20 mod 12 = 4
H(k, 6) = 8 + 24 mod 12 = 8
...
Similar cycles can also be found with quadratic and double hashing if the probing sequence is bad, so my question then is how are cycles handled? Or do we always pick hash functions/special table sizes which do not permit cycles which are too short?
Could you please let me know how to get the full p-value from gsl_cdf_tdist_P function when p-value is smaller than 1E-16? I am getting 0 instead.
Thanks,
Woody
print "t-test p-value = " . ttest(\#n,\#t) . "\n";
sub ttest{
my ($n,$t) = #_;
my #n = #$n;
my #t = #$t;
my $nn = pdl(#n);
my $tt = pdl(#t);
my ($tstats, $df) = t_test( $nn, $tt );
use PDL::GSL::CDF;
my $p_2tail = 2 * (1 - gsl_cdf_tdist_P( $tstats->abs, $df ));
return $p_2tail;
}
My input values as follows:
my #n = qw (1 2 4 2 3 1 2 4 2 1 2 4 2 3 1 2 4 2 1 2 4 2 3 1 2 4 2);
my #t = qw (11 12 13 12 13 11 14 11 12 13 12 13 11 14 11 12 13 12 13 11 14);
I found an easy solution for this problem. I used gsl_cdf_tdist_Q to get p-values. The p-values are not limited to 16 digits after decimal because they are not remapped p-values like (1-gsl_cdf_tdist_P).
Woody
I use factor(n) in matlab but it don't work for numbers like ((10^11-1)/11) - what can I do? My source is here.
m=input('Enter your number: ');
i=0;
while (i<m)
if(isprime(i))
% sum=factor((10^(i-1)-1));
sum=factor((10^(i-1)-1)/i);
disp(sum);
end
i =i+1;
end
but for large n it returns errors!!
>> FactorGen
Enter your number: 45
3 3
3 3 11
3 3 11 101
3 3 3 7 11 13 37
3 3 11 41 271 9091
3 3 3 7 11 13 37 101 9901
Error using factor (line 26) When n is single or double, its maximum
allowed value is FLINTMAX.
Error in FactorGen (line 7) sum=factor((10^(i-1)-1));
I want the function factor((10^(i-1)-1)) to work for same number. How can I solve my problem?
I think this can be partially alleviated by converting your large number into uint64 format. For R2014b maximum integer that can be handled is:
n = intmax('uint64')
n = 1.8447e+19
While the maximum double that can be handled is:
n = flintmax('double')
n = 9.0072e+15
This can be verified by simple example. Let's use factor on the number larger than flintmax. First, try double:
factor(10^16)
Error using factor (line 26)
When n is single or double, its maximum allowed value is FLINTMAX.
Now, we try uint64:
factor(uint64(10^16))
ans = 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
So, using factor(uint64(x)) increases your limit by several orders. Indeed, there's no sense in using double format for factor function.
By the way, since using factor on large numbers may freeze your system, I suggest using this function:
function f = Factorize(n)
i = 0;
while mod(n,2) == 0
i = i + 1;
n = n/2;
f(i) = 2;
disp([2,n])
end
q = round(sqrt(double(n)));
q = q + 1 - mod(q,2);
for j = 3:2:q
while mod(n,j) == 0
i = i + 1;
f(i) = j;
n = n/j;
end
end
if n > 2;
i = i + 1;
f(i) = n;
end
It is much faster for large numbers and does not overload the system at large n
Hope that helps
I am quite new to matlab and I am trying to find a way to accomplish the following task without using for loops:
I have a data set looking like this:
data = [1 5; 1 3; 1 8; 2 1; 2 2; 2 5; 3 3; 3 8; 3 4]
the first column is a group (being a month year combination in the future)
Now I want to calculate a sum over the second column with a given window size but only if the group index is the same - if not the maximal sum in this group shall be calculated.
With window size=2 I would like to create the following result:
summed_data = [1 8; 1 11; 1 8; 2 3; 2 7; 2 5; 3 11; 3 12; 3 4]
With a window size of 3 the result would look like this:
summed_data = [1 16; 1 11; 1 8; 2 8; 2 7; 2 5; 3 15; 3 12; 3 4]
and so on.
I thought about using accumarray by creating sufficient subindexes - but I have a problem with the window size and that the sums are overlapping.
Has anyone an idea on how to implement it without using loops?
Thanks in advance and best regards
stephan
This seems to work:
ws = 2; k = [ones(ws,1);zeros(mod(ws,2),1)];
C = accumarray(data(:,1),data(:,2),[],#(v){conv(v,k,'same')})
You seem to be anchoring to the current pixel in the window and looking forward. Is that correct?
Not sure if this covers all corner cases, but it might steer you in the right direction.
Test: ws = 2
ws = 2; k = [ones(ws,1);zeros(mod(ws,2),1)];
C = accumarray(data(:,1),data(:,2),[],#(v){conv(v,k,'same')});
summed_data = [data(:,1) vertcat(C{:})]
summed_data =
1 8
1 11
1 8
2 3
2 7
2 5
3 11
3 12
3 4
Test: ws = 3
summed_data = [data(:,1) vertcat(C{:})]
summed_data =
1 16
1 11
1 8
2 8
2 7
2 5
3 15
3 12
3 4
I was previously under the (naive) assumption that the modulus operator returned the remainder of division. I was apparently wrong, as -2 % 5 returns 3. I would have thought that 5 divides -2 zero times with -2 as the remainder.
Now I understand the mechanics of how this operation is performed, but my question is why? Could someone give me a link to something that explains why modulus and remainder are not synonymous, or an example of a situation where it would be useful?
The result is entirely correct. Modular arithmetic defines the following (I'll use "congruent" since I can't type the equal sign with three lines)
a congruent b mod c iff a-b is a multiple of c, i.e. x * c = (a-b) for some integer x.
E.g.
0 congruent 0 mod 5 (0 * 5 = 0-0)
1 congruent 1 mod 5 (0 * 5 = 1-1)
2 congruent 2 mod 5 (0 * 5 = 2-2)
3 congruent 3 mod 5 (0 * 5 = 3-3)
4 congruent 4 mod 5 (0 * 5 = 4-4)
5 congruent 0 mod 5 (1 * 5 = 5-0)
6 congruent 1 mod 5 (1 * 5 = 6-1)
...
The same can be extended to negative integers:
-1 congruent 4 mod 5 (-1 * 5 = -1-4)
-2 congruent 3 mod 5 (-1 * 5 = -2-3)
-3 congruent 2 mod 5 (-1 * 5 = -3-2)
-4 congruent 1 mod 5 (-1 * 5 = -4-1)
-5 congruent 5 mod 5 (-1 * 5 = -5-0)
-6 congruent 4 mod 5 (-2 * 5 = -6-4)
-7 congruent 3 mod 5 (-2 * 5 = -7-3)
...
As you can see, a lot of integers are congruent 3 mod 5:
..., -12, -7, -2, 3, 8, 13, ...
In mathematics, the set of these numbers is called the equivalence class induced by the equivalence relation "congruence". Our understanding of the remainder and the definition of the "mod" function are based on this equivalence class. The "remainder" or the result of a mod computation is a representative element of the equivalence class. By declaration we have chosen the smallest non-negative element (so -2 is not a valid candidate).
So when you read -2 mod 5 = x this translates to "Find the smallest non-negative x so that there exists an integer y with y * 5 = -2 - x", in concordance with the definition of congruence. The solution is y=1 and x = 3 as you can see by simply trying out other values for y.
a = n (mod m) is defined as a = n + m*t and it applies to negative numbers equally well. (Another to look at it is that a = n (mod m) means (a - n) is a multiple of m)
-2 = 3 (mod 5) because -2 = 3 - 5 (i.e. t = -1)
The convention is that the result of taking a modulo m is a number between 0 and m - 1 (inclusive)
The fundamental guarantee that you get is that
(a % b) + b * (a / b) == a
For signed values, there is no reason either sign should be the preferred outcome of a modulo or divide operation. Some languages fix one form, others leave it up to the implementation, so that the implementation can use whichever way the hardware happens to provide. The hardware instruction, in turn, may have been chosen to operate efficiently the hardware's representation of signed integers.
Generally, be very careful when using signed integers together with division, remainder and bit shift operations.
I guess it depends on whether you want your result rounded down or rounded towards 0:
2 / 5 = 0.4 = 5*0 + 2 works in both cases, whereas
-2 / 5 = -0.4 = 5*0 + -2 if you're rounding towards 0 (truncation),
-2 / 5 = -0.4 = 5*-1 + 3 if you're rounding down (floor).
Note that the result is always positive (for a positive divisor) in the second case and it would be useful, for example, when calculating an array index:
hashmapBuckets[getIntHash(obj) % hashmapBuckets.size].add(obj)
or normalizing an angle:
angle = angle % 360; //0-359
It is in fact the other case I'm having trouble finding practical examples for :)
--
Oh, and the Wikipedia page on the modulo operation has some nice graphs. Note that the remainder always has the same sign as the divisor for a floored division.
Think of modulo as an operator that wraps a line of length y (in terms of y % x) around a circle of x pegs. The remaining length of the line that doesn't fully wrap around x is the resultant.