Hi everybody,
I was testing a part of my code when I discovered something strange.
I was testing (function AmawIn) if a variable a is within a range [b,c] and when a was equal to b or c the result could be false.
Changing the types worked but not always. I changed the code testing different algorithms of AmawIn as you can read.
I am using gcc 11.3 (the last adtools). For info, I have just compiled the code with gcc Debian 11.3 with the same output.
After testing a lot, I have got no more ideas.
I hope someone will help me.
I compile with
Quote:
g++ -std=c++11 -athread=native -o test-anim test-anim.cc
Here is the code.
#include <iostream>
#include <cmath>
#include <exec/types.h>
using namespace std;
/*
template <class T>
bool AmawIn(const T a, const T b,const T c) {
//T aa=a,bb=b,cc=c;
// if (b > c) { T tmp=bb; bb = cc; cc = tmp;}
return !(a<b) && !(c<a);
}
*/
/*
bool AmawIn(float64 a, float64 b, float64 c) {
if ( ( ( a >= b) && (a <= c)) || (( a >= c) && ( a <= b)) ) return true;
else { cout <<"Inside AmawiI ( a="<<a<<" b="<<b<<" c="<<c<<")"<<endl;
return false;
}
}
*/
/*
bool AmawIn(const float64 a, const float64 b,const float64 c) {
//T aa=a,bb=b,cc=c;
// if (b > c) { T tmp=bb; bb = cc; cc = tmp;}
return !(a<b) && !(c<a);
}
*/
bool AmawIn(const float64 a, const float64 b,const float64 c) {
return !(a<b) && !(c<a);
}
int main(int argc, char *argv[]) {
bool stopp = false;
bool back = true;
bool infinite = false;
float64 animold;
int32 compteur = 0;
int32 animt = 1;
float64 animS = 1.;
float64 animE = 4.;
float64 animV = 0.2;
float64 animSnew = animS;
float64 animEnew = animE;
float64 anim;
int32 delay1;
int32 delay2;
int32 delay3;
if (animV>=1) {
delay1 = delay2 = delay3 = 0;
} else {
delay1 = static_cast<int>( 1./(animV*2));
delay2 = static_cast<int>( -1.+1./animV);
delay3 = static_cast<int>( 1./animV);
}
if (animV == 0.5) delay2 = 2;
if (back) animt = 2;
if (animS>animE) animV *= -1;
if (back) {
if (animS<animE) animEnew =animE + delay1 * animV;
if (animS>animE) animSnew = animS - delay2 * animV;
}
anim = animSnew;
cout <<endl<<" (animS = "<<animS<<", animE = "<<animE<<", animV = "<<animV<<")";
cout <<endl<<" (animS = "<<animSnew<<", animE = "<<animEnew<<", animV = "<<animV<<")"<<endl;
for (int i=0; (i<1000) && !stopp; ++i) {
if (i%12==0) cout <<endl;
animold = anim;
cout <<anim<<", ";
if (animt > 0) {
anim += animV;
if (!AmawIn(anim, animSnew, animEnew ) ) { //[color=FF6600]HERE IS THE PROBLEM[/color]
cout <<"( in AmawIn: "<<anim<<" not in "<<animSnew<<":"<<animEnew<<")"<<endl;
if ( fabs(anim-animSnew) < fabs(anim-animEnew) ) anim = animS;
else anim = animE;
if (!infinite) --animt;
if (back) {
animV*=-1;
if (animt>0) {
if (animS<animE) anim =animEnew + animV;
if (animS>animE) anim = animE + delay3 * animV;
}
cout <<endl<<endl<<" back commencé (animS = "<<animSnew<<", animE = "<<animEnew<<", animV = "<<animV<<")"<<endl;
} else if ( (animt>1) || ( (animt==1) && infinite ) ) anim = animS;
}
}
if (anim==animold) ++compteur;
if (compteur> 4) stopp = true;
}
cout <<endl;
return 0;
}
Here is the output
(animS = 1, animE = 4, animV = 0.2)
(animS = 1, animE = 4.4, animV = 0.2)
1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6, 2.8, 3, 3.2,
3.4, 3.6, 3.8, 4, 4.2, ( in AmawIn: 4.4 not in 1:4.4) //[color=FF6600]HERE IS THE PROBLEM[/color]
back commencé (animS = 1, animE = 4.4, animV = -0.2)
4.2, 4, 3.8, 3.6, 3.4, 3.2, 3,
2.8, 2.6, 2.4, 2.2, 2, 1.8, 1.6, 1.4, 1.2, ( in AmawIn: 1 not in 1:4.4) //[color=FF6600]HERE IS THE PROBLEM[/color]
back commencé (animS = 1, animE = 4.4, animV = 0.2)
1, 1, 1,
1, 1,