Announcement

Collapse
No announcement yet.

240 / 10 = 23,9999

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    240 / 10 = 23,9999

    Hi,

    I got a strange Problem.

    I create a var(float) "UsedTime" in the Character BP. On Eventtick => Delay 0.1 => Then usedTime+0.1.
    Works fine.

    Now create a new BP, just a floating text.

    In the Graph:
    Eventtick => Cast To myChar get UsedTime => UsedTime * 10 => Round (UsedTime) => So I reveice an Int (for example 240).
    Then I divide this Int by 10 and receive a Float (24,0, right?)

    This float is about 30-40% of the time either 23.9999 or 24.00001 (Printed directly via PrintString)

    I looked at some points via "Print String", and it is the division 240 / 10 that makes 23.9999.

    Maybe I am doing something wrong (I can post BP Pics later when I am at home)?
    Anybody seen something like this?

    Thanks and best regards!
    Alex

    #2
    This is not a problem of your code, its more a problem of your understanding of floats.

    Maybe this helps:

    http://stackoverflow.com/questions/2...uracy-examples

    Game Design - Photogrammetry - Programming
    Tutorials · Twitter · Twitch

    Comment


      #3
      Thank you for the Link!
      I get an idea of the "problem". But to really understand I need to read this a few times.
      Is there a simple way around? I just need a simple lap timer.

      P.S. @SchnitzelDude
      Bist du am 21. in Köln dabei?

      Comment


        #4
        Another simple example is: 3 * (1/3) = 1;

        You can easily say that the result is 1, but in your code (1/3) represents sth like 0.3333 and 0.3333 * 3 equals 0.9999.

        If you want to have 2 decimal places, a simple workaround would be to do it like this:

        Round(F * 100) / 100

        That should work, currently I can't test it.

        Game Design - Photogrammetry - Programming
        Tutorials · Twitter · Twitch

        Comment


          #5
          You just need to cast your float to integer and then do the operations.

          Comment


            #6
            Oh - you are right. That should work in this case.
            Last edited by SchnitzelDude; 02-17-2015, 08:52 AM.

            Game Design - Photogrammetry - Programming
            Tutorials · Twitter · Twitch

            Comment


              #7
              Sorry to bother you.

              What do you mean with "Cast to int"?

              Before making the division I convert the float to an Int with the value 240 (for example). Then I divide it by ten and get the result 23.99999

              Here's the Char BP:

              Click image for larger version

Name:	Time.png
Views:	1
Size:	174.4 KB
ID:	1066606

              Comment


                #8
                to put it simple: your computer is just not able to represent decimal values in an accurate way.
                And he is doing mistakes the whole time you run your computer, the reason why you dont see any mistakes usually is that f.e. your operating system can handle all occuring errors that happen.

                So for the future every time you are working with floats,doubles or whatever... just never try to check if two decimal variables are "==". And if you divide some decimal values never expect them to have the value you would have when you solve it on a piece of paper
                Live long and prosper.

                Comment


                  #9
                  just divide 2 integers instead of float and the result should also be an integer.

                  In your case:


                  when your "Round (or Cast to Int)" to int. Do a int / int division instead of float / float
                  Last edited by Wallhalla; 02-17-2015, 10:22 AM.
                  Live long and prosper.

                  Comment


                    #10
                    You round it to int but after that you cast it to float. But you have to keep the int and do a int-division:

                    currently:

                    float(int) / float = float

                    you want:

                    int / int = int

                    Link to docs

                    Game Design - Photogrammetry - Programming
                    Tutorials · Twitter · Twitch

                    Comment


                      #11
                      You could use floor.

                      Click image for larger version

Name:	Capture.JPG
Views:	1
Size:	86.3 KB
ID:	1066609

                      Just curious, why are you counting at 0.1 and then times by 10?

                      Narg
                      Last edited by Narghile; 02-17-2015, 10:31 AM.
                      http://www.seven-mountains.eu/

                      Comment


                        #12
                        Hi,

                        full seconds aren't exact enough, I need 1/10 seconds.
                        So I thought "take a float, since Int is a round number".
                        Float = something around 24.567 (what ever) * 10 = 245 /10 : = 24.5(float) // Print this and I'm done.


                        If I understand you all correct, I will make an int, (logically defined as 1/10 of a second) and add 1 every 0.1 seconds.
                        A value of 245 represents 24.5 seconds

                        Then I need to update my "Display Time" BPs, to format it as xx.x.

                        This way I don't have to handle with floats and it will work fine (as far as I think ).

                        Thank you all for your patience!
                        Alex
                        Last edited by nn23; 02-17-2015, 10:53 AM.

                        Comment


                          #13
                          If you want to build a timer, I wouldn't do it like this.

                          I would use the delta seconds from event tick and just count them -> time += deltaSeconds;

                          Quote from User Guide

                          [ATTACH=CONFIG]25898[/ATTACH]
                          Attached Files
                          Last edited by SchnitzelDude; 02-17-2015, 10:53 AM.

                          Game Design - Photogrammetry - Programming
                          Tutorials · Twitter · Twitch

                          Comment


                            #14
                            The use of Delta Seconds sounds reasonable.
                            I will give this a try!

                            Thanks!

                            Comment


                              #15
                              Nowadays its normal to use the delta secs, because of the amount of seconds your pc needs between to ticks depends on its hardware.

                              E.g.

                              A fast pc can process 200 ticks in 1 second.

                              A slower one can process only 30 per second.
                              Last edited by SchnitzelDude; 02-17-2015, 11:11 AM.

                              Game Design - Photogrammetry - Programming
                              Tutorials · Twitter · Twitch

                              Comment

                              Working...
                              X