I know the use of anonym function have to be done with parsimony but :
private function getAnonymFct() : Function
{
return function () : void
{
var num : uint = -1;
if( num < uint.MIN_VALUE )
trace( "WTF ??" );
trace( getQualifiedClassName( num ) );
trace( num );
}
}
public function Main()
{
getAnonymFct()();
}
Will trace :
int
WTF ??
-1
Any ideas why my var num magically become an int instead of uint as typed ?
Type uint is shorthand for unsigned integer, -1 is not a valid unsigned integer, it is a signed integer and is less than uint.MIN_VALUE. I would assume, to avoid an obvious runtime error actionscript has converted num to type int.
In AS both unsigned and signed are stored as 32-bits and -1 in base10 as int would be 11111111111111111111111111111111 in base2, converting that to uint it would be 4294967295 in base 10, which is uint.MAX_VALUE and 10 orders of magnitude different to the original number
Related
I need to create a function that transmits a parameter as a 'char' and returns the value of that 'char' as a integer at its square value.
For example:
char nr[] = "11";
cout << strToNumber(nr);//will return the value 121;
int strToNumber (char c[]) {
int cifra, rezultat, fin;
cifra = c - '0';
return cifra * cifra;
}
int main () {
char sir[9] = "13";
char c;
int n = strlen(sir);
for (int i = 0; i < n; ++i) {
c = sir[i];
strToNumber(c);
}
return 0;
}
I tried to send the whole 'char' as a parameter one at a time. First I sent sir[0] which is '1' and afterwards I sent sir[1] which is '3'. The problem is that I did not manage to save the parameters in one variable so that I can easily multiply it by itself. In this case each parameter is multiplied by itself one at a time. '1' * '1' and '3' * '3'.
I am new to this 'char' and I do not fully understand it yet. If there is another way to send my parameters and to handle the function I would truly appreciate it... Thanks in advance!
I get this error at line 50.I dont know what to use instead of (*p).
I am learning how to use pointers and trying to use pointers in a function passing arguments by reference.
I've been staring at it for some time now.
# include "stdio.h"
int odd (int (*), int );
main(){
int i,n;
int size;
int main(){
int v[i];
int *p;
p = &v[0];
printf("Write the quantity of integers you want to ingress");
scanf("%d",&size);
for(i=0;i<size;i++){
printf("write a number");
scanf("%d",&n);
v[i]= n;
p = &v[i];
odd(&v[i],size);
printf("The value number %d is: %d \n",i,*p);
}
return 0;
}
int odd(int *p,int siz){
int i;
int counter = 0;
for(i=0;i<siz;i++){
/*50*/ if(*p % 2 = 0){ }
else counter++ ;
return counter;
}
}
You are confusing assignment (=) with testing for equality (==). Change:
if(*p % 2 = 0)
to:
if(*p % 2 == 0)
Also your prototype for odd is wrong - change:
int odd (int (*), int );
to:
int odd (int *, int );
I have the following problem that I want to implement on CUDA:
I want to read an array (say "flag[20]"), and based on a certain condition, write indices of this array to another array (say "pindex[]")
Simple code implementation in C can be:
int N = 20;
int flag[N];
int pindex[N];
for(int i=0;i<N;i++)
flag[i] = -1;
for(int i=0;i<N;i+=2)
flag[i] = 0;
for(int i=0;i<N;i++)
pindex[i] = 0;
//operation: count # of times flag != -1 and write those indices in a different array
int pcount1 = 0;
for(int i=0;i<N;i++)
{
if(flag[i] != -1)
{
pindex[pcount1] = i;
++pcount1;
}
}
How will I implement this in CUDA?
I can use atomicAdd() to calculate total number of times my condition is satisfied. But, how do I write indices in a different array. For example, I tried the following:
__global__ void kernel_tryatomic(int N,int* pcount,int* flag, int* pindex)
{
int tId=threadIdx.x;
int n=(blockIdx.x*2+blockIdx.y)*BlockSize+tId;
if(n > N-1) return;
if(flag[n] != -1)
{
atomicAdd(pcount,1);
atomicExch(&pindex[*pcount],n);
//pindex[*pcount] = n;
}
}
This code calculates "pcount" correctly, but does not update "pindex" array.
I need help to do this operation on GPUs.
Thanks
Since your condition (flag) is conceptually a binary, you can use binary prefix sum (thoroughly explained here) to determine which place the thread with a positive flag should write.
For example if N is 20, with the help of below __device__ functions:
__device__ int lanemask_lt(int lane) {
return (1 << (lane)) − 1;
}
__device__ int warp_prefix_sums(int lane, int p) {
const int mask = lanemask_lt( lane );
int b = __ballot( p );
return __popc( b & mask );
}
your __global__ function can simply be written like below:
__global__ void kernel_scan(int N,int* pcount,int* flag, int* pindex)
{
int tId=threadIdx.x;
if(tId >= N)
return;
int threadFlag = ( flag[tId] == -1 ) ? 0 : 1;
int position_to_write = warp_prefix_sum( tId & (warpSize-1), threadFlag );
if( threadFlag )
pindex[ position_to_write ] = tId;
}
If N is bigger than the warp size (32), you can use intra-block binary prefix sum that is explained in the provided link.
When I debugging my code this error shown when cursor goto :
scoreMatrix[0] = dev_matrix[a]+similarityScore(dev_strA[a-1],dev_strB[b-1]); and repeatedly shown for scoreMatrix[1], scoreMatrix[2], scoreMatrix[3]
__global__ void kernel_ScoreMatrix(char *dev_strA, char *dev_strB, int *dev_matrix, int *dev_array, int *array_length)
{
int x= blockIdx.x;
int y=blockIdx.y;
int m = COLUMNS*y + x;
for (int i=0; i<*(array_length); i++)
if (m==dev_array[i]){
int a = COLUMNS*(y-1) + (x-1);
int b= COLUMNS*(y-1) + (x);
int c= COLUMNS*(y) + (x-1);
int scoreMatrix[4];
scoreMatrix[0] = dev_matrix[a]+similarityScore(dev_strA[a-1],dev_strB[b-1]);
scoreMatrix[1] = dev_matrix[b]+GAP;
scoreMatrix[2] = dev_matrix[c]+GAP;
scoreMatrix[3] = 0;
dev_matrix[m] = findMax(scoreMatrix,4);
}
}
this a picture of problem.
Value of the variable a equals zero so the statement dev_strA[a-1] causes an access violation.
For the purpose of the exercise, I have to implement the exponential function with the most basic arithmetic operations. I came up with this, where x is the base and y the exponent:
function expAetB() {
product=1;
for (i=0; i<y; i++)
{
product=product*x;
}
return product;
};
However, there are more basic operations than product=product*x;. I should somehow be able to insert instead another for loop which multiply and pass the result, but I can't find a way to do it without falling into an infinite loop.
In the same way that exponentiation is repeated multiplication, so multiplication is simply repeated addition.
Simply create another function mulAetB which does that for you, and watch out for things like negative inputs.
You could go even one more level and define adding in terms of increment and decrement, but that may be overkill.
See, for example, the following program which uses the overkill method of addition:
#include <stdio.h>
static unsigned int add (unsigned int a, unsigned int b) {
unsigned int result = a;
while (b-- != 0) result++;
return result;
}
static unsigned int mul (unsigned int a, unsigned int b) {
unsigned int result = 0;
while (b-- != 0) result = add (result, a);
return result;
}
static unsigned int pwr (unsigned int a, unsigned int b) {
unsigned int result = 1;
while (b-- != 0) result = mul (result, a);
return result;
}
int main (void) {
int test[] = {0,5, 1,9, 2,4, 3,5, 7,2, -1}, *ip = test;
while (*ip != -1) {
printf ("%d + %d = %3d\n" , *ip, *(ip+1), add (*ip, *(ip+1)));
printf ("%d x %d = %3d\n" , *ip, *(ip+1), mul (*ip, *(ip+1)));
printf ("%d ^ %d = %3d\n\n", *ip, *(ip+1), pwr (*ip, *(ip+1)));
ip += 2;
}
return 0;
}
The output of this program shows that the calculations are correct:
0 + 5 = 5
0 x 5 = 0
0 ^ 5 = 0
1 + 9 = 10
1 x 9 = 9
1 ^ 9 = 1
2 + 4 = 6
2 x 4 = 8
2 ^ 4 = 16
3 + 5 = 8
3 x 5 = 15
3 ^ 5 = 243
7 + 2 = 9
7 x 2 = 14
7 ^ 2 = 49
If you really must have it in a single function, it's a simple matter of refactoring the function call to be inline:
static unsigned int pwr (unsigned int a, unsigned int b) {
unsigned int xres, xa, result = 1;
// Catch common cases, simplifies rest of function (a>1, b>0)
if (b == 0) return 1;
if (a == 0) return 0;
if (a == 1) return 1;
// Do power as repeated multiplication.
result = a;
while (--b != 0) {
// Do multiplication as repeated addition.
xres = result;
xa = a;
while (--xa != 0)
result = result + xres;
}
return result;
}