Titanium Accelerator - How to output the result of a function - appcelerator-mobile

i have the following code for rolling a dice.
function rollDice(){
var die1 = Math.floor(Math.random() * 6) + 1;
var die2 = Math.floor(Math.random() * 6) + 1;
var diceTotal = die1 + die2;
}
i am wanting this function to execute when a button is pressed so i have this.
button1.addEventListener('click', function(e){
rollDice();
});
so how do i display the results of die1,die2 and diceTotal currently i have return in just because it's what im used to using to displaying the result.

If you want to access dice1, dice2, diceTotal outside the function rollDice(), you can define variable outside rollDice() (see below). After doing that, you can access that variable in any part of same js file.
var dice1, dice2, diceTotal;
function rollDice(){
dice1 = Math.floor(Math.random() * 6) + 1;
dice2 = Math.floor(Math.random() * 6) + 1;
diceTotal = dice1 + dice2;
}
button1.addEventListener('click', function(e){
rollDice();
alert( 'dice1=' + dice1 + ' dice2=' + dice2 + ' diceTotal=' + diceTotal);
}

Related

Why are the results different under debugging and running?

I'm solving a algorithm problem https://codeforces.com/contest/1671/problem/E. Although my submit can pass the tests provided by the contest, I find it fails on specific test(Hack). When I'm trying to find where's the error, I find that if I choose start debugging, the program would run perfectly. However, when I click "run", it would give a wrong answer. So, I'm curious about what happens.
#include<iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int const NN = 1e6;
int const MOD = 998244353;
char str[NN];
long long dfs_data[NN];
int powans[20];
string myhash[NN];
int n;
long long dfs(int num) {
if (dfs_data[num] != 0) return dfs_data[num];
if (num >= powans[n - 1] - 1) {
dfs_data[num] = 1;
return 1;
}
if (myhash[num * 2 + 1] == myhash[num * 2 + 2]) {
dfs_data[num] = (dfs(num * 2 + 1) % MOD) * (dfs(num * 2 + 2) % MOD) % MOD;
} else dfs_data[num] = 2 * (dfs(num * 2 + 1) % MOD) * (dfs(num * 2 + 2) % MOD) % MOD;
return dfs_data[num];
}
void gethashcode(int t) {
if (t >= powans[n - 1] - 1) {
myhash[t] += str[t];
return;
}
if (myhash[2 * t + 1] == "") gethashcode(2 * t + 1);
if (myhash[2 * t + 2] == "") gethashcode(2 * t + 2);
if (myhash[2 * t + 1] < myhash[2 * t + 2]) myhash[t] = str[t] + myhash[2 * t + 1] + myhash[2 * t + 2];
else myhash[t] = str[t] + myhash[2 * t + 2] + myhash[2 * t + 1];
}
void solve() {
memset(dfs_data, 0, sizeof dfs_data);
cin >> n;
cin >> str;
powans[0] = 1;
for (int i = 1; i < 19; i++) {
powans[i] = 2 * powans[i - 1];
}
for (int i = 0; i < NN; i++) {
myhash[i] = "";
}
gethashcode(0);
cout << dfs(0);
}
int main() {
solve();
}

Result not matching up to Set Variable

I am scratching my brain off trying to figure this one out.
Why are running backs getting 60+ carries a game with this code? I just don't get it.
The team is an array, this for loop is only supposed to run 25 times AT MOST. I've even tried setting the for loop to (int g = 1; g < 20; g++) and yet still it is coming up with 60+ carries a game for no reason. Any help?
hold3 = rand() % (10 - 15 + 1) + 15;
for(int g = 1; g < hold3; g++){
hold = rand() % (15 - team[u].elusivness[2] + 1) + team[u].elusivness[2];
if(hold == 13 || hold == 14){
team[u].carries[2]++;
hold = rand() % (17 - team[u].strength[2] + 1) + team[u].strength[2];
if(hold == 13){
team[u].rushingtouchdowns[2]++;
hold = rand() % 25 + 5;
team[u].rushingyards[2] += hold;
}else{
hold = rand() % (12 - team[u].speed[2] + 1) + team[u].speed[2];
if(hold == 12){
hold = rand() % 15 + 5;
team[u].rushingyards[2] += hold;
}else{
hold = rand() % 5 + 1;
team[u].rushingyards[2] += hold;
}
}
}
}
you are trying getting mod from deviser below 0. hold3 = rand() % (10 - 15 + 1) + 15;

C++ manually-unrolled (conditional-sum) is slower than regular code, was expecting AVX vectorization

test_euclid_ask.h (only need to read 2 functions: euclid_slow, euclid_fast)
#pragma once
#include "included.h"
double
euclid_slow(int n, double* data1, double* data2, int* mask1, int* mask2, const double weight[])
{
double result = 0.0;
double totalWeight = 0;
for (int i = 0; i < n; i++) {
if (mask1[i] && mask2[i]) {
double term = data1[i] - data2[i];
result += weight[i] * term * term;
totalWeight += weight[i];
}
}
if (totalWeight==0) return 0;
return result / totalWeight;
}
double
euclid_fast(int n, double* data1, double* data2, int* mask1, int* mask2, const double weight[])
{
double result = 0.0;
double totalWeight = 0;
double subResult[4] = { 0. };
double subTweight[4] = { 0. };
double subDiff[4] = { 0. };
double subWeight[4] = { 0. };
double subMask[4] = { 0. };
int nstep4 = n - n % 4;
for (int i = 0; i < nstep4; i += 4) {
subMask[0] = mask1[i] && mask2[i];
subMask[1] = mask1[i + 1] && mask2[i + 1];
subMask[2] = mask1[i + 2] && mask2[i + 2];
subMask[3] = mask1[i + 3] && mask2[i + 3];
if (!(subMask[0] || subMask[1] || subMask[2] || subMask[3])) continue;
subDiff[0] = data1[i] - data2[i];
subDiff[1] = data1[i + 1] - data2[i + 1];
subDiff[2] = data1[i + 2] - data2[i + 2];
subDiff[3] = data1[i + 3] - data2[i + 3];
subDiff[0] *= subDiff[0];
subDiff[1] *= subDiff[1];
subDiff[2] *= subDiff[2];
subDiff[3] *= subDiff[3];
subWeight[0] = weight[i] * subMask[0];
subWeight[1] = weight[i + 1] * subMask[1];
subWeight[2] = weight[i + 2] * subMask[2];
subWeight[3] = weight[i + 3] * subMask[3];
subTweight[0] += subWeight[0];
subTweight[1] += subWeight[1];
subTweight[2] += subWeight[2];
subTweight[3] += subWeight[3];
subResult[0] += subWeight[0] * subDiff[0];
subResult[1] += subWeight[1] * subDiff[1];
subResult[2] += subWeight[2] * subDiff[2];
subResult[3] += subWeight[3] * subDiff[3];
}
for (int i = nstep4; i < n; i++) {
if (mask1[i] && mask2[i]) {
double term = data1[i] - data2[i];
result += weight[i] * term * term;
totalWeight += weight[i];
}
}
result += subResult[0] + subResult[1] + subResult[2] + subResult[3];
totalWeight += subTweight[0] + subTweight[1] + subTweight[2] + subTweight[3];
//cout << "end fast\n";
if (!totalWeight) return 0;
return result / totalWeight;
}
void test_euclid_ask()
{
const int MAXN = 10000000, MINN = 1000000;
double* data1, * data2;
int* mask1, * mask2;
double* dataPro1, * dataPro2;
int* maskPro1, * maskPro2;
double *weight, * weightPro;
//***********
data1 = new double[MAXN + MINN + 1];
data2 = new double[MAXN + MINN + 1];
mask1 = new int[MAXN + MINN + 1];
mask2 = new int[MAXN + MINN + 1];
dataPro1 = new double[MAXN + MINN + 1];
dataPro2 = new double[MAXN + MINN + 1];
maskPro1 = new int[MAXN + MINN + 1];
maskPro2 = new int[MAXN + MINN + 1];
// ******
weight = new double[MAXN + MINN + 1];
weightPro = new double[MAXN + MINN + 1];
MyTimer timer;
int n;
double guess1, guess2, tmp, total1 = 0, total2 = 0, prev1 = 0, prev2 = 0;
for (int t = 5000; t < 6000; t++) {
if (t <= 5000) n = t;
else n = MINN + rand() % (MAXN - MINN);
cout << n << "\n";
int index = 0;
for (int i = 0; i < n; i++) {
weight[i] = int64(randomed()) % 100;
data1[i] = int64(randomed()) % 100;
data2[i] = int64(randomed()) % 100;
mask1[i] = rand() % 10;
mask2[i] = rand() % 10;
}
memcpy(weightPro, weight, n * sizeof(double));
memcpy(dataPro1, data1, n * sizeof(double));
memcpy(dataPro2, data2, n * sizeof(double));
memcpy(maskPro1, mask1, n * sizeof(int));
memcpy(maskPro2, mask2, n * sizeof(int));
//****
int tmp = flush_cache(); // do something to ensure the cache does not contain test data
cout << "ignore this " << tmp << "\n";
timer.startCounter();
guess1 = euclid_slow(n, data1, data2, mask1, mask2, weight);
tmp = timer.getCounterMicro();
total1 += tmp;
cout << "time slow = " << tmp << " us\n";
timer.startCounter();
guess2 = euclid_fast(n, dataPro1, dataPro2, maskPro1, maskPro2, weightPro);
tmp = timer.getCounterMicro();
total2 += tmp;
cout << "time fast = " << tmp << " us\n";
bool ok = fabs(guess1 - guess2) <= 0.1;
if (!ok) {
cout << "error at N = " << n << "\n";
exit(-1);
}
cout << "\n";
}
cout << "slow speed = " << (total1 / 1000) << " ms\n";
cout << "fast speed = " << (total2 / 1000) << " ms\n";
}
Basically, the function computes a kind-of Euclidean distance between 2 arrays:
result = sum(weight[i] * (data1[i] - data2[i])^2)
but only in positions where both values are available (mask1[i]==0 means it's ignored, same with mask2). The normal code is in function euclid_slow.
So I tried to improve the code by processing 4 elements at once, hoping that SSE/AVX can speed this up. However, the result stays the same or slower(using g++ -O3 -march=native) or becomes 40% slower (using Visual Studio 2019 compiler, release mode (x64), -O2, AVX2 enabled). I tried both -O2 and -O3, same result.
The compiler made better optimizations than what I wrote. But how can I make it actually faster?
Edit: code to test the programs here

Unhandled exception: Access violation reading location 0x015E2348

Ok I have a little problem with my code. This error disp when I try run it.
Unhandled exception at 0x012D4CBF in Dywan.exe: 0xC0000005: Access violation reading location 0x015E2348.
When ROZMIAR=257 or less then code works properly. But i need set higher value like 500. Error stucked at double odcien = dane[x][y] + dane[x + dlboku][y] + dane[x][y + dlboku] + dane[x + dlboku][y + dlboku];
Should i use dynamic arrays to this ?
double dane[ROZMIAR][ROZMIAR];
double wartoscinit = 0.0;
dane[0][0] = dane[0][ROZMIAR - 1] = dane[ROZMIAR - 1][0] = dane[ROZMIAR -1][ROZMIAR - 1] = wartoscinit;`
void comp_Fractal(){
for (int dlboku = ROZMIAR - 1; dlboku >= 2; dlboku /= 2, h /= 2.0) {
int polboku = dlboku / 2;
for (int x = 0; x<ROZMIAR - 1; x += dlboku) {
for (int y = 0; y<ROZMIAR - 1; y += dlboku) {
double odcien = dane[x][y] + dane[x + dlboku][y] + dane[x][y + dlboku] + dane[x + dlboku][y + dlboku];
odcien /= 4.0;
double wzor = (-h) + rand() * (2*h) / RAND_MAX;
dane[x + polboku][y + polboku] = odcien + wzor;
}
}
for (int x = 0; x<ROZMIAR - 1; x += polboku) {
for (int y = (x + polboku) % dlboku; y<ROZMIAR - 1; y += dlboku) {
double odcien =
dane[(x - polboku + ROZMIAR) % ROZMIAR][y] +
dane[(x + polboku) % ROZMIAR][y] +
dane[x][(y + polboku) % ROZMIAR] +
dane[x][(y - polboku + ROZMIAR) % ROZMIAR];
odcien /= 4.0;
double wzor = (-h) + rand() * (2*h) / RAND_MAX;
odcien = odcien + wzor;
dane[x][y] = odcien;
if (x == 0) dane[ROZMIAR - 1][y] = odcien;
if (y == 0) dane[x][ROZMIAR - 1] = odcien;
}
}
}
for (int i = 0; i<ROZMIAR - 1; i++)
for (int j = 0; j<ROZMIAR - 1; j++) {
if (dane[i][j] > maxY)
maxY = dane[i][j];
if (dane[i][j] < minY)
minY = dane[i][j];
}
}
When both dlboku, y and x are something like ROZMIAR - 2, following
dane[x + dlboku][y + dlboku];
will make you to access dane[ROZMIAR + ROZMIAR - 4][ROZMIAR + ROZMIAR - 4] which is out of bounds. You alvays had a problem with your code. It just large values of ROZMIAR which made it access out of allocated memory and crash.

I met some errors when I want to convert IpLmage to mat, opencv

void LBP(Mat src, IplImage* dst)
{
int tmp[8] = { 0 };
CvScalar s;
Mat temp = Mat(src.size(), IPL_DEPTH_8U, 1);
uchar *data = (uchar*)src.data;
int step = src.step;
//cout << "step" << step << endl;
for (int i = 1; i<src.size().height - 1; i++)
for (int j = 1; j<src.size().width - 1; j++)
{
int sum = 0;
if (data[(i - 1)*step + j - 1]>data[i*step + j])
tmp[0] = 1;
else
tmp[0] = 0;
if (data[i*step + (j - 1)]>data[i*step + j])
tmp[1] = 1;
else
tmp[1] = 0;
if (data[(i + 1)*step + (j - 1)]>data[i*step + j])
tmp[2] = 1;
else
tmp[2] = 0;
if (data[(i + 1)*step + j]>data[i*step + j])
tmp[3] = 1;
else
tmp[3] = 0;
if (data[(i + 1)*step + (j + 1)]>data[i*step + j])
tmp[4] = 1;
else
tmp[4] = 0;
if (data[i*step + (j + 1)]>data[i*step + j])
tmp[5] = 1;
else
tmp[5] = 0;
if (data[(i - 1)*step + (j + 1)]>data[i*step + j])
tmp[6] = 1;
else
tmp[6] = 0;
if (data[(i - 1)*step + j]>data[i*step + j])
tmp[7] = 1;
else
tmp[7] = 0;
s.val[0] = (tmp[0] * 1 + tmp[1] * 2 + tmp[2] * 4 + tmp[3] * 8 + tmp[4] * 16 + tmp[5] * 32 + tmp[6] * 64 + tmp[7] * 128);
cvSet2D(dst, i, j, s);
}
}
Above is my original code for local binary pattern, src is an input matrix and dst is the output. Now i want to change the IPLimage in void LBP(Mat src, IplImage* dst) to void LBP(Mat src, mat dst), i tried many ways but I always met problems like assertion failed or something else, i thinks it might be the problem of cvSet2D(dst, i, j, s);
this is the definition for the input src:
Mat Gray_face = Mat(image.size(), image.depth(), 1);
this is my definition for output dst:
IplImage* lbp_face = cvCreateImage(Gray_face.size(), IPL_DEPTH_8U, 1);
And i want to change it to mat and make it work for my program.
this is how i call the LBP function:
LBP(Gray_face, lbp_face);
I am quite new to this, can anyone help me? thank you very much!
Indeed cvSet2D is the old interface. To set a point to a cv::Mat dst you can use at(). Fisrt, you first (re)allocate it as:
dst.create(src.size(),CV_8U);
Then to set a value point in your case:
dst.at<char>(i,j) = (tmp[0] * 1 + tmp[1] * 2 + tmp[2] * 4 + tmp[3] * 8 + tmp[4] * 16 + tmp[5] * 32 + tmp[6] * 64 + tmp[7] * 128);
Last but not least, if you want the result returned, you function definition should be:
void LBP(Mat src, Mat & dst);
with a reference (&) to the destination.