This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
My code produces this error:
ISO C++ forbids comparison between pointer and integer [-fpermissive]
I have commented out the body of my code which doesn't have an impact what is causing the error. The code is a section of a program I wrote to calculate Newton's Method. I really do not know very much about pointers. I am trying to avoid them right now. All I want to do is stop my while loop if it runs too many times.
int iter = 0;
while (abs(nextValue - currValue) > 0.00000000001) and iter < 100000;
{
// currValue = nextValue;
//
// double polyValue = 0;
// int n3;
// for (n3 = degree; n3 >= 0; n3--)
// {
// polyValue += coef[n3] * pow(currValue, n3);
// }
// double polynomial = polyValue;
//
// polyValue = 0;
// int n4;
// for (n4 = degree; n4 >= 1; n4--)
// {
// polyValue += coef[n4] * n4 * pow(currValue, n4 - 1);
// }
// double polyPrime = polyValue;
//
// nextValue = currValue - (polynomial / polyPrime);
iter += 1;
}
The while condition should probably read as:
while (abs(nextValue - currValue) > 0.00000000001 && iter < 100000)
Note that
There is no semicolon at the end.
The entire condition must be in parentheses.
and is replaced by && - this is not strictly necessary because and is valid in C++ as far as I know, but I have never seen and being used in production code so far.
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
here is the code which is giving error 'ranf' was not declared in this scope.
the code is about generating random numbers with initial position and velocity.
i am not able to configure how to resolve the problem.
#include<iostream>
#include<fstream>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<map>
using namespace std;
const int N= 3000, nstep= 20000000, nps= 50000, nprint =1000, noverlap= 10000;
const double R=1, eps=0.95, Lbox=150,pssize= 500;
const double infty= 1e20, null= 1e-10;
vector<double> x(N), y(N), vx(N), vy(N);
vector<double> clist[N];
double Time = 0;
map<double, pair<int,int> > cseq;
void init(double tol)
{
bool overlap;
int i, j;
x[0]= ranf(Lbox-R-tol);
y[0]= ranf(Lbox-R-tol);
vx[0]=ranf(1);
vy[0]=ranf(1);
for(i=1;i<N;i++)
{
if(!(1 % 100)) cout << "Init " << i << endl;
do {
overlap = false;
x[i]= ranf(Lbox-R-tol);
y[i]= ranf(Lbox-R-tol);
j=0;
do {
overlap = ((x[i]-x[j])*(x[i]-x[j])+ (y[i]-y[j])*(y[i]-y[j]) < 4*(R+tol)*(R+tol));
}
while((++j<i) && !overlap);
}
while(overlap);
vx[i]=ranf(1);`enter code here`
vy(i)=ranf(1);
}
}
The ranf function you are calling is not standardized in any way I'm aware of, so it's undefined because it simply does not exist. You need to define it first. Something like this should work:
double ranf(double max)
{
return (((double)rand())/RAND_MAX)*max;
}
Are you sure you got the right function? It is actually rand defined in cstdlib. See here for a reference.
The function ranf seems designed to return a random floating point in the (0, x) range.
You can check out C++ random float number generation and roll out your own ranf:
/**
* ranf - return a random double in the [0,m] range.
* #param m maximum value to be returned
* #return a random double in the [0,m] range
*/
double ranf(double m) {
return (m*rand())/(double)RAND_MAX;
}
or also
#define ranf(m) (((m)*rand())/(double)RAND_MAX)
Remember to call srand to initialize the random number generator, or each run of your program will always return the same sequence of "random" numbers. (which can be useful for debugging, though).
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have found a lot of answers that have to do with release version problems but none with the exact opposite.
I have a loop similar to the following:
while(index < 7 && FlagIsUp)
{
// process
Inner Loop
Inner Inner Loop
Array[index] = number;
++index;
}
Problem is that index changes radically from 6 (the last iteration) to 17209 for an int16_t and 1133165442 for size_t. NOTHING IN THE LOOP changes the index except the ++index. I replaced the while with a for and it still happens.
It only happens in debug mode, in release version it does finish without issues.
I also added volatile to the index and results were the same, it still overflowed.
Any ideas, pointers, would be appreciated. I can't provide a working copy of the bug so any theories are welcomed, I want to exhaust my options to find the problem.
EDIT:
Yes I'm sorry. I gave to little information.
First off I'm working with QNX Momentics Version: 4.6.0 and my debugger is part of the GNU Compiler Collection 4.3.3.
Now the inner loop is this:
cSignalNoIndex = 0;
while ((cSignalNoIndex < (2 * NO_PHASES + 1)) && !ShutDownFlag)
{
wSF0 = 0;
wExtSF = 0;
dwSFAcc = 0;
dwExtSFAcc = 0;
std::string SignalNo= " Waveform number " + Tool::toString(cSignalNoIndex);
Results[cSignalNoIndex].printWaveForm(SignalNo);
// Prepare Input vectors for FFT compute
cComponent = 0;
while (cComponent < (HCYCLE_SAMPLES << 1))
{
awReal[cComponent] = static_cast<int>(Results[cSignalNoIndex].WaveForm[cComponent/64][cComponent % 64]);
awImg[cComponent] = 0;
pwSource++;
cComponent++;
}
Results[cSignalNoIndex].printWaveForm(SignalNo);
// Get FFT (forward)
// Changed the wPwr from 7 to something else
iFft(&awReal[0], &awImg[0], wPwr, 1);
Results[cSignalNoIndex].printWaveForm(SignalNo);
// Compute magnitudes
//fMult = pInBlock3->fMult[cSignalNoIndex]; // Get Multiplier
fMult = 1;
for (cComponent = 0; cComponent < HCYCLE_SAMPLES && !ShutDownFlag; cComponent++)
{
int64_t dlOp = static_cast<int64_t>(awReal[cComponent]) * awReal[cComponent] + static_cast<int64_t>(awImg[cComponent]) * awImg[cComponent];
dlOp <<= 1; // Apply sqrt(2) term to result
dlOp = static_cast<int>(fMult * isqrt64(dlOp));
// Store into FFT object
oFFTMag3.wFFT[cSignalNoIndex][cComponent] = static_cast<int16_t>( dlOp );
// Set Base frequency magnitude and accumulate harmonics
if (cComponent == 1) // Base
{
wSF0 = dlOp;
if(cSignalNoIndex == 6)
{
wRefMagnitude = static_cast<int16_t> ( 0.4 * wSF0 );
}
if(awReal[1] != 0) // Also get phase for Base
{
dfPhase = std::atan((double)((float)awImg[1]/awReal[1])) * 180.0 / PI;
}
else
{
if(awImg[1] >= 0)
{
dfPhase = 90.0;
}
else
{
dfPhase = -90.0;
}
}
if(awReal[1] < 0) // convert to 2*PI range
{
dfPhase += 180.0;
}
else if(awImg[1] < 0)
{
dfPhase += 360.0;
}
//// THIS IS THE LINE
fPhase[cSignalNoIndex] = dfPhase; ////////// WTF! cSignalNoIndex = 6 - cComponent = 2
/// HERE cSignalNoIndex is overflown
}
}
You haven't really posted enough code but my best bet is Array[index] = number overwrites index at some point. The fact that it only happens sometimes (in your case, when debugging) is a good example of "undefined behavior".
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm currently trying use a very simple check using the variable "breakOK", in order to break out of a for loop. If the condition that the current value in my puzzle matrix is set to 46 is satisfied, a function is called and breakOK is set to 1 so that we can break out of the loop. However I get the following errors where I use breakOK in the if statements of the code below:
sudoku.cc:482:13: error: 'breakOK' was not declared in this scope
sudoku.cc:485:11: error: 'breakOK' was not declared in this scope
This is really weird because I declare Guess in the same manner and I do not get a scope error when using it at a later time in my code! Also, the compiler doesn't complain when I set breakOK equal to 1 in the loop. Any help would be much appreciated, I've been stuck on this for too long!
int Guess = 0; // will be set to a value if a guess is ok
int breakOk = 0; // will be set to 1 if breaking out of loops is necessary
for (int row = 0; row < 9; row++)
{
for (int col = 0; col < 9; col++)
{
if (puzzle.matrix[row][col].currentValue == 46)
{
Guess = guessValues(row, col, puzzle, tried, allowed);
breakOk = 1;
}
if (Guess != 0)
{
tried[row][col].tries.push_back(Guess);
//puzzle.decide(row, col, Guess);
puzzle.matrix[row][col].currentValue = Guess;
}
if (breakOK == 1) // line 482
break;
}
if (breakOK == 1) // line 485
break;
}
You declare breakOk, but check breakOK. Notice the capital K in OK.
It's because your variable is breakOk and you are referencing breakOK
breakOK is not the same as breakOk. Check your case on the k.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
What is the check_union256d function?
It's placed in following code:
/* { dg-do run } */
/* { dg-require-effective-target avx } */
/* { dg-options "-O2 -mavx" } */
#include "avx-check.h"
void static
avx_test (void)
{
int i;
union256d u, s1, s2;
double e [4];
s1.x = _mm256_set_pd (2134.3343,1234.635654,453.345635,54646.464356);
s2.x = _mm256_set_pd (41124.234,2344.2354,8653.65635,856.43576);
u.x = _mm256_div_pd (s1.x, s2.x);
for (i = 0; i < 4; i++)
e[i] = s1.a[i] / s2.a[i];
if (check_union256d (u, e))
abort ();
}
It's from Intel AVX which is:
a new 256 bit instruction set extension to SSE and is designed for applications that are Floating Point (FP) intensive.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
Hi guys i know what NaN(let me say i know the acronym stands for Not a Number) is but i don't understand why C++ returns it - The following is the approximation of the mathematical constant e - When using the debugger the functions evaluate fine, it's when writing to the console that it returns NaN
Thanks for any feedback
double Factorial(int k)
{
if(k == 0)
return 1;
int value = 1;
for(int i = k; i > 0; i--)
value *= k;
return value;
}
double e(int p)
{
double value = 0.0;
for(int i = 0; i < p; i++)
{
value += 1/Factorial(i);
}
}
You don't return a value in your e function.
You forgot to return value at the end of e. I don't know when c++ stopped warning about missing returns.