|
-
Oct 30th, 2004, 02:14 PM
#1
Thread Starter
Hyperactive Member
my brain isn't working today folks, need help with a math formula
Alrighty, I am trying to:
I have a float data type, Misc, that starts at a predetermined value...in this case 0.0.
My program loops it in increments of 0.1 until 2.5 is met, at which point it starts over at 0.0. How can I have it count backwards in increments of 0.1 during execution when it reaches 2.5? (So I can get a swing effect instead of start to finish~
Code:
static int Counter;
FLOAT fAngle = Misc += 0.1f;
while (Misc == 0){
for ( Counter = 1; Counter <= 25; Counter++ ){
Misc = Misc + 0.1;
}
}
while (Misc == 25){
for ( int Counter = 25; Counter >= 1; Counter-- ){
Misc = Misc - 0.1;
}
}
This isn't working. There is a flaw somewhere I guess. Anyways, help me if you can.:afrog:
Last edited by KodeShark; Oct 30th, 2004 at 03:02 PM.
-
Oct 30th, 2004, 03:01 PM
#2
yay gay
if (fAngle == 2.5) {
Angle -= 0.1;
}
??
\m/  \m/
-
Oct 30th, 2004, 03:03 PM
#3
Thread Starter
Hyperactive Member
that would put it at 2.4~
-
Oct 30th, 2004, 03:19 PM
#4
yay gay
you have 25 instead of 2.5.
is that it?
\m/  \m/
-
Oct 30th, 2004, 08:45 PM
#5
Thread Starter
Hyperactive Member
nah, i did that for the "for" loop. I didn't want to use a float variable...i usually use int for loops (so i had 1.0 instead of .1)
-
Oct 30th, 2004, 08:59 PM
#6
Lively Member
Off the top of my head:
Code:
float swing = 45.0;
bool left = false;
if(left)
{
swing += 1.0f;
if(swing >= 45.0f)
{
left = true;
}
}
else
{
swing -= 1.0f;
if(swing <= -45.0f)
{
left = false;
}
}
Something like that.
-
Oct 31st, 2004, 07:21 AM
#7
Not NoteMe
The standard game programming method would be to give it a rate of change.
Start it and +vb 0.1, then when it reaches 2.5 change it to -ve 0.1
Then juse increnemt the angle by that each time.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Oct 31st, 2004, 07:57 PM
#8
PowerPoster
A basic example of what SLH explained I believe.
Incorperate that idea into a for loop how you like.
PHP Code:
int delim = 0.1;
int num=0;
num =+ delim;
if (num == 2.5) delim = -0.1;
if (num == 0) delim = 0.1;
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Oct 31st, 2004, 11:09 PM
#9
Thread Starter
Hyperactive Member
Thanks guys. I guess this thread got taken out of my "to watch" list. Ill go work with it now~ =)
-
Nov 1st, 2004, 02:00 PM
#10
Avoid == comparisons with floating point numbers, they might unexpectedly fail, perhaps just when porting from one platform to another. Always use >, >= etc.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 1st, 2004, 04:53 PM
#11
PowerPoster
Really?
== can fail...I guess putting a hint of thought to the subject I can see exactly how it may fail.
floats already have a loss of accuracy...I suppose some platforms may signify your float 2.5 to 3 when it see's == cuz it expects a whole or integeral number.
I'll get out of that habit myself.
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Nov 2nd, 2004, 07:20 AM
#12
No, conversion to integral is not the problem. The problem is different.
Floating point numbers can represent very small numbers with high accuracy or very large numbers with low accuracy, with a linear curve between the two extremes (average numbers with average accuracy, that is). That is the nature of the floating point.
Now, let's say that you're incrementing from 0.0 to 10.0 in steps of 0.1, ending the increment by testing == 10.0. 10.0 could be exactly representable by a float, so you think there's nothing that can go wrong, right? Wrong. For one, this is not portable. For another, perhaps 9.9 is not representable, and you get 9.899123921483 instead. The next increment takes you to 9.999123921483. Again, this is not representable and gets rounded to 10.0. Lucky one. The increment becomes a decrement. Taking 0.1 away from 10.0 yields 9.9 again, so you get 9.899123921483. Taking 0.1 away again gets you 9.799123921483. Because there's higher accuracy at smaller numbers, this stays representable. And so does every such number down to 0.099123921483 (instead of 0.1) and -0,000876078517 (instead of 0.0). Because your lower test was == 0.0, this is not true. You're past the limit, the loop becomes infinite and unbounded. Very bad.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 2nd, 2004, 10:59 AM
#13
Not NoteMe
What data type would you suggest as an alternative?
I need to represent +ve and -ve numbers which would be coordinates of objects. I'd also need to do for loops and comparisons and such, as in your post above.
At the moment i'm using double, and checking things like >= and <=, but sometimes i'd like to check for equality.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Nov 2nd, 2004, 11:43 AM
#14
Usually you don't need another datatype. Just don't use ==.
If you really need known precision, fixed point math or an arbitrary precision math library comes to mind.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 2nd, 2004, 01:15 PM
#15
Not NoteMe
Ok, so i could just make a function like this:
Code:
bool FloatEquals(float f1, float f2, float tollerence)
{
return (f1>=f2-tollerence) && (f1<=f2+tollerence)
}
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Nov 2nd, 2004, 02:00 PM
#16
Originally posted by SLH
Ok, so i could just make a function like this:
Code:
bool FloatEquals(float f1, float f2, float tollerence)
{
return (f1>=f2-tollerence) && (f1<=f2+tollerence)
}
Yeah thats is more or less the way I do it....here is an example:
Code:
inline bool deltaRangeTest(T a, T b, T epsilon)
{
return (absoluteValue(a - b) < epsilon) ? true : false;
}
-
Nov 10th, 2004, 06:59 PM
#17
PowerPoster
What if you used fixed and setprecision(1) to force the decaimal places to stop at 1? Would it still then be inacurate?
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Nov 11th, 2004, 03:02 AM
#18
fixed and setprecision only affect the way the numbers are converted to strings on output, they only have meaning when you're writing the numbers. What is saved in the variable is not affected.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 12th, 2004, 09:04 AM
#19
Not NoteMe
Originally posted by NoteMe
Yeah thats is more or less the way I do it....here is an example:
Code:
inline bool deltaRangeTest(T a, T b, T epsilon)
{
return (absoluteValue(a - b) < epsilon) ? true : false;
}
I guess this would be equivilent (and faster)
Code:
inline bool deltaRangeTest(T a, T b, T epsilon)
{
return (absoluteValue(a - b) < epsilon);
}
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|