Results 1 to 19 of 19

Thread: my brain isn't working today folks, need help with a math formula

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    281

    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.

  2. #2
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    if (fAngle == 2.5) {
    Angle -= 0.1;
    }

    ??
    \m/\m/

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    281
    that would put it at 2.4~

  4. #4
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    you have 25 instead of 2.5.
    is that it?
    \m/\m/

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    281
    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)

  6. #6
    Lively Member
    Join Date
    Jan 2004
    Posts
    82
    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.

  7. #7
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    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.


  8. #8
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    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.5delim = -0.1;
    if (
    num == 0delim 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

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    281
    Thanks guys. I guess this thread got taken out of my "to watch" list. Ill go work with it now~ =)

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  11. #11
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    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

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  13. #13
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    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.


  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  15. #15
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    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.


  16. #16
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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;
    }

  17. #17
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    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.


  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  19. #19
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    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
  •  



Click Here to Expand Forum to Full Width